⬡ GraphQL Intermediate

How do you implement pagination in GraphQL?

Answer

Two main pagination styles in GraphQL: Offset/limit pagination: simple but has problems at high offsets. query { users(limit: 20, offset: 40) { id name } }. Cursor-based (Relay-style) pagination: the recommended approach. Uses opaque cursors (base64-encoded IDs or timestamps) as pointers into the dataset. Schema: type UserConnection { edges: [UserEdge!]! pageInfo: PageInfo! } type UserEdge { cursor: String! node: User! } type PageInfo { hasNextPage: Boolean! hasPreviousPage: Boolean! startCursor: String endCursor: String }. Query: { users(first: 20, after: "cursor123") { edges { node { name } cursor } pageInfo { hasNextPage endCursor } } }. Cursor pagination is stable (no items missed/duplicated when data changes) and scales to any dataset size. Libraries like prisma-relay-cursor-connection implement the Relay Cursor Connections spec.