Skip to Content
Octopus Energy Packages

Getting started

Send GraphQL requests with inferred result and variable types

After installing the package, create a client and pass it a typed document to infer the request’s variables and result.

Create the request client

Create a request function preconfigured with your GraphQL endpoint:

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

export const kraken = createGraphQLRequest("https://api.oexx-kraken.energy/v1/graphql/");

Define a query

Install and configure gql.tada with your API’s schema before defining the query. The example imports graphql directly from gql.tada.

lib/vehicles/get-vehicles.ts
import { graphql } from "gql.tada";
import { kraken } from "#/lib/graphql/request";

export const ElectricVehiclesQuery = graphql(`
  query ElectricVehicles($make: String!) {
    electricVehicles(make: $make) {
      make
      models {
        vehicleId
        model
        batterySize
      }
    }
  }
`);

Supported typed documents

Inference works with any library producing TypedDocumentNode or TypedDocumentString values.

Make a request

Pass the document and variables to the client. It returns the GraphQL data field with an inferred type:

lib/vehicles/get-vehicles.ts
export function getVehicles(make: string) {
  return kraken(ElectricVehiclesQuery, { variables: { make } });
}

Call the function:

import { getVehicles } from "#/lib/vehicles/get-vehicles";

const { electricVehicles } = await getVehicles("Tesla");

By default, GraphQL errors reject the promise with RequestError. To work with partial results, choose an error policy.

On this page