Skip to content

Lesson 3 of 9 · 21m

Pagination and Query Shape for Real Clients

Cursor connections versus offsets, stable ordering, filtering, and the schema choices that prevent duplicate rows and runaway queries.

Offset pagination is how teams discover race conditions in production. A client asks for page two while someone inserts a row on page one, and suddenly an item is duplicated or missing. Nobody notices in a demo. Support notices when a billing export skips a customer. GraphQL does not solve pagination for you; it gives you room to design it properly.

Prefer cursors for changing collections

A cursor is an opaque token pointing at a position in a stable ordering. The client does not need to know what is inside it:

type Query {
  auditEvents(first: Int = 50, after: String, filter: AuditEventFilter): AuditEventConnection!
}

type AuditEventConnection {
  edges: [AuditEventEdge!]!
  pageInfo: PageInfo!
  totalCount: Int  # expensive; expose only if the product needs it
}

type AuditEventEdge {
  cursor: String!
  node: AuditEvent!
}

type PageInfo {
  hasNextPage: Boolean!
  endCursor: String
}

A typical implementation encodes the sort values, not a row offset:

{"createdAt":"2026-11-30T19:41:03Z","id":"evt_01J9V4"}

Then the database predicate is the continuation of the ordering:

WHERE (created_at, id) < ($1, $2)
ORDER BY created_at DESC, id DESC
LIMIT $3 + 1

Fetch one extra row to compute hasNextPage without a second count query.

Stable ordering is mandatory

If you sort only by created_at, two rows can share the same timestamp and swap positions between requests. Add a unique tiebreaker such as id. The cursor must include every ordering component, or the next query is not deterministic.

Offset pagination still has a place

Use offset/limit for small, mostly static collections or admin tables that explicitly need numbered pages. Do not use it for feeds, audit logs, search results, or anything ordered by time while writes continue.

Filters belong in input objects

input AuditEventFilter {
  workspaceId: ID!
  actorIds: [ID!]
  actions: [AuditAction!]
  occurredAfter: DateTime
  occurredBefore: DateTime
}

Input objects give you a migration path: adding a filter is additive, while positional arguments multiply into unusable combinations. Validate combinations server-side. A filter that silently ignores half its inputs is worse than an error.

Bound query shape aggressively

Set maximum page sizes, reject invalid cursor direction combinations, and consider whether last/before backward pagination is worth implementing. Many products only need forward pagination. If you implement Relay-style arguments because a blog post told you to, implement them correctly or not at all.

Field notes

  • Cursor opacity is not encryption; sign or HMAC cursors if clients should not tamper with internal state.
  • totalCount can be the most expensive field in the graph. Make it optional or omit it.
  • Sorting and filtering must use the same underlying indexes, or pagination becomes a full-table scan.
  • Document whether cursors survive filter changes. Usually they should not.
  • Returning endCursor when there is no next page confuses less than returning null in some implementations — pick one behavior and test it.

Was this lesson helpful?

Sign in to rate