Skip to main content
nestjs-query supports multiple paging strategies that each have their own pros and cons. This documentation will cover the different paging strategies and their applicable use cases. The following examples are based on the following TodoItemDTO
todo-item.dto.ts

Cursor Based Paging

By default nestjs-query will expose all query many endpoints as cursor based connections that you can use to page through results.
When using cursor based connections you are not tied to any particular implementation described below, because of the opaque nature of cursors you can start out with the default and switch to key set based cursors later on without changing your clients.
All cursor connections, regardless of paging strategy, expose the following schema

Offset Based Cursor

By default all cursors will use a form of offset based paging to back cursors.

Key Set Based Cursor

You have the option to specify a key set on your DTO which will replace the offset with a where clause. A keyset is a set of fields that uniquely identify a record (e.g. id). To enable key set based paging all you need to do is decorate your DTO with the @KeySet decorator.

Sorting and Key set cursors

When using key set based cursors we must take into account any client provided sorting in order to uniquely identify a record within the sorted set of nodes For example assume we’re using the same DTO as above. If we added a sort by completed a comparision on id would no longer be sufficient
If we only compared on the keyset [id] our pages would no longer be sorted properly, if we only compared on completed you would not be able to page. We solve this problem by encoding information about the each field in the sort as well as the key set fields into the cursor so we can page properly. In the above example the filter from the cursor (when paging forward) would be something like

Relation Connections

Key set paging will not apply to relations because they are recursive by nature. For example if you query for multiple TodoItems and their subTasks if key set paging was used for the subTasks connection the cursor from one todoItems subTasks may not be applicable to all todoItems For example, assume you have the following todo items and subTasks
If you ran the following graphql query
The resulting query would look for all subTasks with an id > 3 breaking paging for Todo 1, for this reason the @KeySet decorator is used for all relations.

Paging

To page with cursors it works the same way for all strategies. In this example we’ll fetch the first 2 records.
Lets take a look the pageInfo from the previous example
Notice how hasNextPage is true and there is an endCursor that can be used to fetch the next page.
We can also page backward by using before and last. In the following example we’ll use the startCursor from the previous example and set last to 2.

Offset Based Paging

An alternative to cursor based querying is to use the OFFSET pagingStrategy. When using the OFFSET strategy queries that return multiple records will return an OffsetConnection that looks like the following.
To enable OFFSET based paging all you need to do is set the pagingStrategy option using the @QueryOptions decorator.
todo-item.dto.ts
In this example we’ll fetch the first 2 records.
In this example we’ll also pass in an offset to fetch the next 2 records.

Disabling Paging

This strategy is only recommended if you are sure your dataset is small.
When using the NONE paging strategy the paging argument is removed and all methods will return an ArrayConnection.
To disable paging all you can set the pagingStrategy option using the @QueryOptions decorator to PagingStrategies.NONE.
todo-item.dto.ts
When using the strategy queries that return multiple records will return an array instead of a connection.