# GraphQL

A request to any GraphQL API over HTTP from the back end: the query's text,
its variables, the answer's `data`, its `errors` as failures with a kind, an
answer in part read as it is, and a connection read page by page by its
cursor. An [extension](../extensions/README.md) that knows no service:
[github](github.md) and [linear](linear.md) are made on it, and a program
uses it for any other GraphQL API (Shopify's Admin API, Contentful, Hasura,
a service of its own). Back to the [map](README.md).

```mote
use "pkg:graphql/graphql.mote"

shape Product
  id: string
  title: string

fn shopifySay(): GraphqlSay = {headers: {"x-shopify-access-token": secret("SHOPIFY_TOKEN")}}

fn shopifyAt(): string = `https://${env("SHOPIFY_SHOP")}.myshopify.com/admin/api/2025-10/graphql.json`

// Every product, 50 a page, by the connection's cursor.
server fn products(): Product[] requires signedIn
  let q = "query($after: String) { products(first: 50, after: $after) { nodes { id title } pageInfo { hasNextPage endCursor } } }"
  return graphqlAll(shopifyAt(), q, {}, "products", 1000, shopifySay())?.map(p => ({id: `${p.id}`, title: `${p.title}`}))
```

```toml
[packages]
graphql = "../mote/extensions/graphql"
```

## The calls

| | |
|---|---|
| `graphql(url, query, variables, say)` | The answer's `data`; an answer with errors (or a status that is not 2xx) is a failure. |
| `graphqlAnswer(url, query, variables, say)` | The whole answer, `GraphqlAnswer {status, data, errors, headers, said}`, without failing on its errors: for an API that answers in part (GitHub gives the nodes it may show and errors for the rest). Fails only when no answer came. |
| `graphqlPage(url, query, variables, at, cursor, say)` | One page of the connection at `at` (its parts joined with dots: `"repository.issues"`): `GraphqlPage {nodes, next, total}`. The query takes the cursor as `$after: String` and asks for `pageInfo { hasNextPage endCursor }` and `nodes { … }` (or `edges { node { … } }`); `cursor` is `""` for the first page. |
| `graphqlAll(url, query, variables, at, most, say)` | Every page of it, up to `most` nodes. |
| `graphqlAt(data, at)` | What is at a path of dots. |

`say` (`GraphqlSay`) is how to say who is asking, each part optional:
`headers` (a record of the API's own), `bearer` (a token sent as
`Authorization: Bearer …`), `authorization` (the header as it is), `timeout`
(milliseconds, 30000 unsaid), `retries` (2 unsaid).

Each error is a `GraphqlError {message, code, path, extensions}`: `code` is
its `extensions.code` (Apollo's, Shopify's `THROTTLED`, Linear's
`RATELIMITED`), else its `type` (GitHub's `NOT_FOUND`); `path` its path
joined with dots.

| Kind | |
|---|---|
| `"graphql"` | The answer had errors: the sentence is the first's message (and how many more), `data.errors` all of them, `data.code` and `data.path` the first's, `data.partial` what `data` came. |
| `"auth"` | 401, 403, or an error coded `UNAUTHENTICATED`, `FORBIDDEN`, `ACCESS_DENIED`. |
| `"limited"` | 429, or an error coded `RATE_LIMITED`, `RATELIMITED`, `THROTTLED`, after the tries: `data.retryAfter` in seconds when the API said. |
| `"server"` · `"invalid"` · `"offline"` | 5xx with no errors; another status with no errors (the API's own `message`, when it said one, follows); no answer. |

A query (anything whose first word is not `mutation` or `subscription`) is
asked again on 429, after its `Retry-After`, `say.retries` times; a
mutation never is, since it may have been done. Throttles that an API says
inside a 200 (Shopify's query cost, GitHub's points) are the caller's to
read from `data.errors` or `GraphqlAnswer.headers`: this piece does not know
each API's budget.

## Left out

Subscriptions (over a WebSocket: none on the back end yet); persisted
queries; batched queries in one request; uploads (the multipart request
spec); a schema read by introspection and typed at compile time (the
`gaps` agent's OpenAPI generator is where that would go).

Tested in `extensions/graphql/tests.mote` with `given net`, and through
GitHub and Linear's stand-ins (`tests/all.sh real_svc_code real_svc_work`).
No real API has been asked.
