Pagination
This document explains the approach to implement pagination in GraphQL queries, specifically for listing resources such as funnels. The pagination is based on cursor-based pagination principles.
Pagination Query Structure
A GraphQL query for pagination includes parameters for controlling the number of items (first), and the point to start fetching items (after). Below is a template for a pagination query:
{
"query": "query funnelsQuery(
$first: Int
$after: String
$query: String!
) {
...funnelsPagination
}
fragment funnelsPagination on Query {
pagination: funnels(first: $first, after: $after, query: $query) {
edges {
node {
id
name
slug
published
created_at
updated_at
share_code
destinationTemplate {
uid: id
id
}
currency
__typename
}
cursor
}
pageInfo {
endCursor
hasNextPage
}
}
}
",
"variables": {
"query": "order_by:id order_dir:desc",
"first": 25
}
}
Query Parameters
first: The number of records to fetch.after: A cursor, indicating the starting point for fetching records.query: A query string to filter or sort the data.
Response Structure
edges: An array of edge objects, each representing an item.node: The actual item with its fields.cursor: A unique identifier for the item, used for pagination.
pageInfo: Contains pagination information.endCursor: The cursor of the last item in the response.hasNextPage: A boolean indicating if there are more items to fetch.
Implementing Pagination
Fetching Initial Data
- Set the
firstparameter to define how many items you want to load initially. - Leave the
afterparameter empty to start from the beginning.
Fetching More Data
- Use the
endCursorfrom thepageInfoas theaftervalue in the next query. - Check
hasNextPagebefore making the next query to ensure there are more items to fetch.
Example Usage
Query for First Page
{
"query": "...",
"variables": {
"query": "order_by:id order_dir:desc",
"first": 25
}
}
Query for Subsequent Pages
To fetch subsequent pages, you will need to use the endCursor from the previous query's response. Here's how the query should be structured:
{
"query": "...",
"variables": {
"query": "order_by:id order_dir:desc",
"first": 25,
"after": "[endCursor from previous response]"
}
}
In this query:
- The
firstparameter remains the same, indicating the number of items you want to fetch. - The
afterparameter is set to theendCursorfrom the previous page'spageInfo. This tells the server to start fetching results from the next item after the given cursor.
Continuation Logic
When implementing pagination:
- Check
hasNextPage: Before performing a subsequent query, always check thehasNextPageboolean from thepageInfoof the current response. IfhasNextPageis false, there are no more items to fetch. - Update
afterParameter: IfhasNextPageis true, use theendCursoras theafterparameter in your next query. - Repeat Process: Continue this process to navigate through the pages of data.
Best Practices
- Caching: Consider caching the
endCursorof each page for a smoother backward navigation experience. - Error Handling: Implement error handling for scenarios where the
endCursormight be invalid or expired. - Performance Considerations: Monitor the performance impact of large
firstvalues, as fetching too many items at once can impact server performance.
Conclusion
Cursor-based pagination in GraphQL is an effective way to handle large datasets. By following the outlined approach and best practices, developers can implement a robust and efficient pagination system in their GraphQL APIs.