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 defaultnestjs-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.
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 theoffset 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 bycompleted a comparision on id would
no longer be sufficient
[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 multipleTodoItems 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
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.- GraphQL
- Response
pageInfo from the previous example
hasNextPage is true and there is an endCursor that can be used to fetch the next page.
- GraphQL
- Response
before and last. In the following example we’ll use the startCursor from the
previous example and set last to 2.
- GraphQL
- Response
Offset Based Paging
An alternative to cursor based querying is to use theOFFSET 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
- GraphQL
- Response
- GraphQL
- Response
Disabling Paging
When using theNONE 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
- GraphQL
- Response