Count total before pagination

What I want to do

Get total count of entries prior to pagination.

What I did

I tried out essentially the following query

query CountingQuery{
    stateLocations as var(func: eq(Location.state, "California"))

    moreFilteredLocations as var(func: eq(Location.otherPredicate, "Some Other Filter")) @filter(uid(stateLocations)) {
        total as count(uid)
    }
  
    FilteredLocations(func: uid(moreFilteredLocations), first:10, offset:0) {
        total: val(total)
        uid
        Location.name
        Location.state
        Location.otherPredicate
    }
}

Expecting to see total show up in the response. But I just got the list of entries that contains all the other data and not the total. I know if I did count(uid) in the final root query it would just return 10 since that’s what I set the pagination to. Is there something I’m doing wrong with the query variables?

 "FilteredLocations": [
      // I was expecting total to be here
      {
        "uid": "0xd431",
        "Location.name": "Test Location 1",
        "Location.state": "California",
        ...
      },
      {
        "uid": "0xd4ed",
        "Location.name": "Test Location 2",
        "Location.state": "California",
        ...
      },
      ...
]

Dgraph metadata

dgraph version v21.03.0-76-ged09b8cc1

Well, in DQL, you can not control what position the value will come from. But I suspect that it should be always at the top or always at the bottom. Not sure, I don’t remember from the top of my mind. About the “total” there’s no “total concept” in DQL functions. This means the count uid will always obey the given parameters in the query filters and query root params. You should use the first block as the Total value you want. Instead of waiting for it in the FilteredLocations query body.

Let me know if I got your question right.

Cheers.

I’m not entirely sure what you mean by this. Would you be able to write out a sample of what this would look like?

Just change this from var to “total”.

but then it becomes an int, and i wouldnt be able to use that as a list of filtered uids to pass down further no? Or does it still get stored as a list of uids that i can use to filter later on?

You can easily transform it into a string in any language.

Have you tried? Believe me, the var will be useable. Or maybe I don’t know what you are talking about. Please, elaborate.

of course, I’m not sure what is the misunderstanding. Please, try it and let me know if is that what you need.

So I tried it, it didn’t work. It just gave me an empty total result back.

However I managed to get another workaround by adding another query

 			Total(func:uid(moreFilteredLocations)) {
 				Count: count(uid)
 			}

It should be like this

query CountingQuery{
    stateLocations as var(func: eq(Location.state, "California"))

    moreFilteredLocations as Total(func: eq(Location.otherPredicate, "Some Other Filter")) @filter(uid(stateLocations)) {
        total as count(uid)
    }
  
    FilteredLocations(func: uid(moreFilteredLocations), first:10, offset:0) {
        total: val(total)
        uid
        Location.name
        Location.state
        Location.otherPredicate
    }
}

So I will always need the Total as the name of the query of the last query before the root query right? I ask because I’m trying to generate the overall query and adding the intermediate filter queries in between as I go before passing the final list of filtered locations into the root query.

I just want to confirm that if I were to do it this way, I would need to keep track of which was the last filtered query used and change that query name from var to Total. I will play with it more on my end as well but I just want to confirm my understanding is correct.