Reference
createGraphQL options
This page lists every option createGraphQL(config) takes. Look here when you set up a GraphQL client and need an option’s exact type or default.
The config is a GraphQLBaseConfig, plus the operations: either operations, or queries and mutations.
| Option | Type | Default | What it does |
|---|---|---|---|
endpoint | string | required | The full URL of the GraphQL endpoint. It takes the place of baseUrl. |
operations | an object of Operations | — | Each key becomes a method on the client, such as graphql.getCategory (Operation options). |
queries | an object of Operations | — | Each key becomes a method under graphql.query, such as graphql.query.getCategory. |
mutations | an object of Operations | — | Each key becomes a method under graphql.mutation, such as graphql.mutation.updateCategory. |
middleware | Middleware[] | — | Runs on every operation, before operation and call middleware. |
headers | HeadersInit | — | Sent with every operation. An operation or a call can replace a header. Content-Type is application/json unless you set your own. |
fetchOptions | FetchOptions | — | Options passed through to fetch on every operation, such as credentials: 'include'. An operation or a call replaces a field (Cookies and other fetch options). |
fetch | (url: string, init: RequestInit) => Promise<Response> | the global fetch | The fetch this client sends with, such as undici’s or mockFetch().fetch. Without it, liaise looks up the global fetch on every call, so a stub or polyfill installed later is used. It is called unbound, so window.fetch works as is. A method that needs its object, such as a Cloudflare service binding’s, goes in a wrapper: fetch: (url, init) => env.SERVICE.fetch(url, init). cacheMiddleware keys on which fetch sends a call, so clients with different fetch functions never share entries, even through one shared operation. Clients given the same function share entries. A wrapper created for each request, such as (url, init) => env.SERVICE.fetch(url, init) inside a client built per request, gets entries of its own, so a cache shared across requests never hits for it: create the wrapper once, outside the request, if those requests may share answers. Clients on the global fetch share as before. |
timeout | number (ms) | no deadline | One deadline for every operation, retries included. An operation or a call can set its own, and 0 there turns it off. |
log | boolean | LogOptions | off | Logs every call to the console. Each line names the operation (Log every call, Log options). |
onError | (error: ApiError) => void | — | Called once per failed call, GraphQL errors included. Calls that share one failed request count as one. Never for 'abort'. If it throws, the caller still gets its Result. |
Use operations for one flat client. To keep queries and mutations apart, use queries, mutations or both instead. TypeScript refuses operations together with either of the others. A query and a mutation may have the same name, and each runs its own operation (Queries and mutations).
A method takes the variables and an optional CallOptions, and returns a promise of a Result. An operation whose variables type is Record<string, never> is called with no arguments. Each method also has getHeaders().
createGraphQL throws if an operation sets both share and dedupe, as createApi does for an endpoint. A call never throws.
Next: Endpoint options lists what one REST endpoint can set.