In order to use a custom GraphQL Client, the Linear SDK must be extended and provided with a request function:
import { LinearError, LinearFetch, LinearRequest, LinearSdk, parseLinearError, UserConnection } from "@linear/sdk";
import { DocumentNode, GraphQLClient, print } from "graphql";
import { CustomGraphqlClient } from "./graphql-client";
/** Create a custom client configured with the Linear API base url and API key */
const customGraphqlClient = new CustomGraphqlClient("https://api.linear.app/graphql", {
headers: { Authorization: apiKey },
});
/** Create the custom request function */
const customLinearRequest: LinearRequest = <Response, Variables>(
document: DocumentNode,
variables?: Variables
) => {
/** The request must take a GraphQL document and variables, then return a promise for the result */
return customGraphqlClient.request<Data>(print(document), variables).catch(error => {
/** Optionally catch and parse errors from the Linear API */
throw parseLinearError(error);
});
};
/** Extend the Linear 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 Linear Client */
async function getUsers(): LinearFetch<UserConnection> {
const users = await customLinearClient.users();
return users;
}