Skip to content

Latest commit

 

History

History
260 lines (210 loc) · 6 KB

pagination-in-airstack-sdk.md

File metadata and controls

260 lines (210 loc) · 6 KB
description layout
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.
title description tableOfContents outline pagination
visible
true
visible
true
visible
true
visible
visible
true

📑 Pagination

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

{% hint style="info" %} 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. {% endhint %}

Pre-requisites

Install Airstack SDK

If you are using JavaScript/TypeScript or Python, Install the Airstack SDK:

{% tabs %} {% tab title="npm" %} React

npm install @airstack/airstack-react

Node

npm install @airstack/node

{% endtab %}

{% tab title="yarn" %} React

yarn add @airstack/airstack-react

Node

yarn add @airstack/node

{% endtab %}

{% tab title="pnpm" %} React

pnpm install @airstack/airstack-react

Node

pnpm install @airstack/node

{% endtab %}

{% tab title="pip" %}

pip install airstack

{% endtab %} {% endtabs %}

Example

Here is sample implementation of using the special functions and variables mentioned above for paginating through all the data returned by the API:

{% tabs %} {% tab title="React" %}

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;

{% endtab %}

{% tab title="Node" %}

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();

{% endtab %}

{% tab title="Python" %}

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())

{% endtab %} {% endtabs %}

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