Skip to main content

Pagination

The AIRICA API supports a cursor based pagination to paginate through a list of results. Next to the data it provides all necessary information about the connection, next and previous pages. It implements GraphQL Connections and follows the formal specification of the relay project.

Pagination parameters#

Each list accepts 4 different parameters:

ParameterDescription
 after (String) Returns the elements in the list that come after the specified cursor. Can only be used together with 'first'
 before (String)Returns the elements in the list that come before the specified cursor. Can only be used together with 'last'
first (Int)Returns the first n elements from the list
last (Int)Returns the last n elements from the list 

Example#

The field buildings on the client object returns a list of buildings. We have the following buildings in our account:

Building IDBuilding Name
d34ee8d9-bd09-4e7c-b402-d031e02c7c1eBuilding 1
09f5bd6e-b60a-4405-bfed-a2a228dca87cBuilding 2
5c8366df-a534-47c8-8e0e-e53341fc7274Building 3
c26eaad6-fc2e-43b1-b5a0-46682c8193cfBuilding 4
67b60ade-349d-4158-a5db-645c81e9f6b4Building 5

Retrieve first three buildings#

We can use the following query to retrieve a list of the first three buildings:

Request#

query {  client {    buildings(first: 3) {      edges {        node {          name        }      }    }  }}

Response#

{  "data": {    "client": {      "buildings": {        "edges": [          {            "node": {              "id": "d34ee8d9-bd09-4e7c-b402-d031e02c7c1e",              "name": "Building 1"            }          },          {            "node": {              "id": "09f5bd6e-b60a-4405-bfed-a2a228dca87c",              "name": "Building 2"            }          },          {            "node": {              "id": "5c8366df-a534-47c8-8e0e-e53341fc7274",              "name": "Building 3"            }          }        ]      }    }  }}

Retrieve first three buildings with paging information#

The above request does not help us to paginate as we did not request any cursors. We can add the field pageinfo to the query. Additionally we can request the cursor on every edge.

Request#

query {  client {    buildings(first: 3) {      edges {        cursor        node {          id          name        }      }      pageInfo {        hasNextPage        hasPreviousPage        startCursor        endCursor      }    }  }}

Response#

The response returns hasNextPage: true which indicates that there are more list entries.

{  "data": {    "client": {      "buildings": {        "edges": [          {            "cursor": "eyJpZCI6ImQzNGVlOGQ5LWJkMDktNGU3Yy1iNDAyLWQwMzFlMDJjN2MxZSIsInZhbHVlIjoiQnVpbGRpbmcgMSJ9",            "node": {              "id": "d34ee8d9-bd09-4e7c-b402-d031e02c7c1e",              "name": "Building 1"            }          },          {            "cursor": "eyJpZCI6IjA5ZjViZDZlLWI2MGEtNDQwNS1iZmVkLWEyYTIyOGRjYTg3YyIsInZhbHVlIjoiQnVpbGRpbmcgMiJ9",            "node": {              "id": "09f5bd6e-b60a-4405-bfed-a2a228dca87c",              "name": "Building 2"            }          },          {            "cursor": "eyJpZCI6IjVjODM2NmRmLWE1MzQtNDdjOC04ZTBlLWU1MzM0MWZjNzI3NCIsInZhbHVlIjoiQnVpbGRpbmcgMyJ9",            "node": {              "id": "5c8366df-a534-47c8-8e0e-e53341fc7274",              "name": "Building 3"            }          }        ],        "pageInfo": {          "hasNextPage": true,          "hasPreviousPage": false,          "startCursor": "eyJpZCI6ImQzNGVlOGQ5LWJkMDktNGU3Yy1iNDAyLWQwMzFlMDJjN2MxZSIsInZhbHVlIjoiQnVpbGRpbmcgMSJ9",          "endCursor": "eyJpZCI6IjVjODM2NmRmLWE1MzQtNDdjOC04ZTBlLWU1MzM0MWZjNzI3NCIsInZhbHVlIjoiQnVpbGRpbmcgMyJ9"        }      }    }  }}

Retrieve next buildings#

To retrieve the next two buildings, we must use the endCursor from the previous query. We can do this by setting the after parameter with the value from endCursor:

#### Request

query {  client {    buildings(      first: 3      after: "eyJpZCI6IjVjODM2NmRmLWE1MzQtNDdjOC04ZTBlLWU1MzM0MWZjNzI3NCIsInZhbHVlIjoiQnVpbGRpbmcgMyJ9"    ) {      edges {        cursor        node {          id          name        }      }      pageInfo {        hasNextPage        hasPreviousPage        startCursor        endCursor      }    }  }}

Response#

The response returns all entries starting after "Building 3" (in our example 4 + 5). Please note that hasNextPage is set to false now as there are no more results

{  "data": {    "client": {      "buildings": {        "edges": [          {            "cursor": "eyJpZCI6ImMyNmVhYWQ2LWZjMmUtNDNiMS1iNWEwLTQ2NjgyYzgxOTNjZiIsInZhbHVlIjoiQnVpbGRpbmcgNCJ9",            "node": {              "id": "c26eaad6-fc2e-43b1-b5a0-46682c8193cf",              "name": "Building 4"            }          },          {            "cursor": "eyJpZCI6IjY3YjYwYWRlLTM0OWQtNDE1OC1hNWRiLTY0NWM4MWU5ZjZiNCIsInZhbHVlIjoiQnVpbGRpbmcgNSJ9",            "node": {              "id": "67b60ade-349d-4158-a5db-645c81e9f6b4",              "name": "Building 5"            }          }        ],        "pageInfo": {          "hasNextPage": false,          "hasPreviousPage": true,          "startCursor": "eyJpZCI6ImMyNmVhYWQ2LWZjMmUtNDNiMS1iNWEwLTQ2NjgyYzgxOTNjZiIsInZhbHVlIjoiQnVpbGRpbmcgNCJ9",          "endCursor": "eyJpZCI6IjY3YjYwYWRlLTM0OWQtNDE1OC1hNWRiLTY0NWM4MWU5ZjZiNCIsInZhbHVlIjoiQnVpbGRpbmcgNSJ9"        }      }    }  }}

Reverse Paging#

AIRICA supports backwards pagination by using last and before. We can modify our query to retrieve the last two buildings:

Request#

query {  client {    buildings(last: 2) {      edges {        cursor        node {          id          name        }      }      pageInfo {        hasNextPage        hasPreviousPage        startCursor        endCursor      }    }  }}

Response#

{  "data": {    "client": {      "buildings": {        "edges": [          {            "cursor": "eyJpZCI6ImMyNmVhYWQ2LWZjMmUtNDNiMS1iNWEwLTQ2NjgyYzgxOTNjZiIsInZhbHVlIjoiQnVpbGRpbmcgNCJ9",            "node": {              "id": "c26eaad6-fc2e-43b1-b5a0-46682c8193cf",              "name": "Building 4"            }          },          {            "cursor": "eyJpZCI6IjY3YjYwYWRlLTM0OWQtNDE1OC1hNWRiLTY0NWM4MWU5ZjZiNCIsInZhbHVlIjoiQnVpbGRpbmcgNSJ9",            "node": {              "id": "67b60ade-349d-4158-a5db-645c81e9f6b4",              "name": "Building 5"            }          }        ],        "pageInfo": {          "hasNextPage": false,          "hasPreviousPage": true,          "startCursor": "eyJpZCI6ImMyNmVhYWQ2LWZjMmUtNDNiMS1iNWEwLTQ2NjgyYzgxOTNjZiIsInZhbHVlIjoiQnVpbGRpbmcgNCJ9",          "endCursor": "eyJpZCI6IjY3YjYwYWRlLTM0OWQtNDE1OC1hNWRiLTY0NWM4MWU5ZjZiNCIsInZhbHVlIjoiQnVpbGRpbmcgNSJ9"        }      }    }  }}

Reverse Paging with cursor#

To retrieve the next two buildings we can set the parameter before to the value of startCursor.

Request#

query {  client {    buildings(      last: 2      before: "eyJpZCI6ImMyNmVhYWQ2LWZjMmUtNDNiMS1iNWEwLTQ2NjgyYzgxOTNjZiIsInZhbHVlIjoiQnVpbGRpbmcgNCJ9"    ) {      edges {        cursor        node {          id          name        }      }      pageInfo {        hasNextPage        hasPreviousPage        startCursor        endCursor      }    }  }}

#### Response

{  "data": {    "client": {      "buildings": {        "edges": [          {            "cursor": "eyJpZCI6IjA5ZjViZDZlLWI2MGEtNDQwNS1iZmVkLWEyYTIyOGRjYTg3YyIsInZhbHVlIjoiQnVpbGRpbmcgMiJ9",            "node": {              "id": "09f5bd6e-b60a-4405-bfed-a2a228dca87c",              "name": "Building 2"            }          },          {            "cursor": "eyJpZCI6IjVjODM2NmRmLWE1MzQtNDdjOC04ZTBlLWU1MzM0MWZjNzI3NCIsInZhbHVlIjoiQnVpbGRpbmcgMyJ9",            "node": {              "id": "5c8366df-a534-47c8-8e0e-e53341fc7274",              "name": "Building 3"            }          }        ],        "pageInfo": {          "hasNextPage": true,          "hasPreviousPage": true,          "startCursor": "eyJpZCI6IjA5ZjViZDZlLWI2MGEtNDQwNS1iZmVkLWEyYTIyOGRjYTg3YyIsInZhbHVlIjoiQnVpbGRpbmcgMiJ9",          "endCursor": "eyJpZCI6IjVjODM2NmRmLWE1MzQtNDdjOC04ZTBlLWU1MzM0MWZjNzI3NCIsInZhbHVlIjoiQnVpbGRpbmcgMyJ9"        }      }    }  }}