cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Pagination in REST API with GraphQL

oguimard
Retired

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.
blank.png

 

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 :

  1. First : will take the number of element given in first starting form the first item first.png

     

    query application {
      application(first:3) {
        id 
        name
      }
    }​
  2. First with Skip : will take the number of element given in first skipping the numbered of skipped elementsfirstandskip.png
    query application {
      application(first:3 skip:5) {
        id 
        name
      }
    }​
  3. First with After : will take the number of element given in first starting after the given object IDfirstafter.png
    query application {
      application(first:3  after:"exeiHMRhHjHI" ) {
        id 
        name
      }
    }​
  4. 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 elementsfirstskipafter.png
    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 objectsafter.png
    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 :

  1. Last : will take the number of element given in last starting form the end of the itemlast.png
    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 skiplastskip.png
    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 objectlastbefore.png
    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 skiplastbeforeskip.png
    query application {
      application(last:3 skip:5 before:"ZEQyZ7FqOnxL") {
        id 
        name
      }
    }​
  5. Before Only : will take all the object before the given objectbefore.png
    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
  }
}

 

 

 

0 Replies