For the complete documentation index, see [llms.txt](https://manual.firstresonance.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://manual.firstresonance.io/api/pagination.md).

# Pagination

For any query that returns a list of results, we highly recommend using pagination. If pagination is not applied, it is much more likely that a network timeout error (in the form of a 502 error) will be returned. This occurs when an API call doesn't receive a response for 30 seconds. Pagination allows you to return a smaller data payload by returning a subset of the results to avoid the timeout error. It subsequently gives you a way to query further results as needed.

We use a cursor pagination pattern for our GraphQL API. See more info on GraphQL pagination [here](https://graphql.org/learn/pagination/).

## Example

In this example, the parts query grabs the first five parts by utilizing the `first` parameter and fetch the `endCursor` in the `pageInfo` object as shown in the request as shown below.

```graphql
{
  parts(first: 5) {
    edges {
      node {
        id
      }
    }
    pageInfo {
      endCursor         #use this to grab the next set of parts
      hasNextPage       #are their more parts after the ones returned in this query?
      totalCount        #total number of parts
      count             #number of parts returned by this query
    }
  }
}
```

Example Response

```json
  "data": {
    "procedures": {
      "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 pagination parameters of `last`, `before`, and `after`.

In this next example, this parts query returns the next five parts after the part identified by the `endCursor` from the previous query. If all parts in ION were to be queried, this pattern can be used to fetch all parts in batches of 5. In addition, use the `hasNextPage` to determine if there are more results remaining after this query.

```graphql
{
  parts(first: 5, after: "YXJyYXljb25uZWN0aW9uOjE5") {
    edges {
      node {
        id
      }
    }
    pageInfo {
      endCursor         #use this to grab the next set of parts
      hasNextPage       #are their more parts after the ones returned in this query?
      totalCount        #total number of parts
      count             #number of parts returned by this query
    }
  }
}
```
