> ## Documentation Index
> Fetch the complete documentation index at: https://docs.firstresonance.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Paginate query results

> Use cursor-based pagination to fetch large GraphQL result sets in batches and avoid timeouts.

For any query that returns a list of results, you should use pagination. Without pagination, a large query is more likely to time out and return a 502 error. A 502 occurs when an API call doesn't receive a response within 30 seconds.

The ION GraphQL API uses a cursor pagination pattern.

## Fetch the first page

This example queries the first five parts with the `first` parameter and fetches the `endCursor` in the `pageInfo` object:

```graphql theme={null}
{
  parts(first: 5) {
    edges {
      node {
        id
      }
    }
    pageInfo {
      endCursor         # use this to fetch the next page
      hasNextPage       # are there more parts after this page?
      totalCount        #total number of parts
      count             #number of parts returned by this query
    }
  }
}
```

```json theme={null}
{
  "data": {
    "parts": {
      "edges": [
        {
          "node": {
            "id": 20
          }
        },
        {
          "node": {
            "id": 22
          }
        },
        {
          "node": {
            "id": 24
          }
        },
        {
          "node": {
            "id": 26
          }
        },
        {
          "node": {
            "id": 28
          }
        }
      ],
      "pageInfo": {
        "endCursor": "YXJyYXljb25uZWN0aW9uOjE5",
        "hasNextPage": true,
        "totalCount": 1259,
        "count": 5
      }
    }
  }
}
```

In addition to `first`, ION queries have the pagination parameters `last`, `before`, and `after`.

## Fetch the next page

To fetch the next page, pass the `endCursor` from the previous query to the `after` parameter:

```graphql theme={null}
{
  parts(first: 5, after: "YXJyYXljb25uZWN0aW9uOjE5") {
    edges {
      node {
        id
      }
    }
    pageInfo {
      endCursor         # use this to fetch the next page
      hasNextPage       # are there more parts after this page?
      totalCount        #total number of parts
      count             #number of parts returned by this query
    }
  }
}
```

To query every part in ION, repeat this pattern to fetch parts in batches of five. Use `hasNextPage` to determine whether more results remain after each query.
