Pagination
What is Pagination?
entrest supports pagination for all list operations by default. Pagination at a high-level ensures that as the data returned by your API grows, there is a linear request size and query time/complexity, and you can better enforce rate limits for querying your API. If you cannot be confident that the number of results will not grow beyond a reasonable size, you should keep pagination enabled for any schemas where that may apply.
Configuration
Pagination has a few configuration options in a few locations:
- Turning pagination on/off.
- Globally: with the
DisablePagination
config option. - Per-schema: with the
WithPagination
annotation.
- Globally: with the
- Adjusting the default, minimum and maximum number of results per page.
- Globally: with the
ItemsPerPage
,MinItemsPerPage
, andMaxItemsPerPage
config options. - Per-schema: with the
WithItemsPerPage
,WithMinItemsPerPage
, andWithMaxItemsPerPage
annotations.
- Globally: with the
Example of querying a paginated endpoint
Using our example API, and some of the example queries,
we have the /users/{id}/pets
endpoint which returns a list of pets associated with the user. Since the
user could have many pets (who knows, maybe they run a cat cafe), this is a perfect use case for pagination.
You will notice that we provided the page
parameter (not required for the first page), and the
per_page
parameter (to control how many results we want per page). per_page
will be limited to
the MinItemsPerPage
and MaxItemsPerPage
values, and they have relatively sane defaults.
As we only requested 5
results, and there are a total of 124
results as shown in the total_count
field, we can see that there are 25
pages in total. You can use the last_page
or is_last_page
fields to determine if we are on the last page, or if there are more pages to fetch.
Python example
Below is an example of how you might achieve collecting all results from a paginated endpoint in Python:
Go example
Below is an example of how you might achieve collecting all results from a paginated endpoint in Go: