Q13 of 20 · GraphQL
How do you test pagination in a GraphQL API?
GraphQLMidgraphqlpaginationtest-designapi
Short answer
Short answer: Most GraphQL APIs use cursor-based (connections/edges/pageInfo) pagination rather than offset. Test that cursors page forward correctly, `pageInfo.hasNextPage` is accurate, the last page terminates, and no items are duplicated or skipped across pages.
Detail
GraphQL commonly uses the cursor connection pattern (the Relay spec):
query {
orders(first: 10, after: "cursor123") {
edges { node { id } cursor }
pageInfo { hasNextPage endCursor }
}
}
Test cases:
- Forward paging: fetch page 1, take
pageInfo.endCursor, pass it asafterto fetch page 2; assert page 2 continues where page 1 ended. - Termination: page to the end; assert
hasNextPagebecomesfalseand the final page has the expected remainder. - No gaps or dupes: collect IDs across all pages; assert the set matches an unpaginated count with no duplicates and nothing skipped.
- Boundary:
first: 0, very largefirst, and an invalid cursor should each behave sensibly (empty, capped, or a clean error). - Stability under change: cursor pagination should handle items being added/removed mid-pagination more gracefully than offset — worth testing if the data is live.
If the API uses offset pagination instead (limit/offset), test the classic offset pitfalls: drift when items are inserted between page fetches.
// WHAT INTERVIEWERS LOOK FOR
Knowing the cursor/connection pattern, and testing continuity (no gaps/dupes) and correct termination via pageInfo.
// Related questions