API reference
Request client options, result types, and errors
Import createGraphQLRequest and RequestError from @octopus-energy/graphql/request. See Getting started for a typed request example.
createGraphQLRequest
import { createGraphQLRequest } from "@octopus-energy/graphql/request";
const request = createGraphQLRequest("https://api.example.com/graphql");
const data = await request(AccountQuery, { variables: { accountNumber: "A-12345678" } });CreateGraphQLRequestOptions<TErrorPolicy>Prop
Type
GraphQLRequestParams<TVariables, TErrorPolicy>Prop
Type
The client sends operations as JSON over POST and asks the server for application/graphql-response+json or application/json.
Error policies
The factory defaults to none. Set a different default at creation or override it per call.
Malformed GraphQL errors
The client passes through server errors as received. The public type describes well-formed errors.
Malformed values can fall outside readonly GraphQLError[].
Every policy rejects non-2xx responses and invalid GraphQL envelopes with RequestError. See RequestError for how the client selects the error message.
| Error policy | Resolved value | GraphQL errors |
|---|---|---|
none | TData | Reject with RequestError |
all | GraphQLResponse<TData> | Return errors alongside any available data |
ignore | TData | null | undefined | Return available data and discard errors |
GraphQLRequestResult<TData, TErrorPolicy> describes the resolved value in the table. See Configure requests for a partial-results example.
GraphQLResponse<TData>Prop
Type
GraphQLErrorProp
Type
Type arguments
Typed documents infer the result and variable types. For a plain query string, you can supply these type arguments in order.
| Type argument | Default | Purpose |
|---|---|---|
TData | unknown | Operation result |
TVariables | Record<string, unknown> | Operation variables |
TErrorPolicy | Factory policy | Resolved value |
RequestError
The client rejects with RequestError for HTTP failures, invalid responses, and GraphQL errors under the none policy. Network and abort errors propagate unchanged.
RequestErrorProp
Type
RequestErrorMetadataProp
Type
GraphQLPayload<TData>Prop
Type
When the operation name is available, RequestError prefixes the message with it, for example [Account] Access denied. For errors raised by the client, the HTTP status determines the rest of the message.
- 2xx responses. The client uses
errors[0].messageif it’s a string. Otherwise it supplies a message describing why it rejected the response. - Non-2xx responses. The client uses
errors[0].messageif it’s a string and the response usesapplication/graphql-response+jsonorapplication/json. Otherwise it reports the HTTP status.
error.body retains the parsed JSON or the original text when parsing fails. The response, body, request, and graphql properties are non-enumerable. See Catch request failures for an example.
error.graphql exposes the parsed payload when it contains usable GraphQL data or errors. It references the same payload as error.body when available and is undefined otherwise. On non-2xx responses, it also requires a supported GraphQL JSON media type.