Tutorial

GraphQL Cursor Pagination

Paginate a GraphQL list with cursors rather than offsets so inserts during paging do not shift the window, using the Relay connection spec.

On this page

  1. Introduction
  2. Request edges and cursors
  3. Pass after on the next page
  4. Stop at hasNextPage
  5. Stable to inserts
  6. Sort key cursors
  7. Test the insert
  8. Sources

Introduction

This guide covers GraphQL cursor pagination, a recurring concern in the data-fetching scenario. Offset pagination breaks when an item is inserted at the top during paging: the next offset overlaps the previous page. Cursor pagination uses an opaque cursor tied to the last item, so the next page starts after it regardless of inserts, which is why the Relay connection spec uses cursors.

Request edges and cursors

Ask for edges with node and cursor plus pageInfo, so each item carries its own position and the page tells you whether more exists.

The trade-off worth naming is that ask for edges with node and cursor plus pageinfo, so each item carries its own position and the page tells you whether more exists adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.

query Orders($after: String) {
  orders(first: 20, after: $after) {
    edges { node { id amount } cursor }
    pageInfo { hasNextPage endCursor }
  }
}

Pass after on the next page

Store the endCursor and pass it as after on the next fetch, so the server returns the window after that item, not a shifted offset.

The trade-off worth naming is that store the endcursor and pass it as after on the next fetch, so the server returns the window after that item, not a shifted offset adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.

let cursor = null;
async function nextPage() {
  const r = await fetch('/gql', { method: 'POST', body: JSON.stringify({ query, variables: { after: cursor } }) }).then(r => r.json());
  cursor = r.data.orders.pageInfo.endCursor;
  return r.data.orders.edges;
}

Stop at hasNextPage

Check pageInfo.hasNextPage and stop when false, so you do not fetch an empty page and the UI knows when to hide the more button.

The trade-off worth naming is that check pageinfo.hasnextpage and stop when false, so you do not fetch an empty page and the ui knows when to hide the more button adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.

Stable to inserts

Because the cursor is tied to an item, an insert at the top does not shift the next window, so paging does not duplicate or skip, which is the point over offsets.

The trade-off worth naming is that because the cursor is tied to an item, an insert at the top does not shift the next window, so paging does not duplicate or skip, which is the point over offsets adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.

Sort key cursors

For sorted lists, encode the sort key in the cursor so the server can resume after that exact position, which handles non-id orderings.

The trade-off worth naming is that for sorted lists, encode the sort key in the cursor so the server can resume after that exact position, which handles non-id orderings adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.

Test the insert

Load page one, insert an item at the top, load page two, and assert no duplicate and no skip, so cursor pagination holds under change.

The trade-off worth naming is that load page one, insert an item at the top, load page two, and assert no duplicate and no skip, so cursor pagination holds under change adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.

Sources

Sources: Relay connection spec, GraphQL pagination.

FAQ

Cursor or offset for GraphQL?
Cursor, because an insert or delete during paging shifts offsets and causes duplicates or skips, while a cursor is stable to changes above it.
What is a cursor?
An opaque token encoding the position of an item, usually base64 of the id or a sort key; the client passes it back as after to get the next page.

Related Examples