Skip to main content

Paginate query results

There are multiple ways to obtain this behavior, let’s take a look at a couple of them.

Cursor based pagination

Cursors are used to traverse across entities of an entity set. They work by returning a pointer (“cursor”) to a specific entity which can then be used to fetch the next batch. The batch will start with the entity after the one the cursor points to. For cursor-based pagination, OpenReader follows the Relay Cursor Connections spec. Currently, only forward pagination is supported. If your use case requires bidirectional pagination please let us know at our Telegram channel. In SQD GraphQL server, cursor based pagination is implemented with {entityName}sConnection queries available for every entity in the input schema. These queries require an explicitly supplied orderBy argument. The ordered field does not have to appear in the selection set. Example: this query fetches a list of videos where isExplicit is true and gets their count.

Operator first

The first operator is used to fetch a specified number of entities from the beginning of the output. Example: Fetch the first 5 videos:

PageInfo object

PageInfo is a “virtual” entity that can be requested from any {entityName}sConnection query (see below). It returns the relevant cursors and some page information:

Operator after

Example: Fetch the first 10 channels, ordered by createdAt. Then, in a second query, fetch the next 10 channels:
When first is omitted, a connection returns at most 100 nodes. OpenReader supports forward pagination with first and after; it does not expose last or before arguments.

Examples

An interactive example of using cursor-based pagination can be found in this repo.

Paginating with {entityName}s queries

Arguments limit and offset

In a list of entities returned by a query, the limit argument specifies how many should be retained, while the offset argument specifies how many should be skipped first. offset defaults to 0. There is no default limit: a plain list query without one is unbounded, so production clients should always provide a limit or use a connection query.

Limit results

Example: Fetch the first 5 channels:

Limit results from an offset

Example: Fetch 5 channels from the list of all channels, starting with the 6th one: