2

I'm trying to build something to search my data that is stored in Craft with graphql. To make pagination work I want to know how many results there are in total.

I'm doing a query like this:

query($search: String, $relatedTo: [Int]) {
  entries(section: "address", search: $search, orderBy: "companyName", relatedTo: $relatedTo, limit: 10, offset: 0) {
    searchScore, 
    ... on address_address_Entry {
      sid,
      address_title,
    }
  }
}

Truncated for readability

I set a limit and an offset to see the first 10 results. But i also need to know the total amount to determine how many pages there are

I've seen that there is a _count field of type Int on the entryInterface which says "Return a number of related elements for a field." But it seems to just give me the field's value itself, if I, for example, add it like this:

query($search: String, $relatedTo: [Int]) {
  entries(section: "address", search: $search, ... same) {
    _count:id,
  }
}

It just adds in the response {"_count": "8154"} (which is the id, so not very usefull)

I've looked at graphql docs: https://graphql.org/learn/pagination/

But it doesn't work to just add 'pageInfo' or 'totalCount', neither does the cursor stuff.

Any help to get this working is welcome!

2

Try entryCount. You can also add arguments to this, such as section: entryCount(section:"news"), for example.

2
  • Thanks, but: There doesn't seem to be such option in what i'm trying. Can you elaborate how you've done this? All i get is "cannot query "entryCount" on query" or "cannot query "entryCount" on type elementInterface" Or i just don't know where to put the "entryCount", also possible!
    – Vincent
    Jun 18 '20 at 15:32
  • Fastforward however much time has passed. Looking back at graphql in the current version of Craft (3.5.8) and 'entryCount' works fully as expected. I haven't looked into the changelog, but I'm assuming it has been added somewheren between then and now! Marking your answer as accepted for if anybody is looking for it.
    – Vincent
    Sep 7 '20 at 10:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.