âī¸ Basicsđ PaginationLearn how to use cursor pagination in the Airstack SDK using special and powerful pagination functions and variables offered by the Airstack SDK out of the box.
With the Airstack SDKs, you will not need to manually manage the cursor of each page yourself as they provide special pagination functions and variables that can help you simplify the process:
React/Node
Python
Description
Boolean indicating if the next page exists
Boolean indicating if the previous page exists
Get response data and error on the next page
Get response data and error on the previous page
If you are using the SDK for paginating response data, pageInfo.nextCursor
and pageInfo.prevCursor
will not be necessarily added to your query as it will be added automatically by the SDK.
Thus, you can just provide a query without any of the cursor field in your schema.
Pre-requisites
Install Airstack SDK
If you are using JavaScript/TypeScript or Python, Install the Airstack SDK:
npm yarn pnpm pip
React
Copy npm install @airstack/airstack-react
Node
Copy npm install @airstack/node
React
Copy yarn add @airstack/airstack-react
Node
Copy yarn add @airstack/node
React
Copy pnpm install @airstack/airstack-react
Node
Copy pnpm install @airstack/node
Example
Here is sample implementation of using the special functions and variables mentioned above for paginating through all the data returned by the API:
React Node Python
Copy import { init , useQueryWithPagination } from "@airstack/airstack-react" ;
init ( "YOUR_AIRSTACK_API_KEY" );
const query = `
query MyQuery {
TokenBalances(
input: {filter: {tokenAddress: {_eq: "0xBC4CA0EdA7647A8aB7C2061c2E118A18a936f13D"}}, blockchain: ethereum, limit: 200}
) {
TokenBalance {
owner {
identity
}
}
}
}
` ;
const Component = () => {
const { data , pagination } = useQueryWithPagination (query);
const {
hasNextPage , // Boolean â indicate if there's next page
hasPrevPage , // Boolean - indicate if there's prev page
getNextPage ,
getPrevPage
} = pagination;
/**
* @description change cursor to the next page, this will
* immediately be reflected on `data`
*/
const handleNextPage = () => {
getNextPage ();
};
/**
* @description change cursor to the prev page, this will
* immediately be reflected on `data`
*/
const handlePrevPage = () => {
getPrevPage ();
};
return (
// your JSX component
)
}
export default Component;
Copy import { init , fetchQueryWithPagination } from "@airstack/node" ;
init ( "YOUR_AIRSTACK_API_KEY" );
const query = `
query MyQuery {
TokenBalances(
input: {filter: {tokenAddress: {_eq: "0xBC4CA0EdA7647A8aB7C2061c2E118A18a936f13D"}}, blockchain: ethereum, limit: 200}
) {
TokenBalance {
owner {
identity
}
}
}
}
` ;
// counting page indicator
let count = 0 ;
let response;
const main = async () => {
while ( true ) {
if ( ! response) {
response = await fetchQueryWithPagination (query);
}
const { data , error , hasNextPage , getNextPage } = response;
if ( ! error) {
/**
* Add more code to format the `response` data
*/
console .log ( `Data page ${ ++ count } : ` , data);
if ( ! hasNextPage) {
break ;
} else {
response = await getNextPage ();
}
} else {
console .error ( "Error: " , error);
break ;
}
}
};
main ();
Copy import asyncio
from airstack . execute_query import AirstackClient
api_client = AirstackClient (api_key = "YOUR_AIRSTACK_API_KEY" )
query = """
query MyQuery {
TokenBalances(
input: {filter: {tokenAddress: {_eq: "0xBC4CA0EdA7647A8aB7C2061c2E118A18a936f13D" }} , blockchain: ethereum, limit: 200}
) {
TokenBalance {
owner {
identity
}
}
}
}
"""
async def main ():
count = 0
execute_query_client = api_client . create_execute_query_object (
query = query)
query_response = await execute_query_client . execute_paginated_query ()
while True :
if query_response . error is None :
count += 1
print ( f "Data page { count } : " , query_response.data)
if not query_response . has_next_page :
break
else :
query_response = await query_response . get_next_page
else :
print ( "Error: " , query_response.error)
break
asyncio . run ( main ())
Developer Support
If you have any questions or need help regarding how to use cursor pagination with Airstack SDK, please join our Airstack's Telegram group.
More Resources
Last updated 8 months ago