When an API call is able to return unbounded data, it is important to provide the data in smaller and manageable chunks, i.e. pages. For example, in our Planetary Party Planner API, if we request for all parties, it is impractical to provide the data for all parties at once. Instead, we may want to show the latest ten parties, and then provide an ability to request for the next ten. The number of items on a page (i.e. ten, in this example) and other details should be customizable.
This is called pagination — an essential component in any API. In this lesson, we learn how to build pagination in GraphQL APIs.
Pagination Strategies
There are two common pagination strategies: offset-based and cursor-based.
We assume that the data we are requesting is an ordered list. When requesting for paginated data, we need to mention how many items we want (often called limit
) and the position in that list to start from (often called offset
).
In an offset-based pagination, we may request for the first 4 items by mentioning limit = 4
and offset = 0
.