GraphQL API > Pagination
OverviewGraphQL APIPagination
Pagination

The Lana TypeScript/JavaScript core library includes helper functions for pagination. These are available via the import path: @lana-commerce/core/fetchAllItems. This library exports two functions, and their usage necessitates the provision of a specially formatted query. The query must meet the following conditions:

  1. It must define a lastKey: String! variable.
  2. It must define a limit: Int! variable.
  3. The result must be named page.

For instance, let's define a products pagination query:

query GetProductsPage($shopID: String!, $lastKey: String!, $limit: Int!) {
  page: productsPage(shop_id: $shopID, last_key: $lastKey, limit: $limit, sort_by: created_at) {
    items {
      id
      title
    }
    last_key
  }
}
NOTE

Additional variables can be defined if required.

Generate the TypeScript files for the above GraphQL query and examine each of the functions in detail:

  • fetchAllItems - Fetches all items simultaneously. This function requires a query that meets the above-listed requirements.

    Example usage:

    import { prettyPrintRequestResponseError } from "@lana-commerce/core/request";
    import { fetchAllItems } from "@lana-commerce/core/fetchAllItems";
    import getProductsPageQuery from "./graphql/operations/GetProductsPageQuery";
    
    const opts = {
      authToken: TOKEN,
      commerce: true,
    };
    const result = await fetchAllItems(getProductsPageQuery, { shopID: SHOP_ID }, opts);
    if (result.kind !== "data") {
      throw new Error(prettyPrintRequestResponseError(result));
    }
    for (const p of result.data) {
      console.log(`id: ${p.id}, title: ${p.title}`);
    }
    
  • iterAllItems - Returns an async iterator over all items. This function also requires a query that meets the aforementioned conditions. If an error occurs, the function will throw IterError.

    Example usage:

    import { iterAllItems } from "@lana-commerce/core/fetchAllItems";
    import getProductsPageQuery from "./graphql/operations/GetProductsPageQuery";
    
    const opts = {
      authToken: TOKEN,
      commerce: true,
    };
    const iter = iterAllItems(getProductsPageQuery, { shopID: SHOP_ID }, opts);
    for await (const p of iter) {
      console.log(`id: ${p.id}, title: ${p.title}`);
    }
    
PREVIOUS
Errors
NEXT
JSON API