# 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/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/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.

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/developers/migrations/relay-connections.md). The [API reference](https://docs.pyck.cloud/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/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/developers/request-limits.md), which is generated from the deployment configuration.
