Pagination
To implement pagination we make use of the pageInfo: PageInfo property:
type PageInfo {
hasNextPage: Boolean!
hasPreviousPage: Boolean!
startCursor: String
endCursor: String
}
Where hasNextPage and hasPreviousPage will tell us wether there is a next or previous page or we hit the last or first one respectively.
On the other hand, startCursor and endCursor will give us cursor values for the start and end of the collection retrieved. These can be used as the after or before parameter in subsequent queries.
Example - getting paginated projects
We run the following query:
- Query
- Response
- cURL
query firstPage {
me {
accounts(first: 1) {
nodes {
projects(first: 3) {
pageInfo {
hasNextPage
endCursor
}
nodes {
name
}
}
}
}
}
}
{
"data": {
"me": {
"accounts": {
"nodes": [
{
"projects": {
"pageInfo": {
"hasNextPage": true,
"endCursor": "Mw"
},
"nodes": [
{
"name": "Cool Project"
},
{
"name": "Deep Crawling"
},
{
"name": "Pro Ject"
}
]
}
}
]
}
}
}
}
curl -X POST -H "Content-Type: application/json" -H "apollographql-client-name: docs-example-client" -H "apollographql-client-version: 1.0.0" -H "x-auth-token: YOUR_API_SESSION_TOKEN" --data '{"query":"query firstPage { me { accounts(first: 1) { nodes { projects(first: 3) { pageInfo { hasNextPage endCursor } nodes { name } } } } } }"}' https://api.lumar.io/graphql
We can then get the second page with the query:
- Query
- Response
- cURL
query secondPage {
me {
accounts(first: 1) {
nodes {
projects(first: 3, after: "Mw") {
pageInfo {
hasNextPage
endCursor
}
nodes {
name
}
}
}
}
}
}
{
"data": {
"me": {
"accounts": {
"nodes": [
{
"projects": {
"pageInfo": {
"hasNextPage": true,
"endCursor": "Ng"
},
"nodes": [
{
"name": "Dev.io: Main"
},
{
"name": "Dev.io: Blog"
},
{
"name": "Some Project"
}
]
}
}
]
}
}
}
}
curl -X POST -H "Content-Type: application/json" -H "apollographql-client-name: docs-example-client" -H "apollographql-client-version: 1.0.0" -H "x-auth-token: YOUR_API_SESSION_TOKEN" --data '{"query":"query secondPage { me { accounts(first: 1) { nodes { projects(first: 3, after: \"Mw\") { pageInfo { hasNextPage endCursor } nodes { name } } } } } }"}' https://api.lumar.io/graphql