Пагинация в 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 рабочих дня.







