Skip to content

Latest commit

 

History

History
42 lines (30 loc) · 1.16 KB

pagination.md

File metadata and controls

42 lines (30 loc) · 1.16 KB

How to implement pagination with GraphQL

There are various ways of implementing pagination. My favorite is using offset and limit. If prefer it over cursor or page / display_on_page because of it's simplicity. You don't need to calculate anything, just pass raw values.

First, add these properties as variables to a GraphQL query:

type Query {
  allPositions(limit: Int, offset: Int): [Position!]!
}

Second, add them to the allPositions resolver:

export const resolvers = {
  Query:
  {
    allPositions: (_: any, { limit, offset }: { limit: number, offset: number }) => allPositions(limit, offset),
  }
}

Finally, use them in a data fetching query. I'm using MongoDB with Deno, so this is how it looks:

export const allPositions = async (limit: number, offset: number) => {
  const db = database();

  const positions = db.collection<PositionSchema>("positions");

  return await positions.find({}, FIND_OPTIONS).skip(offset).limit(limit).toArray();
}

If you were to use SQL, then it would be:

SELECT * FROM dbo.Positions OFFSET @offset FETCH @limit

where @offset and @limit are the variables.