# API conventions

Audience: Developer

Pagination, errors and request limits that hold across the Pyck GraphQL API

## Overview

This page collects the rules that hold across the Pyck GraphQL API: how lists are paged, where to find the error format, and the limits on a request. For the first request, see [Getting started](https://docs.pyck.cloud/dev/developers/getting-started.md).

## Pagination

Lists such as `inventoryItems` are Relay-style connections, and you page them with a cursor. A connection field takes these arguments:

- `first`: return the first _n_ elements from the list.
- `after`: return the elements that come after the given cursor.
- `last`: return the last _n_ elements from the list.
- `before`: return the elements that come before the given cursor.

Pass `first` with `after` to page forwards, or `last` with `before` to page backwards. Do not pass `first` and `last` together, the API does not support that. A negative `first` or `last` is an error, see [Error codes](https://docs.pyck.cloud/dev/developers/error-handling.md#error-codes).

A connection returns these fields:

- `edges`: the list of edges. Each edge has a `node`, which is the item, and a `cursor` for that item.
- `pageInfo`: `hasNextPage`, `hasPreviousPage`, `startCursor` and `endCursor`. When paginating forwards, `endCursor` is the cursor to continue with. When paginating backwards, `startCursor` is.
- `totalCount`: how many items the connection holds.

This query reads one page of inventory items:

```graphql
query GetInventoryItems($first: Int, $after: Cursor) {
  inventoryItems(first: $first, after: $after) {
    totalCount
    pageInfo {
      hasNextPage
      endCursor
    }
    edges {
      cursor
      node {
        id
        sku
      }
    }
  }
}
```

For the first page, send these variables:

```json
{ "first": 10 }
```

While `pageInfo.hasNextPage` is `true`, send the same query again with `after` set to the `endCursor` of the previous response:

```json
{ "first": 10, "after": "<endCursor of the previous response>" }
```

Always pass `first` or `last`, also on connections nested inside a node, to limit the results.

### Page size limit

A connection over stored records, such as `inventoryItems`, never returns more than 200 items at once, and the API does not tell you when it cuts the list:

- Without `first` or `last`, the connection returns at most 200 items, and `pageInfo.hasNextPage` does not tell you that more items exist. `totalCount` is not reliable here: it counts all matching items when the query sends `after` or `before` or does not select `edges`, and only the returned items otherwise. A list of 500 items can look like a complete list of 200.
- With `first` or `last`, `totalCount` counts all matching items and `hasNextPage` tells whether more follow.
- A `first` or `last` above 200 fails with an error whose message contains `limit exceeded: the maximum accepted limit is 200`. The error has no `extensions.code`. Use 200 or less.
- When a query without `first` or `last` returns exactly 200 items, the server logs a warning. The client gets no sign of it.

The rule: always page with `first` and `after` (or `last` and `before`), and keep going while `pageInfo.hasNextPage` is `true`. Never read a list without `first` or `last` as the full result.

For a loop that collects every page, filtering and ordering on a connection, and the Relay specification, see [Migration guide: Relay-style connections](https://docs.pyck.cloud/dev/developers/migrations/relay-connections.md). The [API reference](https://docs.pyck.cloud/dev/api.md) lists the arguments of each connection.

## Errors

A response carries errors in an `errors` array next to `data`. The structure of an error, the codes Pyck sets and how to handle each kind of failure are in [Error handling](https://docs.pyck.cloud/dev/developers/error-handling.md).

## Request limits

The endpoint limits how many requests a client may send and how large each request may be. The values and what a client gets back over a limit are in [Request limits](https://docs.pyck.cloud/dev/developers/request-limits.md), which is generated from the deployment configuration.
