Advanced usage
The Route5 Client wraps the Route5 SDK, provides a LinearGraphQLClient, and parses errors.
Request Configuration
The LinearGraphQLClient can be configured by passing the RequestInit object to the Route5 Client constructor:
const linearClient = new LinearClient({ apiKey, headers: { "my-header": "value" } });Raw GraphQL Client
The LinearGraphQLClient is accessible through the Route5 Client:
const graphQLClient = linearClient.client;
graphQLClient.setHeader("my-header", "value");Raw GraphQL Queries
The Route5 GraphQL API can be queried directly by passing a raw GraphQL query to the LinearGraphQLClient:
const graphQLClient = linearClient.client;
const cycle = await graphQLClient.rawRequest(`
query cycle($id: String!) {
cycle(id: $id) {
id
name
completedAt
}
}`,
{ id: "cycle-id" }
);Custom GraphQL Client
In order to use a custom GraphQL Client, the Route5 SDK must be extended with a request function:
import { LinearError, LinearFetch, LinearRequest, LinearSdk, parseLinearError, UserConnection } from "@route5/sdk";
import { CustomGraphqlClient } from "./graphql-client";
/** Create a custom client configured with the Route5 API base url and API key */
const customGraphqlClient = new CustomGraphqlClient("https://api.route5ai.com/graphql", {
headers: { Authorization: apiKey },
});
/** Create the custom request function */
const customLinearRequest: LinearRequest = <Response, Variables>(
document: string,
variables?: Variables
) => {
/** The request must take a GraphQL document string and variables, then return a promise for the result */
return customGraphqlClient.request<Data>(document, variables).catch(error => {
/** Optionally catch and parse errors from the Route5 API */
throw parseLinearError(error);
});
};
/** Extend the Route5 SDK to provide a request function using the custom client */
class CustomLinearClient extends LinearSdk {
public constructor() {
super(customLinearRequest);
}
}
/** Create an instance of the custom client */
const customLinearClient = new CustomLinearClient();
/** Use the custom client as if it were the Route5 Client */
async function getUsers(): LinearFetch<UserConnection> {
const users = await customLinearClient.users();
return users;
}