03 / 03

How to implement Cursor-Based Pagination in GraphQL?

Difficulty: 6/10
cursor encoding, relay pagination spec, keyset pagination

This approach uses a to keep track of the current position in the dataset and fetch the next set of results. It is more efficient for large datasets as it avoids the operation. Better suited for real-time applications where data changes frequently.

Schema Definition:
Resolver:
Query Example:

Scenario Questions

0-2 years experience

  1. 1We have a GraphQL query `posts(first: 10, after: $cursor)`. How would you implement the resolver to return the next page of posts?
  2. 2If a client sends a malformed base64 cursor, what response should your server give and how would you detect it in code?

2-5 years experience

  1. 1After adding a new filter argument to the posts connection, the pagination started returning duplicate items. What could be causing this and how would you fix it?
  2. 2Explain the trade‑offs between encoding a row ID versus a timestamp in an opaque cursor, especially when the data can be reordered.

5-8 years experience

  1. 1Design a cursor‑based pagination layer for a GraphQL API that aggregates data from several microservices. How do you guarantee a consistent ordering and stable cursors across services?
  2. 2How would you handle pagination when the underlying dataset is frequently mutated, ensuring the client neither misses nor sees duplicate records?
  3. 3Compare the performance impact of using SQL OFFSET versus keyset pagination behind a GraphQL cursor for a high‑traffic feed.

8+ years experience

  1. 1Our platform is migrating from offset‑based pagination to cursor‑based GraphQL connections across many services. Outline a migration plan that minimizes breaking changes and supports backward compatibility.
  2. 2How would you standardize cursor generation and validation across dozens of teams to avoid security leaks and maintain observability?
  3. 3If different teams need forward‑only versus bidirectional cursor semantics, how would you design the schema and infrastructure to accommodate both while keeping a unified pagination contract?

Follow-up Questions

  • What would you change if the client also needed to paginate backwards?
  • How do you ensure the cursor stays stable when rows are inserted or deleted?
  • Can you describe how you’d unit‑test this pagination resolver?
Share

Share via WhatsApp, X, Facebook, LinkedIn or copy link. Open Graph preview enabled.