- Cursor-based pagination overview
- The request parameters
- The PageInfo response object
- GraphQL API pagination
Cursor-based pagination overview
Cursor-based pagination works by requesting some number of items before or after a particular item (the cursor) in a collection. The API returns the fetched items and a new pair of cursors pointing to the first and last items of the results. Your application can then use these new cursors in subsequent requests to page forward or backward through the items, always fetching the next page before or the next item after the first and last items in the current page, respectively.Key terminology
- Your application - The web or mobile application you are building.
- Cursor - A pointer to an individual item in a collection.
- Your application makes an initial request with a parameter indicating the number of items it wants per page, 10, for example.
- The Propel API responds with the items if they exist and a pagination object (PageInfo) that contains a cursor to the first and last item of the results and whether a next or previous page exists.
- To see the next items, your application makes another API request indicating the number of items to fetch and the cursor to the next page.
- The Propel API fetches the next 10 items after the cursor and returns them to your application.
The request parameters
The Propel API expects the following parameters when making requests that fetch collections of items in list-style queries in GraphQL (dataSources, dataPools, metrics, and applications).
To page forward, the first and after parameters are expected:
| Parameter | Description | Default | Maximun | Required | 
|---|---|---|---|---|
| first | Indicates the number of items to be returned. | 10 | 100 | No | 
| after | The cursor that indicates the position where to start fetching items forward. The result will not include the item in the aftercursor. | most recent | — | No | 
last and before parameters are expected:
| Parameter | Description | Default | Maximun | Required | 
|---|---|---|---|---|
| last | Indicates the number of items to be returned. | 10 | 100 | No | 
| before | The cursor that indicates the position where to start fetching items backward. The result will not include the item in the beforecursor. | most recent | — | No | 
- If no parameters are provided, the most recent 10 items will be returned.
- If all parameters are provided, a 400 Bad Request is returned.
- If only firstis provided, the most recent specified number of items will be returned.
- If only lastis provided, an empty list is returned.
- If only afterorbeforeare provided, the default count of items before or after the cursor will be returned.
- If both afterandbeforeare provided, a 400 Bad Request is returned.
- If only firstandbeforeare provided, a 400 Bad Request is returned.
- If only lastandafterare provided, a 400 Bad Request is returned.
The PageInfo response object
Every API response to requests fetching collections of items like list queries in GraphQL (dataSources, dataPools, metrics, and applications) will return a PageInfo object.
The PageInfo object has the following properties:
- startCursor - Points to the first item returned in the results. Used when paginating backward.
- endCursor - Points to the last item returned in the results. Used when paginating forward.
- hasNextPage - A boolean that indicates whether a next page exist. Used to know whether to display a “Next page” UI button.
- hasPreviousPage - A boolean that indicates whether a previous page exist. Used to know whether to display a “Previous page” UI button.
A note about cursors
Cursors are opaque strings that contain the necessary information to locate the next (or previous) page of results. Your application doesn’t need to know the format of the cursor — just pass the cursor string along to Propel, and we’ll figure out the rest.GraphQL API pagination
Propel’s GraphQL cursor-based pagination is based on the Relay Cursor Connections Specification. Apollo has a great in-depth post, “Explaining GraphQL Connections”, that we recommend if the concept of GraphQL connections is new to you. The key thing to note is that, in addition to the parameters andpageInfo response object described above, GraphQL cursor-based pagination makes use of Connections and Edges.
- 
Connections - A wrapper object that contains the details of the list you are paging through. A connection has two properties: edgesandpageInfo. The propertypageInfois an instance of thePageInfoobject described above.
- Edge - A wrapper around an individual item in the result set that includes the item and a cursor that points to it.
cursor value when returning an object and the pageInfo object when returning the list without changing the base objects.
Let’s look at an example of how these objects look like in the Metrics GraphQL Schema.
A Metric connection has the pageInfo object and a MetricEdge array.
cursor and the actual Metric object.
getAllMetrics query would be called with either first and after or last and before and would return a MetricsConnection.
Example getAllMetrics request
This example illustrates how to fetch the 50 most recently created Metrics across two pages of 25 Metrics each. First we fetch the 25 most recently created Metrics, settingfirst to 25 in our query. This will return the first page of results. Make sure to request pageInfo and edges to be included in the response.
first to 25 and after to the value of the endCursor we got in the MetricsConnection response. This will return the second page of results.