GraphQL
Call a GraphQL endpoint with variables, unwrap data, and treat a 200 carrying errors as a failure.
Use graphql() when an API speaks GraphQL-over-HTTP and you want a stitch that
POSTs a query, passes variables per call, and hands back the data payload
already unwrapped — with a 200 that carries an errors array treated as a
failure, not a success.
Example
import { } from 'stitchapi';
const = <{ : { : string; : string } }>({
: 'https://api.example.com',
: '/graphql',
: `query GetUser($id: ID!) { user(id: $id) { id name } }`,
});
// Variables travel in the input; the result is already unwrapped from `data`.
const = await ({ : { : '1' } });Options
graphql() is a preset over stitch(): it fixes kind: 'graphql',
method: 'POST', and unwrap: 'data' for you. You supply the GraphQL document
as query, plus a baseUrl/path pointing at the endpoint.
Pass GraphQL variables through the input's variables field at call time —
getUser({ variables: { id: '1' } }) — so one stitch serves every set of
arguments. Because unwrap defaults to 'data', the resolved value is the
contents of the response's data object; override unwrap if you need a
different field or the raw envelope.
A GraphQL endpoint can return HTTP 200 while reporting an errors array
in the body. The stitch treats that as a failure and surfaces it — see
STITCH_GRAPHQL.
For the full set of fields a stitch accepts, see Reference → Config types.