MODULES > REST API and GraphQL > Pagination in REST API with GraphQL
8. Pagination in REST API with GraphQL
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 forward or backward 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 describ the pagination here is an example of a list of 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 without pagination and order will not be sorted as a query with pagination.
8.1. 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:
1. First: will take the number of elements given in first starting form the first itemA row of white circles Description automatically generated
query application {
application(first:3) {
id
name
}
}​
2. First with Skip: will take the number of elements given in first skipping the numbered of skipped elementsA row of white circles Description automatically generated
query application {
application(first:3 skip:5) {
id
name
}
}​
3. First with After: will take the number of elements given in first starting after the given object IDA screenshot of a computer Description automatically generated
query application {
application(first:3 after:"exeiHMRhHjHI" ) {
id
name
}
}​
4. Fist with After and Skip: will take the number of elements given in first starting after the given object ID and skipping the numbered of skipped elementsA number of dots with text Description automatically generated
query application {
application(first:3 skip:5 after:"exeiHMRhHjHI" ) {
id
name
}
}​
5. After alone: will start after the given object ID and take all the remaining objectsA row of green dots Description automatically generated
query application {
application( after:"exeiHMRhHjHI" ) {
id
name
}
}​
 
8.2. 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:
1. Last: will take the number of element given in last starting form the end of the itemA row of white circles Description automatically generated
query application {
application(last:3 ) {
id
name
}
}​
2. 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 skipA row of white dots Description automatically generated
query application {
application(last:3 skip:5 ) {
id
name
}
}​
3. Last and Before: will take the number of element given in last starting form the given id objectA row of circles with red green and white dots Description automatically generated
query application {
application(last:3 before:"ZEQyZ7FqOnxL") {
id
name
}
}​
4. 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 skipA number of circles with red and green dots Description automatically generated
query application {
application(last:3 skip:5 before:"ZEQyZ7FqOnxL") {
id
name
}
}​
5. Before Only: will take all the object before the given objectA row of green circles Description automatically generated
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.
8.3. Pagination and Sort
If you combine 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
}
}