Skip to content

Pagination ​

The examples on this page use the UserModel and PostModel models 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: (_parent, _args, context, info) => {
      return graph.resolvePage(PostModel.query(), 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
graph.resolvePage(PostModel.query().orderBy("id", "desc"), r.cursor({ take: 10 }), {
  context,
  info,
})

Order by columns that never hold NULL. 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: (_parent, { take }, context, info) => {
      if (take != null && (take < 1 || take > 100)) {
        throw new Error("take must be between 1 and 100.")
      }
      return graph.resolvePage(PostModel.query(), r.cursor({ fields: ["-id"], take: 10 }), {
        context,
        info,
      })
    },
  },
}

Currently broken

A query with take: 0 fails when the list has rows.

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.model(UserModel, {
    fields: {
      id: true,
      name: true,
      posts: r.page(r.cursor({ fields: ["-id"], take: 10 })),
    },
  }),
  Post: r.model(PostModel),
})

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" })

Currently broken

A paginated relation is correct only under a single parent, such as user(id: 1) { posts { nodes { id } } }: under a list of parents, such as users { posts { nodes { id } } }, every parent gets the same page.

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 "objection-graphql-resolver"
import { filterQuery } from "objection-graphql-resolver"

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

const graph = r.graph({
  User: r.model(UserModel, {
    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.model(PostModel),
})