How do you implement pagination in GraphQL?
Answer
Two main pagination styles in GraphQL: Offset/limit pagination: simple but has problems at high offsets. query { users(limit: 20, offset: 40) { id name } }. Cursor-based (Relay-style) pagination: the recommended approach. Uses opaque cursors (base64-encoded IDs or timestamps) as pointers into the dataset. Schema: type UserConnection { edges: [UserEdge!]! pageInfo: PageInfo! } type UserEdge { cursor: String! node: User! } type PageInfo { hasNextPage: Boolean! hasPreviousPage: Boolean! startCursor: String endCursor: String }. Query: { users(first: 20, after: "cursor123") { edges { node { name } cursor } pageInfo { hasNextPage endCursor } } }. Cursor pagination is stable (no items missed/duplicated when data changes) and scales to any dataset size. Libraries like prisma-relay-cursor-connection implement the Relay Cursor Connections spec.
Previous
What is the args argument in a GraphQL resolver?
Next
What is the Relay specification in GraphQL?
More GraphQL Questions
View all →- Intermediate What is the Relay specification in GraphQL?
- Intermediate How do you implement authorization in GraphQL resolvers?
- Intermediate What is graphql-shield and how does it work?
- Intermediate How do you implement real-time subscriptions with GraphQL?
- Intermediate What is schema stitching in GraphQL?