Cannot figure out how to use @groupby in custom DQL

I already checked Using custom DQL with groupby but copying what they did didn’t work.
I’m trying to get unique results with the count on fields of the Stems type, like Stems.brand, Stems.color, and so on.

I have this graphQL schema,

type Stems {
	id: ID! 
	link: String! 
	image: String! 
	name: String! @search(by:[fulltext])
	length: Int @search
	rise: Int @search 
	clampDiameter: Float @search 
	steererDiameter: String @search(by:[hash]) 
	color: String @search(by:[hash]) 
	material: String! @search(by:[hash]) 
	price: Float! @search 
	weight: Int @search 
	brand: String! @search(by:[hash]) 
}

I can do what I want using this DQL query in the DQL section of Dgraph Cloud:

{	  
  distinctBrands(func: type(Stems)) @groupby(Stems.brand) {
    count(uid)
  }
}

Returns:

{
  "data": {
    "distinctBrands": [
      {
        "@groupby": [
          {
            "Stems.brand": "Thomson",
            "count": 1
          },
          {
            "Stems.brand": "Easton",
            "count": 2
          },
          {
            "Stems.brand": "FSA",
            "count": 2
          },
          {
            "Stems.brand": "Syntace",
            "count": 2
          },
          {
            "Stems.brand": "OneUp Components",
            "count": 3
          },
          {
            "Stems.brand": "TruVativ",
            "count": 3
          },
   }

So I think what I’m supposed to do to get this to work in my Angular app that runs on web browsers, is to make a custom DQL query.

I have added this to the schema:

type BrandCount @remote {
    brand: String
    count: Int
}

type Query {
  distinctBrands: [BrandCount] @custom(dql: """
   query {	  
    distinctBrands(func: type(Stems)) @groupby(Stems.brand) {
      count(uid)
    }
  }
  """)
}

but when I query like this

query MyQuery {
  distinctBrands {
    brand
    count
  }
}

I get the following

  "data": {
    "distinctBrands": [
      {
        "brand": null,
        "count": null
      }
    ]
  },

Please help, been trying to do this for a long time.

Another thing is that adding “orderasc” makes it only return one group

{	  
    distinctBrands(func: type(Stems), orderasc: Stems.brand) @groupby(Stems.brand) {
      count(uid)
    }
}

Returns:

{
  "distinctBrands": [
    {
      "@groupby": [
        {
          "Stems.brand": "TruVativ",
          "count": 1
        }
      ]
    }
  ]
}

Context:
TL:DR Is there a way to do this such that the groupby returns groups with counts of zero?

I want the unique fields and counts because I’m making a table with filter checkboxes, and they should have a count of how many items that would remain if you were to click that filter checkbox. The count that is returned by the DQL query is exactly what I need. The only issue aside from not being able to get this to work as a custom DQL query is the fact that if I add filtering to this query, then if the count on a group is zero, that group is not returned at all.

It should be like below. This example I made out of the air following what was mentioned in the thread you linked. So you should test out and change according.

The response has 2 levels. So you need 2 types to follow the mapping.

type BrandCount @remote {
    brand: String @remoteResponse(name: "Stems.brand")
    count: Int
}

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

type Query {
  queryPropertyKeyMap: [GroupedPropertyMapQ] @custom(dql: """
    {
    q(func: type(Stems)) @groupby(Stems.brand) {
      count(uid)
    }
    }"""
  )
}
1 Like

@MichelDiz

I tried this:

type PropertyMap @remote {
    brand: String @remoteResponse(name: "Stems.brand")
    count: Int
}

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

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

And queried for:

query MyQuery {
  queryPropertyKeyMap {
    groupby {
      count
      brand
    }
  }
}

But it just returned an empty array.

groupby has an @? maybe…

J

1 Like
query MyQuery {
  queryPropertyKeyMap {
    @groupby {
      count
      brand
    }
  }
}
 "errors": [
    {
      "message": "Expected Name, found @",
      "locations": [
        {
          "line": 3,
          "column": 5
        }
      ]
    }
  ],

I’ve got it working now! The problem was that the DQL query and GraphQL query names didn’t match.
Now if only the sorting would work…

In Custom DQL it says:

The name of the DQL query that you want to map to the GraphQL response, should be same as the name of the GraphQL query.

type PropertyMap @remote {
    brand: String @remoteResponse(name: "Stems.brand")
    count: Int
}

type GroupPropertyMap @remote {
    groupby: [PropertyMap] @remoteResponse(name: "@groupby")
}

type Query {
  queryPropertyKeyMap: [GroupPropertyMap] @custom(dql: """
    {
    queryPropertyKeyMap(func: type(Stems)) @groupby(Stems.brand) {
      count(uid)
    }
    }"""
  )
}