Pagination in GraphQL: Cursor-Based and Offset
GraphQL has two main pagination strategies: offset (LIMIT/OFFSET) and cursor-based (Relay Connection). Cursor-based works correctly when data changes between page requests, offset is simpler to implement and supports arbitrary page jumps.
Offset Pagination
Suitable for admin tables and lists with rare updates:
type Query {
posts(limit: Int = 20, offset: Int = 0): PostList!
}
type PostList {
items: [Post!]!
total: Int!
limit: Int!
offset: Int!
hasNextPage: Boolean!
}
const resolvers = {
Query: {
posts: async (parent, { limit = 20, offset = 0 }, context) => {
// Limit maximum request size
const safeLimit = Math.min(limit, 100)
const [items, total] = await Promise.all([
context.db.query(
'SELECT * FROM posts ORDER BY created_at DESC LIMIT $1 OFFSET $2',
[safeLimit, offset]
),
context.db.queryOne('SELECT COUNT(*) as total FROM posts')
])
return {
items,
total: parseInt(total.total),
limit: safeLimit,
offset,
hasNextPage: offset + safeLimit < parseInt(total.total)
}
}
}
}
Cursor-Based Pagination (Relay Connection)
Relay standard—the right choice for infinite scroll and frequently changing data:
type Query {
posts(
first: Int
after: String
last: Int
before: String
filter: PostFilter
): PostConnection!
}
type PostConnection {
edges: [PostEdge!]!
pageInfo: PageInfo!
totalCount: Int!
}
type PostEdge {
node: Post!
cursor: String!
}
type PageInfo {
hasNextPage: Boolean!
hasPreviousPage: Boolean!
startCursor: String
endCursor: String
}
// Cursor is base64-encoded ID or timestamp
function encodeCursor(id) {
return Buffer.from(`cursor:${id}`).toString('base64')
}
function decodeCursor(cursor) {
const decoded = Buffer.from(cursor, 'base64').toString('utf8')
const match = decoded.match(/^cursor:(.+)$/)
return match ? match[1] : null
}
const resolvers = {
Query: {
posts: async (parent, { first = 20, after, last, before, filter }, context) => {
const limit = Math.min(first || last || 20, 100)
let query = 'SELECT * FROM posts'
const params = []
const conditions = []
// Apply filters
if (filter?.authorId) {
params.push(filter.authorId)
conditions.push(`author_id = $${params.length}`)
}
// Cursor condition
if (after) {
const afterId = decodeCursor(after)
params.push(afterId)
conditions.push(`id < $${params.length}`) // for DESC order
}
if (before) {
const beforeId = decodeCursor(before)
params.push(beforeId)
conditions.push(`id > $${params.length}`)
}
if (conditions.length) {
query += ' WHERE ' + conditions.join(' AND ')
}
query += ' ORDER BY id DESC'
// Request one extra to determine hasNextPage
params.push(limit + 1)
query += ` LIMIT $${params.length}`
const rows = await context.db.query(query, params)
const hasMore = rows.length > limit
const items = hasMore ? rows.slice(0, limit) : rows
const edges = items.map(row => ({
node: row,
cursor: encodeCursor(row.id)
}))
// Count total (only if requested—expensive operation)
const totalCount = await context.db.queryOne(
'SELECT COUNT(*) FROM posts'
).then(r => parseInt(r.count))
return {
edges,
totalCount,
pageInfo: {
hasNextPage: after ? hasMore : false,
hasPreviousPage: before ? hasMore : false,
startCursor: edges[0]?.cursor ?? null,
endCursor: edges[edges.length - 1]?.cursor ?? null
}
}
}
}
}
Cursor by Timestamp for Time Series
For data with non-unique order, use composite cursor:
// Cursor encodes (created_at, id)—two fields for unambiguous pagination
function encodeTimeCursor(createdAt, id) {
return Buffer.from(JSON.stringify({ t: createdAt, id })).toString('base64')
}
function decodeTimeCursor(cursor) {
try {
return JSON.parse(Buffer.from(cursor, 'base64').toString())
} catch {
return null
}
}
// SQL condition for composite cursor
// Exclude records with same timestamp but larger ID
const cursorCondition = after
? `(created_at < $1 OR (created_at = $1 AND id < $2))`
: null
Client Usage (Apollo Client)
// Infinite scroll with fetchMore
const { data, fetchMore, loading } = useQuery(GET_POSTS, {
variables: { first: 20 }
})
const loadMore = () => {
const endCursor = data.posts.pageInfo.endCursor
if (!endCursor || !data.posts.pageInfo.hasNextPage) return
fetchMore({
variables: { first: 20, after: endCursor },
updateQuery: (prev, { fetchMoreResult }) => {
if (!fetchMoreResult) return prev
return {
posts: {
...fetchMoreResult.posts,
edges: [
...prev.posts.edges,
...fetchMoreResult.posts.edges
]
}
}
}
})
}
// With Apollo Client 3—InMemoryCache field policies
const cache = new InMemoryCache({
typePolicies: {
Query: {
fields: {
posts: relayStylePagination(['filter'])
}
}
}
})
Strategy Comparison
| Criterion | Offset | Cursor |
|---|---|---|
| Arbitrary page jump | Yes | No |
| Correctness with inserts | No (dupes/gaps) | Yes |
| Sort by any field | Easy | Requires index |
| Infinite scroll | No | Yes |
| Scalability (OFFSET 1M) | Slow | Fast |
| Implementation | Simpler | Complex |
Timelines
Implementing pagination (offset + cursor Relay Connection) for GraphQL API—1–2 working days.







