How can I get the total number of results for use in pagination

I’m trying to get pagination working in my React application. I’ve setup Apollo Server but now I need to get pagination working. I’m looking to use offset-based pagination.

I have a couple of questions:

  1. When implementing limit on the server, do I use it to limit the results returned from the database or should I return the full set of matching results and then limit the results returned from the resolver?

  2. How can I return the total number of results matching a search query so that I can use it in pagination on the client-side?

  3. Do I need to include the offsetLimitPagination in the field policy?


I think I’ve figured these questions out now.

  1. Results shouldn’t be limited when querying the database. Instead they should be limited in the resolver with slice() used for pagination.

  2. I altered my schema to create a new return type:

type PaginatedResults {
    totalCount: Int
    result: [SearchResult]

I then was able to count the number of results returned from the database and return it with the relevant slice (page) as the result.

  1. It seems that offsetLimitPagination is required in addition to fetchMore to merge pages of results into a single store in the cache.