Pagination

Pagination in Swan's APIs follows Relay's "Connections" Specs. Here is a quick intro that will let you use pagination unhindered. If you want to know more, we suggest you read the specifications.

Take for example the accountMembership query. Imagine you want to know the first ten account you are attached to. Submit the query as follows:

Request

{
accountMemberships(first: 10) {
edges {
node {
account {
number
}
}
cursor
}
}
}

Response

{
"accountMemberships":{
"edges":[
{
"node":{
"account":{
"number":"00100260169"
}
},
"cursor":"Y3Vyc29yMg=="
},
{
"node":{
"account":{
"number":"00100260168"
}
},
"cursor":"Y3Vyc29xMg=="
}
]
}
}

Even though reading this request seems pretty easy, you are already manipulating an accountMemberships 's Connection. A connection takes four optional forms:

  • first : the number of elements to load.

  • after : the index (a unique reference in string form) from which you will load the following elements.

  • before : the index (a unique reference in string form) from which you will load the preceding elements.

  • filters : a filtering table you can apply to your list of connections.

A connection contains a list ofedges. Each edge contains a node that corresponds to an element. If you have at least ten accounts this request will return ten edges each one containing a node, which itself contains an AccountMembership.

In addition to containing the node, theedge also contains a very important cursor attribute. This attribute serves as an opaque index for the list you are currently browsing. Therefore, if you wish to load the ten following AccountMembership, you just have to call the last cursor and call the request back:

Request

{
accountMemberships(first: 10, after:"Y3Vyc29xMg==") {
edges {
node {
account {
number
}
}
cursor
}
}
}

Finally, a connection contains a totalCount attribute and a pageInfosobject. The first seems pretty straightforward; it provides the total number of elements in the list. The second will allow you to recuperate a bunch of information on the current and neighbouring pages:

Request

{
accountMemberships(first: 2, after:"lastCursorOfPreviousCall") {
totalCount
edges {
node {
account {
number
}
}
cursor
}
pageInfo {
endCursor
hasNextPage
}
}
}

Response

{
"accountMemberships":{
"totalCount": 15,
"edges":[
{
"node":{
"account":{
"number":"00100260169"
}
},
"cursor":"X3Vyc29yMg=="
},
{
"node":{
"account":{
"number":"00100260168"
}
},
"cursor":"B3Vyc29xMg=="
}
],
"pageInfo": {
"endCursor": "Y3Vyc29xMg==",
"hasNextPage": false
}
}
}