Skip to Content
Octopus Energy Packages

Configure requests

Set request headers, handle partial results, and cancel requests

Set request defaults, choose how to handle errors, and cancel requests. The examples use the client and typed document from Getting started.

Set request defaults

Set defaults when creating the request function:

lib/graphql/request.ts
import { createGraphQLRequest } from "@octopus-energy/graphql/request";

export const kraken = createGraphQLRequest("https://api.oexx-kraken.energy/v1/graphql/", {
  headers: { "x-application": "example-app" },
});

Override options for an individual request. Headers merge by name, so this request keeps the default x-application header:

import { kraken } from "#/lib/graphql/request";
import { ElectricVehiclesQuery } from "#/lib/vehicles/get-vehicles";

const data = await kraken(ElectricVehiclesQuery, {
  variables: { make: "Tesla" },
  headers: { "x-request-id": "request-123" },
});

Pass user-specific headers, such as Authorization, per request. See request options for per-call settings and how they override defaults.

Choose an error policy

GraphQL responses can contain both data and errors. The error policy determines how the client handles them:

PolicyBehavior
none (default)Reject with RequestError on GraphQL errors; otherwise return data.
allReturn a GraphQLResponse with data, errors, and response metadata.
ignoreReturn data, ignoring GraphQL errors. Data can be null or undefined.

Use all when you want to handle partial results:

const { data, errors } = await kraken(ElectricVehiclesQuery, {
  variables: { make: "Tesla" },
  errorPolicy: "all",
});

data contains any available results, and errors contains any GraphQL errors. Either field can be absent.

Set errorPolicy on createGraphQLRequest to use it as the default. Individual requests can override it.

HTTP and response failures

Error policies apply to GraphQL errors. HTTP errors and invalid responses reject the promise under every policy.

Catch request failures

Catch RequestError to inspect a failed response:

import { RequestError } from "@octopus-energy/graphql/request";

try {
  await kraken(ElectricVehiclesQuery, { variables: { make: "Tesla" } });
} catch (error) {
  if (!(error instanceof RequestError)) throw error;

  console.error(error.message, error.response.status);
}

The client consumes the response body and stores it in error.body as parsed JSON or original text. Inside the catch block, use error.graphql to inspect the GraphQL payload when available and error.request.variables to inspect the variables you sent.

console.error(error.graphql?.errors);
console.debug(error.request.variables); // { make: "Tesla" }

Network and abort errors propagate unchanged.

Cancel a request

Pass an AbortSignal in the request options. The Fetch transport handles cancellation, including signals that are already aborted. For example, cancel a request after five seconds:

const data = await kraken(ElectricVehiclesQuery, {
  variables: { make: "Tesla" },
  signal: AbortSignal.timeout(5_000),
});

To cancel manually, pass an AbortController’s signal and call controller.abort() when needed.

On this page