GraphQL pagination with cursor-based and offset-based approaches

Our company is engaged in the development, support and maintenance of sites of any complexity. From simple one-page sites to large-scale cluster systems built on micro services. Experience of developers is confirmed by certificates from vendors.
Development and maintenance of all types of websites:
Informational websites or web applications
Business card websites, landing pages, corporate websites, online catalogs, quizzes, promo websites, blogs, news resources, informational portals, forums, aggregators
E-commerce websites or web applications
Online stores, B2B portals, marketplaces, online exchanges, cashback websites, exchanges, dropshipping platforms, product parsers
Business process management web applications
CRM systems, ERP systems, corporate portals, production management systems, information parsers
Electronic service websites or web applications
Classified ads platforms, online schools, online cinemas, website builders, portals for electronic services, video hosting platforms, thematic portals

These are just some of the technical types of websites we work with, and each of them can have its own specific features and functionality, as well as be customized to meet the specific needs and goals of the client.

Our competencies:
Development stages
Latest works
  • image_web-applications_feedme_466_0.webp
    Development of a web application for FEEDME
    1161
  • image_ecommerce_furnoro_435_0.webp
    Development of an online store for the company FURNORO
    1041
  • image_crm_enviok_479_0.webp
    Development of a web application for Enviok
    822
  • image_crm_chasseurs_493_0.webp
    CRM development for Chasseurs
    847
  • image_website-sbh_0.png
    Website development for SBH Partners
    999
  • image_website-_0.png
    Website development for Red Pear
    451

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.