📑Pagination

Learn 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/NodePythonDescription

hasNextPage

has_next_page

Boolean indicating if the next page exists

hasPrevPage

has_prev_page

Boolean indicating if the previous page exists

getNextPage

get_next_page

Get response data and error on the next page

getPrevPage

get_prev_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:

React

npm install @airstack/airstack-react

Node

npm 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:

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;

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

Was this helpful?