Our GraphQL API supports pagination for efficiently handling large datasets, such as transaction histories. This guide explains how to use pagination in your queries.
Understanding Cursors and Pagination
We use cursor-based pagination, similar to the methods used by Facebook's Graph API and recommended by GraphQL. This approach allows you to fetch subsets of data based on cursor values, which represent positions in a dataset.
Pagination Fields
first (Int)
: Limits the number of items returned. Use it to specify how many results to fetch. For example, first: 10 will get the first 10 items.after (String)
: Fetches items after the provided cursor. Combine it with first to paginate forward.last (Int)
: Similar to first, but limits the number of items from the end of the list.before (String)
: Fetches items before the provided cursor. Use it with last to paginate backward.where (...)
: Filters the response based on specified criteria like asset, vault, etc.
Example Query: Transaction History
To retrieve a paginated list of transactions, you can use a query like this:
query {
transactionHistory(first: 10) {
edges {
node {
... on Transaction {
amount
parentTransactionId
}
cursor
}
}
}
}
Using Pagination
To navigate through your data, use the first and after parameters for forward pagination, and last and before for backward pagination. The after and before parameters take a cursor that specifies the starting point within the dataset.
For example, lets imagine the last item in the array returned from the previous response had a cursor of VHJhbnNhY3Rpb25SZXF1ZXN0Ojo6Y2xwbGFmdnh0MDAyZnk2M3d5Ym5uOGh4MQ
. I can now use that cursor to fetch the first 10 results after that item:
query {
transactionHistory(first: 10, after: "VHJhbnNhY3Rpb25SZXF1ZXN0Ojo6Y2xwbGFmdnh0MDAyZnk2M3d5Ym5uOGh4MQ==") {
edges {
node {
...
}
cursor
}
}
}
To continue crawling through the dataset, simply replace the cursor with a new cursor returned in the response. Since have set the 'first' field, we would take the cursor from the last item returned within the array of transactionHistory. To go backwards, use the 'last' field combined with the 'before' cursor.