Groupby returning multiple entries

I’m using @groupby to list values stored in a field (type). I was expecting a single entry for every value, but DQL query is returning the value multiple times.

DQL Query:

{
  var(func: type(TestDto)) @groupby(TestDto.type) {
      node as count(uid)
  }

  result(func: uid(node)) {
      type: TestDto.type
      count: val(node)
  }
}

Returns:

{
  "result": [
    {
      "type": "MyType2",
      "count": 3
    },
    {
      "type": "MyType2",
      "count": 3
    },
    {
      "type": "MyType2",
      "count": 3
    }
  ]
}

I was expecting just one entry grouped as:

{
  "result": [
    {
      "type": "MyType2",
      "count": 3
    }
  ]
}

Is that the expected behaviour? Or I’m missing some setting in the schema?

Info:
Dgraph v21.12.0, Deployed in kubernetes with dgraph/dgraph docker image.

Schema added to /admin/schema :

type TestDto @generate(
  query: {
    get: true,
    query: true,
    password: false,
    aggregate: true
  },
  mutation: {
    add: false,
    update: false,
    delete: false
  },
  subscription: false
) {
  mrId: String! @search(by: [hash])
  type: String @search(by: [exact])
}

Mutation adding test data (POST JSON to /mutate):

{
"set": [
    {
        "dgraph.type": "TestDto",
        "uid": "_:x",
        "TestDto.mrId": "724095000ed07",
        "TestDto.type": "MyType2"
    },
    {
        "dgraph.type": "TestDto",
        "uid": "_:y",
        "TestDto.mrId": "724095000ed08",
        "TestDto.type": "MyType2"
    },
    {
        "dgraph.type": "TestDto",
        "uid": "_:z",
        "TestDto.mrId": "724095000ed09",
        "TestDto.type": "MyType2"
    }    
]
}

My goal is to also create a custom resolver with the groupby query to use from GraphQL.

Thanks!!

Why not:

{
  result(func: type(TestDto)) @groupby(TestDto.type) {
      type: TestDto.type
      count: count(uid)
  }
}

I think the reason for what you are seeing is because the uid function of node returns the set of ids in the group not a singular uid

@amaster507 , if I try your suggestion I get the error:

Only aggregator/count functions allowed inside @groupby.

@gajanan this syntax should be supported.

@MichelDiz or @matthewmcneely any help with group by? Not much experience with this part of DQL

This result is expected. Cuz result(func: uid(node)) the variable called node holds 3 different UIDs. So, that block called result will replicate the 3 entities. Not what you expects.

I recomend this bellow.

{
  result(func: type(TestDto)) @groupby(TestDto.type) {
      count(uid)
  }
}

It will return this and you have to do some kind of destructuring in your coding.

data.result.@groupby

{
"data": {
    "result": [
      {
        "@groupby": [
          {
            "TestDto.type": "MyType2",
            "count": 3
          }
        ]
      }
    ]
  }
 }
1 Like

@MichelDiz I also want to use it from a custom resolver for GraphQL

type Query {

  queryTestDtoTypes: [TestDto] @custom(dql: """
    query q() {

      var(func: type(TestDto)) @groupby(TestDto.type) {
          node as count(uid)
      }

      queryTestDtoTypes(func: uid(node)) {
          type: TestDto.type
      }
    }
  """)
}

I tried you suggestion before, but I could not make it work within the custom resolver.

You may use lambdas for this case.

See