Pagination
GraphQL uses a cursor approach to pagination and this information can be included in the response to requests along side the edges. The response includes a bool for hasNextPage if there is more data to return and and endCursor value which can be passed into the query for the next page.
{
research(
first: 2
) {
edges {
node {
id
title
}
}
pageInfo {
hasNextPage
endCursor
}
}
}Returns:
{
"data": {
"research": {
"edges": [
{
"node": {
"id": "68b993fb85d14160d40303fd",
"title": "Follow up call"
}
},
{
"node": {
"id": "68b993fb85d14160d4030407",
"title": "Info Request - Klein-Kiehn"
}
}
],
"pageInfo": {
"hasNextPage": true,
"endCursor": "WyI2OGI5OTNmYjg1ZDE0MTYwZDQwMzA0MDciXQ"
}
}
}
}The endCursor value can then be used as an input for the after argument when requesting subsequent pages:
{
research(
first: 2
after: "WyI2OGI5OTNmYjg1ZDE0MTYwZDQwMzA0MDciXQ"
) {
edges {
node {
id
title
}
}
pageInfo {
hasNextPage
endCursor
}
}
}This approach can be used to iterate through an entire set of results; application code can work as follows:
- Execute an initial query with
firstset to the number of items per page, andafterset tonull. - Retrieve the value of
hasNextPagefrom thepageInfoobject on the connection. If it's false, there are no more results. If it's true: - Repeat the query from step 1, but this time pass the
endCursorvalue as theafterargument. Return to step 2 and repeat until there are no results left.
Updated 7 days ago
