Advanced Usage
The Linear Client wraps the Linear SDK, provides a LinearGraphQLClient, and parses errors.

Request Configuration

The LinearGraphQLClient can be configured by passing the RequestInit object to the Linear Client constructor:
1
const linearClient = new LinearClient({ apiKey, headers: { "my-header": "value" } });
Copied!

Raw GraphQL Client

The LinearGraphQLClient is accessible through the Linear Client:
1
const graphQLClient = linearClient.client;
2
graphQLClient.setHeader("my-header", "value");
Copied!

Raw GraphQL Queries

The Linear GraphQL API can be queried directly by passing a raw GraphQL query to the LinearGraphQLClient:
1
const graphQLClient = linearClient.client;
2
const cycle = await graphQLClient.rawRequest(`
3
query cycle($id: String!) {
4
cycle(id: $id) {
5
id
6
name
7
completedAt
8
}
9
}`,
10
{ id: "cycle-id" }
11
);
Copied!

Custom GraphQL Client

In order to use a custom GraphQL Client, the Linear SDK must be extended and provided with a request function:
1
import { LinearError, LinearFetch, LinearRequest, LinearSdk, parseLinearError, UserConnection } from "@linear/sdk";
2
import { DocumentNode, GraphQLClient, print } from "graphql";
3
import { CustomGraphqlClient } from "./graphql-client";
4
5
/** Create a custom client configured with the Linear API base url and API key */
6
const customGraphqlClient = new CustomGraphqlClient("https://api.linear.app/graphql", {
7
headers: { Authorization: apiKey },
8
});
9
10
/** Create the custom request function */
11
const customLinearRequest: LinearRequest = <Response, Variables>(
12
document: DocumentNode,
13
variables?: Variables
14
) => {
15
/** The request must take a GraphQL document and variables, then return a promise for the result */
16
return customGraphqlClient.request<Data>(print(document), variables).catch(error => {
17
/** Optionally catch and parse errors from the Linear API */
18
throw parseLinearError(error);
19
});
20
};
21
22
/** Extend the Linear SDK to provide a request function using the custom client */
23
class CustomLinearClient extends LinearSdk {
24
public constructor() {
25
super(customLinearRequest);
26
}
27
}
28
29
/** Create an instance of the custom client */
30
const customLinearClient = new CustomLinearClient();
31
32
/** Use the custom client as if it were the Linear Client */
33
async function getUsers(): LinearFetch<UserConnection> {
34
const users = await customLinearClient.users();
35
return users;
36
}
Copied!
Last modified 8mo ago
Copy link
Contents