Looking for a count aggregator on DQL

I am implementing a pagination feature for a REST API using the “first” and “offset”. With this, I can paginate through pages and even display the “previous” and “next” links. Something like:

{
  "count": 2,
  "next": "next_link...",
  "previous": "previous_link...",
  "results": [...]
}

My problem now is that I’m not finding a way to calculate the “count” field. The intention of this field is to display the total available results for that query, not only the ones returned in the page.

Is there an aggratation feature for count available on GraphQL + -?

1 Like

As far as I know, the count within the same return block would honor the first and offset parameters. However, you could use a separate block to do total counts with DQL.

Something like:

query {
  anythingHere(func: type(YourType), first: 25, offset: 75)  {
    expand(_all_)
  }
  anyNameHere(func: type(YourType)) {
    total: count(uid)
  }
}

On a side note, I am quite confused for the purpose of layering a REST API on top of a GraphDB? GraphQL is a replacement for REST APIs. Do you have clients that require a REST API? This reverses many of the optimizations of having a graph database: underfetching, overfetching, and single endpoint to name a few. Not saying there may not be a use case, I just personally feel like it is taking a step backwards.

3 Likes

Hi @amaster507. Yeah, I was thinking about using a second block but wasn’t sure if there were another way. Maybe it is what other query engines do under the hood too. We will also consider the side note.

Thanks for the reply! :grinning:

1 Like