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:
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.
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:
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.