Search code examples
graphqlapolloapollo-server

Returning a totalCount field in apollo and Relay pagination


In our GraphQL Server we use relay style cursor pagination.

We want to add a totalCount field to queries. How can we do it so that the extra computations are only done if totalCount is queried by the user?

Explanation: Our resolvers (say, BigmapResolver) use the filtering, sorting and pagination requested by the client to create and return a BigmapConnection that has relay specific fields: edges, pageInfo, cursor, node.

Now it's easy to add a totalCount fields to this, but then we have to compute it regardless of whether or not the user is querying for it.

What is the right way to do this in Apollo server?


Solution

  • The library graphql-parse-resolve-info allows parsing the resolve info and enables exactly the needed behavior.