Skip to content

Pagination ​

The examples on this page use the db.user and db.post tables from Relations.

Paginate a list ​

Declare a page type with nodes and cursor, and a cursor argument on the list field:

gql
type PostPage {
  nodes: [Post!]!
  cursor: String
}

type Query {
  posts(cursor: String): PostPage!
}

Resolve the field with graph.resolvePage and a cursor paginator:

ts
const resolvers = {
  Query: {
    posts: async (_parent, _args, context, info) => {
      return await graph.resolvePage(db.post, r.cursor({ fields: ["-id"], take: 10 }), {
        context,
        info,
      })
    },
  },
}

Request the first page without cursor, then pass the cursor of each page to get the next one:

gql
query posts($cursor: String) {
  posts(cursor: $cursor) {
    nodes {
      id
      text
    }
    cursor
  }
}

You can request just cursor to check for another page. The last page returns cursor: null. Do not register the page type in the graph: the graph resolves its nodes with the resolver of Post.

Currently broken

A query fails when it selects nodes under an alias, such as items: nodes { id }.

Order the pages ​

List the columns that order the rows in fields of r.cursor, prefixing a field with - for descending order, and end it with a unique field, such as id, so that the order is the same on every request. Alternatively, omit fields and order the query itself:

ts
await graph.resolvePage(db.post.order({ id: "DESC" }), r.cursor({ take: 10 }), { context, info })

The pages follow the complete order of the query, including mixed directions and NULLS FIRST or NULLS LAST. An order column may hold NULL when the table declares it nullable. With fields of r.cursor, PostgreSQL's default applies: NULL comes last in ascending order and first in descending order; for another placement, omit fields and order the query with it. The paginator selects the order columns along with the fields that the client selected.

Let clients choose the page size ​

take of r.cursor sets the page size, 10 by default. To let clients choose it, add a take argument to the list field, such as posts(cursor: String, take: Int): PostPage!. It replaces take of r.cursor without an upper limit, so check it in the resolver before resolving:

ts
const resolvers = {
  Query: {
    posts: async (_parent, { take }, context, info) => {
      if (take != null && (take < 1 || take > 100)) {
        throw new Error("take must be between 1 and 100.")
      }
      return await graph.resolvePage(db.post, r.cursor({ fields: ["-id"], take: 10 }), {
        context,
        info,
      })
    },
  },
}

Paginate a relation ​

To paginate a to-many relation, resolve its field with r.page and give it the page type and a cursor argument:

gql
type User {
  id: Int!
  name: String!
  posts(cursor: String): PostPage!
}
ts
const graph = r.graph({
  User: r.table(db.user, {
    fields: {
      id: true,
      name: true,
      posts: r.page(r.cursor({ fields: ["-id"], take: 10 })),
    },
  }),
  Post: r.table(db.post),
})

r.page takes the options of r.relation as its second argument, such as tableField to paginate a relation under another field name:

ts
posts_page: r.page(r.cursor({ fields: ["-id"], take: 10 }), { tableField: "posts" })

Filter a page ​

To let clients filter a page with a filter argument, enable filters as for a list: filters: true in the options of graph.resolvePage, or filters: true in the options of r.page.

Currently broken

The filter argument of a paginated field is ignored. Until it works, apply the filter yourself: to the query of a root page before resolvePage, and in modify of r.page for a relation.

ts
import type { FilterValue } from "orchid-graphql"
import { filterQuery } from "orchid-graphql"

const resolvers = {
  Query: {
    posts: async (_parent, { filter }, context, info) => {
      return await graph.resolvePage(
        filterQuery(db.post, filter),
        r.cursor({ fields: ["-id"], take: 10 }),
        { context, info },
      )
    },
  },
}

const graph = r.graph({
  User: r.table(db.user, {
    fields: {
      id: true,
      name: true,
      posts: r.page(r.cursor({ fields: ["-id"], take: 10 }), {
        modify: (query, { tree }) => filterQuery(query, tree.args.filter as FilterValue),
      }),
    },
  }),
  Post: r.table(db.post),
})