Pagination Strategies
Relevant source files
The following files were used as context for generating this wiki page:
- packages/query-graphql/tests/types/query/none-query-args.type.spec.ts
- packages/query-graphql/tests/types/query/cursor-query-args.type.spec.ts
- packages/query-graphql/tests/types/query/offset-query-args.type.spec.ts
- packages/query-graphql/tests/types/query/paging.type.spec.ts
- examples/all-paging/e2e/todo-item.resolver.spec.ts
- examples/all-paging/e2e/sub-task.resolver.spec.ts
Pagination strategies in nestjs-query define how the system segments large datasets for GraphQL queries. The library provides three core strategies: Cursor-based, Offset-based, and None. You configure these strategies via the QueryArgsType factory or resolver decorators to control how clients request data and how the API returns result sets.
Sources: packages/query-graphql/tests/types/query/cursor-query-args.type.spec.ts:80, examples/all-paging/e2e/todo-item.resolver.spec.ts:133
Core Pagination Types
The following diagram illustrates the relationship between the QueryArgsType and the specific paging types generated based on the chosen strategy.
The strategy choice determines which paging input object is available in the GraphQL schema.
Sources: packages/query-graphql/tests/types/query/paging.type.spec.ts, packages/query-graphql/tests/types/query/cursor-query-args.type.spec.ts:106-111
Cursor Pagination Strategy
Cursor pagination implements the Relay Connection specification. It uses stable opaque tokens (cursors) to navigate datasets, making it efficient for frequently changing data.
Request Arguments
first: Specifies the number of items to return from the start of the set.after: A cursor identifying the point after which items should be returned.last: Specifies the number of items to return from the end of the set.before: A cursor identifying the point before which items should be returned.
Sources: packages/query-graphql/tests/types/query/cursor-query-args.type.spec.ts:100-105, packages/query-graphql/tests/types/query/paging.type.spec.ts:31-35
Validation Rules
The cursor strategy enforces strict logical constraints on arguments:
| Constraint | Description |
|---|---|
after requires first | You cannot use after without specifying a first count. |
before requires last | You cannot use before without specifying a last count. |
No first with before | Mixing forward and backward pagination arguments is prohibited. |
No last with after | Mixing backward and forward pagination arguments is prohibited. |
Sources: packages/query-graphql/tests/types/query/paging.type.spec.ts:31-125
Offset Pagination Strategy
The Offset strategy segments data using a simple numeric skip and limit approach. It is suitable for applications requiring direct page jumps (e.g., "Page 5").
Request Arguments
limit: The maximum number of records to return.offset: The number of records to skip before starting to return results.
Sources: packages/query-graphql/tests/types/query/offset-query-args.type.spec.ts:105-110
Response Structure
Unlike Cursor pagination which returns edges, the Offset strategy returns a flat array of nodes alongside basic pageInfo.
The pageInfo object in Offset pagination focuses on page existence rather than cursor tokens.
Sources: examples/all-paging/e2e/todo-item.resolver.spec.ts:192-205
No Pagination Strategy
The NONE strategy disables the paging argument entirely. Queries using this strategy return results as a standard GraphQL list.
- Schema behavior: The
pagingargument is removed from the generated GraphQLQuery. - Filtering and Sorting: Both remains available even when pagination is disabled.
- Default Sizes: The system still respects
defaultResultSizeandmaxResultsSizeto prevent accidental massive fetches.
Sources: packages/query-graphql/tests/types/query/none-query-args.type.spec.ts:111-135, examples/all-paging/e2e/todo-item.resolver.spec.ts:133-145
Global Configuration Options
You can configure limits and defaults regardless of the strategy used (excluding NONE for argument presence).
| Option | Type | Description |
|---|---|---|
defaultResultSize | number | Sets the item count if the client provides no paging argument. |
maxResultsSize | number | Enforces a hard limit on the number of items a client can request. |
maxResultsSize: -1 | number | Disables the maximum result size validation. |
Sources: packages/query-graphql/tests/types/query/cursor-query-args.type.spec.ts:133-176, packages/query-graphql/tests/types/query/offset-query-args.type.spec.ts:130-145
Result Size Validation
When maxResultsSize is defined, the system validates the input at runtime.
Implementation in Resolvers
The following snippet demonstrates how to configure these strategies using the QueryArgsType factory within an @ArgsType class.
// Cursor Strategy
@ArgsType()
class TodoItemCursorQuery extends QueryArgsType(TodoItemDTO, {
pagingStrategy: PagingStrategies.CURSOR
}) {}
// Offset Strategy
@ArgsType()
class TodoItemOffsetQuery extends QueryArgsType(TodoItemDTO, {
pagingStrategy: PagingStrategies.OFFSET
}) {}
Sources: packages/query-graphql/tests/types/query/cursor-query-args.type.spec.ts:80-81, packages/query-graphql/tests/types/query/offset-query-args.type.spec.ts:114-118