Cursor-Based GraphQL Pagination: Avoid Duplicates and Skips
Why offset/limit can duplicate or skip items, and how Relay-style cursor connections keep GraphQL pages stable.
17 Jul 2025, 04:21 UTC

The duplicate-and-skip problem
Imagine a blog API that lists posts with offset and limit. A reader opens page two, which starts at post 11. While they read, an editor publishes a new featured post at the top of the feed. When the reader clicks "next," the same offset now points to a different post. The reader sees post 10 again, or post 21 never appears. That is the classic offset/limit instability under concurrent writes.
GraphQL does not define pagination itself. Servers and clients usually adopt the Relay connection pattern: list fields accept first, last, after, and before, and return edges, nodes, and pageInfo. A cursor is an opaque string that marks a position in an ordered list, not an integer offset.
What a cursor connection looks like
Here is a minimal schema sketch. Field names and cursor encoding vary by server library, so treat this as a pattern, not a portable contract.
type Post {
id: ID!
title: String!
}
type PostEdge {
node: Post!
cursor: String!
}
type PageInfo {
hasNextPage: Boolean!
hasPreviousPage: Boolean!
startCursor: String
endCursor: String
}
type PostConnection {
edges: [PostEdge!]!
pageInfo: PageInfo!
}
type Query {
posts(first: Int, after: String, last: Int, before: String): PostConnection!
}
A client asks for the first page:
query {
posts(first: 10) {
edges {
node { id title }
cursor
}
pageInfo {
hasNextPage
endCursor
}
}
}
The response includes an endCursor value whose format is server-specific. To get the next page, the client sends that exact string back in after:
query {
posts(first: 10, after: "PLACEHOLDER_CURSOR") {
edges {
node { id title }
cursor
}
pageInfo {
hasNextPage
endCursor
}
}
}
Because the cursor identifies a position rather than a row number, inserting a new post before the current page does not duplicate the last item from the previous page or skip the next one.
Worked verification sequence
- Run an introspection query against your endpoint to confirm list fields expose
first/last/after/beforeand apageInfotype. Introspection reveals the schema shape, not runtime behavior. - Request the first page and record
endCursor. - Insert a new item at the top of the ordered list.
- Request the next page using the recorded cursor in
after. - Confirm no
node.idappears on both pages.
Example introspection request, run from a terminal or API client:
curl -X POST https://api.example.com/graphql \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $TOKEN" \
-d '{"query":"{ __type(name: \"Query\") { fields { name args { name type { name kind } } } } }"}'
Where to run: any machine with network access to the GraphQL endpoint. Permissions: whatever the endpoint requires for introspection; some production servers disable it. Placeholders: replace the URL and token. Expected check: the posts or equivalent list field should show pagination arguments. Risk: introspection may expose schema details you intend to keep private.
Trade-offs and limitations
- Complexity: cursor connections require more schema and client code than
offset/limit. For small, static lists, offset/limit is simpler and acceptable. - Opaque cursors: cursor format and validity depend on the server implementation and may change between versions. Do not parse or store cursors as integers across schemas.
- Backward pagination:
last/beforecan be more expensive than forward pagination depending on the data store and indexes. - Cursor invalidation: cursors may become invalid after schema changes, reindexing, or data migration. Test actual insert/delete sequences rather than assuming stability.
Persisted queries are a separate GraphQL feature for smaller payloads and operation allow-listing; they do not replace pagination decisions.
Practical check before you adopt
Compare forward and backward pagination query plans or response times on a production-sized dataset. If your server library documents a default or maximum page size, configure it explicitly instead of relying on defaults. After deploying, monitor whether clients follow pageInfo.hasNextPage instead of incrementing offsets.
Cursor-based connections will not fix every list problem, but they remove the duplicate-and-skip class of bugs that makes offset/limit risky for feeds and other frequently updated lists.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.