Пагінація в GraphQL: cursor-based та offset
GraphQL має дві основні стратегії пагінації: offset (LIMIT/OFFSET) та cursor-based (Relay Connection). Cursor-based коректно працює при змінах даних між запитами сторінок, offset простіший у реалізації й підтримує довільні стрибки на сторінку.
Offset пагінація
Підходить для адміністративних таблиць та списків з рідкими оновленнями:
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
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 пагінація (Relay Connection)
Стандарт Relay—правильний вибір для нескінченної прокрутки та часто змінюваних даних:
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
}
// Курсор—base64-закодований ID або 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 = []
// Застосувати фільтри
if (filter?.authorId) {
params.push(filter.authorId)
conditions.push(`author_id = $${params.length}`)
}
// Cursor умова
if (after) {
const afterId = decodeCursor(after)
params.push(afterId)
conditions.push(`id < $${params.length}`) // для DESC сортування
}
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'
// Запросити на 1 більше для визначення 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)
}))
// Підрахувати total (тільки якщо запрошено—дорога операція)
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 за timestamp для часових рядів
Для даних з неунікальним порядком використовуйте складений cursor:
// Cursor кодує (created_at, id)—два поля для однозначної пагінації
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 умова для складеного cursor
// Виключити записи з тим же timestamp, але більшим ID
const cursorCondition = after
? `(created_at < $1 OR (created_at = $1 AND id < $2))`
: null
Використання на клієнті (Apollo Client)
// Нескінченна прокрутка з 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
]
}
}
}
})
}
// З Apollo Client 3—InMemoryCache field policies
const cache = new InMemoryCache({
typePolicies: {
Query: {
fields: {
posts: relayStylePagination(['filter'])
}
}
}
})
Порівняння стратегій
| Критерій | Offset | Cursor |
|---|---|---|
| Довільний стрибок на сторінку | Так | Ні |
| Коректність при вставках | Ні (дублі/пропуски) | Так |
| Сортування за будь-яким полем | Просто | Вимагає індекс |
| Нескінченна прокрутка | Ні | Так |
| Масштабованість (OFFSET 1M) | Повільно | Швидко |
| Реалізація | Простіше | Складніше |
Терміни
Реалізація пагінації (offset + cursor Relay Connection) для GraphQL API—1–2 робочих дні.







