Using custom DQL with groupby

Hi,

I’m trying to write a DQL query I like to use in a custom query in GraphQL based on the @groupby aggregator. I’m grouping a type on one of it’s field and the result is:

"data": {
    "mr": [
      {
        "@groupby": [
          {
            "key": "Catégorie",
            "count": 5
          },
          {
            "key": "Genre",
            "count": 41
          },
          {
            "key": "Image",
            "count": 1025
          }
        ]
      }
    ]
  }

Then I’m trying to define types to represent the data returned by that DQL queries in GraphQL schema without success:

type PropertyMap @remote {
    key: String
    count: Int
}

type GroupedPropertyMapQ @remote {
    groupby: [PropertyMap]
}

type Query {
  queryPropertyKeyMap: [GroupedPropertyMapQ] @custom(dql: """
    {
      q(func: type(Property)) @groupby(key: Property.key) {
        count(uid)
      }
    }"""
  )
}

I’m not able to provide a correct schema if I’m adding the @ character of @groupby. I tried to escape it.

2 Likes

Any help here?
Can someone confirm the issue? Should I open a ticket somewhere?

1 Like

Hi @doude,

Welcome to Dgraph community !! Sorry, for taking this long to reply.

What error are you getting while using the above GraphQL schema with custom DQL ?
I tried applying the provided GraphQL schema using

curl -X POST localhost:8080/admin/schema --data-binary '@schema.graphql'

and it returned success. The GraphQL schema was applied without any problems.

What version of Dgraph are you using ? and what is your setup ?

1 Like

Hey @doude, sorry we missed it.

@rajas I think he is saying that he is not able to do use @ in naming groupby field inside the GrpahQL schema like this:

type GroupedPropertyMapQ @remote {
    @groupby: [PropertyMap]
}

That is not possible because the GraphQL spec limits the character set of names in the schema to this: /[_A-Za-z][_0-9A-Za-z]*/

I believe we will need to figure out a way to support this use case with @groupby. Dgraph response will be returning @groupby, so we need a way to convey to the GraphQL layer to parse it using @groupby and not just groupby. Maybe we can enhance @dgraph directive to support this use-case.
Thanks @doude for bringing this to our notice, we will try to find out a way about how to achieve this and will keep you updated on the same.

2 Likes

Hey @doude, this issue has been fixed in the master. Please see this PR for more details. We have introduced one new directive named as @remoteResponse. Using this directive you can modify your schema a little bit and can get the desired result. Just change this type:-

type GroupedPropertyMapQ @remote {
    groupby: [PropertyMap] @remoteResponse(name: "@groupby")
}
2 Likes

thanks @rajas @abhimanyusinghgaur and @minhaj that is perfect.

1 Like