Sqlmancer supports offset pagination using the
When using the client inside a resolver, you can use the
resolveInfo method to avoid calling the
offset methods altogether. Sqlmancer will look for arguments named
offset and modify your query builder appropriately.
Instead of adding those arguments directly to your field, you may also utilize the
You can also use the
@many directive instead of applying the
@offset directives separately.
Page types and
Instead of just returning a list of a particular type, your schema can return a "Page" object instead that includes these three fields:
results: a list of the paginated results
hasMore: a boolean indicating whether there are more results to be fetched
aggregate: an object for aggregating the values of the results
We can use the
@paginate directive to generate the appropriate "Page" type for a particular model:
which will be the equivalent of doing something like this:
Each model provides a matching
paginate method that can be used to populate such a field:
paginate method will return an empty object by default. Calling
hasMore will add the
hasMore field to the result object. Calling any of the aggregate methods, like
count will add the aggregate field to the result object. Calling any of the select or load methods will add the
results field to the result object.
PaginateBuilder returned by the
paginate method includes a
resolveInfo method for easy use inside a resolver:
paginate method can be used for either paginating over a large set of results, or for aggregating field values for a particular set of records. The same methods for sorting and filtering are also available on the
PaginateBuilder. You can read about aggregation in more depth here.
While Sqlmancer doesn't explicitly support cursor-based pagination, you can still utilize keyset pagination. Keyset pagination is often more performant than offset-based pagination, particularly on large data sets. The field used as the cursor needs to be sortable and unique. For example, you can use the primary key field
If you want to sort the results by a field that isn't necessarily unique, then you can combine that field with another field that is unique, like the primary key