About finding number of outgoing edges

Hi,

Let’s say I have a type with a field that is of the same type. I would like to know the number of edges for that field/ aka number of objects the field has.

Type Store {
  id: ID!
  substores: [Store]
  employees: [Employee]
}

In that case if I want to know how many substores a particular store has, I am trying to query it like this in dql.

query {
  total(func: type(Store)) {
    count(substores)
  }
}

However, the result is that total is empty. Even if there are 0 substores, it shouldn’t be empty. I am assuming it would give me 0.

"data": {
    "total": []
 },

But when I run the same query replacing count(substores) with count(employees), I get the expected result which includes the number of employees. I am thinking this difference could be because substores is of the same type as the parent type, but would like to know what’s the best way to get around this.

So the best thing to do when switching over from GraphQL to DQL is to first check the schema of your types. In this case what was substores in GQL is actually Store.substores in DQL (for bookkeeping purposes, for if you had a different type with the same field name but different field type it would case a clash in fields.

So once you’re done having a look at the DQL schema type running the following →

query {
  getStores(func: type(Store)) {
    count(Store.substores)
  }
}

and you’ll get something as follows →

"data": {
    "getStores": [
      {
        "count(Store.substores)": 0
      },
      {
        "count(Store.substores)": 0
      },
      {
        "count(Store.substores)": 2
      }
    ]
  }

have fun and keep exploring :+1:

1 Like

Thank you!