Search code examples
javascriptreactjsrelayjsgraphql-js

How to pass total count to the client in pageInfo


I use first after and last before to do pagination. hasNextPage and hasPreviousPage are very useful.

But what I need is also the total count so that I can calculate and show things like page 5 of 343 pages on the client.

Unfortunately that is not part of pageInfo even though I have the information on the server site.

Can you please include a total field in the pageInfo and extend connectionFromArray to take in the total arrayLength like connectionFromArraySlice already does?

Thanks


Solution

  • pageInfo is designed to represent information about the specific page, whereas the total number of items is really a property of the connection itself. We recommend adding a count field to the connection. You might query it with:

    fragment on TodoList {
      tasks(first: 10) {
        count # <-- total number of tasks
        edges { ... }
        pageInfo { ... }
    }
    

    Relay supports arbitrary fields on a connection, so you're free to name this count, totalCount, etc.