Pagination#

The Afosto API uses Relay-style cursor-based pagination for all list fields.

How it works#

List fields return a Connection type with:

  • edges — array of result items wrapped in an Edge type
  • pageInfo — metadata about the current page

PageInfo#

NameTypeRequiredDescription
hasNextPage
Boolean!
RequiredWhether there are more items after this page
hasPreviousPage
Boolean!
RequiredWhether there are more items before this page
startCursor
String
OptionalCursor of the first item in this page
endCursor
String
OptionalCursor of the last item in this page

Edge#

Each edge wraps a single result item with its cursor:

NameTypeRequiredDescription
cursor
String!
RequiredOpaque cursor for this item — pass to after or before
node
T!
RequiredThe item itself (type depends on the query)

Fetching the first page#

query {
  products(first: 20) {
    pageInfo {
      hasNextPage
      endCursor
    }
    edges {
      node {
        id
        name
      }
    }
  }
}

Fetching the next page#

Use the endCursor from the previous response as the after argument:

query {
  products(first: 20, after: "cursor_from_previous_response") {
    pageInfo {
      hasNextPage
      endCursor
    }
    edges {
      node {
        id
        name
      }
    }
  }
}

Arguments#

All paginated list fields accept these arguments:

NameTypeRequiredDescription
first
Int
OptionalNumber of items to fetch from the start (forward pagination)
after
String
OptionalCursor to start after — use endCursor from the previous page
last
Int
OptionalNumber of items to fetch from the end (backward pagination)
before
String
OptionalCursor to fetch before — use startCursor from the previous page

Iterating all pages#

async function fetchAllProducts() {
  let hasNextPage = true
  let cursor: string | null = null
  const products = []

  while (hasNextPage) {
    const result = await query({
      query: GET_PRODUCTS,
      variables: { first: 100, after: cursor },
    })

    products.push(...result.products.edges.map((e) => e.node))
    hasNextPage = result.products.pageInfo.hasNextPage
    cursor = result.products.pageInfo.endCursor
  }

  return products
}
Query Runnerhttps://afosto.app/graphql

No query loaded

Click play on any code block in the docs to load a query here.