Pagination in REST API with GraphQL
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-06-2020 04:18 PM - edited 26-10-2020 09:23 AM
When querying elements through the REST API a lot of items can be returned. To limit the size of items returned it can be relevant to paginate the data.
Pagination allows you to request a certain chunk of objects. You can seek forwards or backwards through the objects and supply an optional starting object:
- To seek forward, use first; specify a starting object with after.
- To seek backward, use last; specify a starting object with before.
To described the pagination we will take an example of a list 30 objects.
This sample query is on the Application object. With no pagination :
query application {
application {
id
name
}
}
Caution :
As soon as a pagination is applied an implicit sort by "ID" is executed, therefore a query withtout pagination and order will not be sorted as a query with pagination.
First elements with Skip or After
You can get the first element and skip an arbitrary amount of objects in forward direction you are seeking by supplying the first, skip and after.
Five possibility of queries :
- First : will take the number of element given in first starting form the first item
query application { application(first:3) { id name } }
- First with Skip : will take the number of element given in first skipping the numbered of skipped elements
query application { application(first:3 skip:5) { id name } }
- First with After : will take the number of element given in first starting after the given object ID
query application { application(first:3 after:"exeiHMRhHjHI" ) { id name } }
- Fist with After and Skip : will take the number of element given in first starting after the given object ID and skipping the numbered of skipped elements
query application { application(first:3 skip:5 after:"exeiHMRhHjHI" ) { id name } }
- After alone : will start after the given object ID and take all the remaining objects
query application { application( after:"exeiHMRhHjHI" ) { id name } }
Last elements with Skip and Before
You can get the last element and skip an arbitrary amount of objects in backward direction you are seeking by supplying the first, skip and before.
Five possibility of queries :
- Last : will take the number of element given in last starting form the end of the item
query application { application(last:3 ) { id name } }
- Last and Skip : will take the number of element given in last starting form the end of the item skipping the number of object in skip
query application { application(last:3 skip:5 ) { id name } }
- Last and Before : will take the number of element given in last starting form the given id object
query application { application(last:3 before:"ZEQyZ7FqOnxL") { id name } }
- Last, Skip and Before : will take the number of element given in last starting form the given id object skipping the number of object in skip
query application { application(last:3 skip:5 before:"ZEQyZ7FqOnxL") { id name } }
- Before Only : will take all the object before the given object
query application { application(before:"ZEQyZ7FqOnxL") { id name } }
Important
You cannot combine first with before or last with after. If you do so in a query, before or after will simply be ignored and only first or last is applied (at the very beginning or end of the list, depending on which you're using).
Note that you can query for more nodes than exist without an error message.
Pagination and Sort
If you combined pagination option and order capabilities the pagination will be done after the sort is executed.
Example of query :
query application {
application(orderBy:[cloudComputing_ASC] first:3) {
id
name
cloudComputing
}
}
