Support GroupBy query

Given the following schema:

type Foo {
   prop: PropType!
}

enum PropType {
   A
   B
   C
   ...
}

I want to find all used values of PropType in my current database.
E.g. when there are two nodes: {prop: A} , {prop: C} I want to get [A,C] as the result.

Note that there can be >10000 Foo nodes in the database and the query shouldn’t take long.

Some options that come to my mind:

  • Custom Query with DQL (does not respect @auth)
  • maintain a list of existing PropTypes (lots of overhead to remember (add, update, delete … ) → error-prone)
  • query all Foo nodes and build the list on the client (over-fetching, potentially slow)

Do I have other possibilities?
How could I formulate this with DQL?

1 Like

Hi @maaft, Sorry for the late reply.
You can do it in DQL by using aggregate function groupby as follows

query {
  queryFoo(func: has(Foo.prop))@groupby(Foo.prop){
    
  }
}

It will return all the distinct values used by prop field.
You will have to do some post processing because result will be in Json in below format

"data": {
    "queryFoo": [
      {
        "@groupby": [
          {
            "Foo.prop": "A"
          },
          {
            "Foo.prop": "B"
          },
          {
            "Foo.prop": "C"
          }
        ]
      }

Groupby function is currently available in DQL only.

Okay, thank you!

As @auth is not working with DQL, could you plan to integrate this within GraphQL?