From graphql-request
Replace GraphQLClient with createGraphQLRequest
Install @octopus-energy/graphql, then replace the client factory and move variables into the request options. You can reuse your typed documents.
Send requests from the browser
Point the client at your existing same-origin GraphQL route. The browser sends the session cookie, and the route handles upstream authentication.
Before:
import { GraphQLClient } from "graphql-request";
const client = new GraphQLClient("/api/graphql");
const data = await client.request(AccountQuery, { accountNumber });After:
import { createGraphQLRequest } from "@octopus-energy/graphql/request";
const request = createGraphQLRequest("/api/graphql");
const data = await request(AccountQuery, { variables: { accountNumber } });Call Kraken directly from server code
These examples call Kraken from server code. Resolve accessToken on the server for the current request.
Before:
import { GraphQLClient } from "graphql-request";
const client = new GraphQLClient("https://api.oexx-kraken.energy/v1/graphql/");
const data = await client.request(AccountQuery, { accountNumber }, { authorization: accessToken });After:
import { createGraphQLRequest } from "@octopus-energy/graphql/request";
const request = createGraphQLRequest("https://api.oexx-kraken.energy/v1/graphql/");
const data = await request(AccountQuery, {
variables: { accountNumber },
headers: { authorization: accessToken },
});Pass an abort signal in the call options as signal. The client sends POST requests, so update any GET-only route to accept POST before switching the client.
Access response metadata and partial results
Replace rawRequest with errorPolicy: "all". Non-2xx responses still throw.
Before:
import { GraphQLClient } from "graphql-request";
const client = new GraphQLClient("https://api.oexx-kraken.energy/v1/graphql/", {
errorPolicy: "all",
});
const { data, errors, status } = await client.rawRequest(
AccountQuery,
{ accountNumber },
{ authorization: accessToken },
);After:
const { data, errors, response } = await request(AccountQuery, {
variables: { accountNumber },
headers: { authorization: accessToken },
errorPolicy: "all",
});
const status = response.status;Catch request failures
Replace ClientError checks with RequestError.
Before:
import { ClientError } from "graphql-request";
try {
await client.request(AccountQuery, { accountNumber }, { authorization: accessToken });
} catch (error) {
if (!(error instanceof ClientError)) throw error;
console.error(error.message, error.response.status);
}After:
import { RequestError } from "@octopus-energy/graphql/request";
try {
await request(AccountQuery, {
variables: { accountNumber },
headers: { authorization: accessToken },
});
} catch (error) {
if (!(error instanceof RequestError)) throw error;
console.error(error.message, error.response.status);
}See Configure requests for examples and Compare clients for feature and bundle differences.