Skip to Content
Octopus Energy Packages

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 policyResolved valueGraphQL errors
noneTDataReject with RequestError
allGraphQLResponse<TData>Return errors alongside any available data
ignoreTData | null | undefinedReturn 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

GraphQLError

Prop

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 argumentDefaultPurpose
TDataunknownOperation result
TVariablesRecord<string, unknown>Operation variables
TErrorPolicyFactory policyResolved 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.

RequestError

Prop

Type

RequestErrorMetadata

Prop

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].message if it’s a string. Otherwise it supplies a message describing why it rejected the response.
  • Non-2xx responses. The client uses errors[0].message if it’s a string and the response uses application/graphql-response+json or application/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.

On this page