-
Notifications
You must be signed in to change notification settings - Fork 3
/
urql-client.ts
38 lines (34 loc) · 1011 Bytes
/
urql-client.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import {
Client,
createClient,
defaultExchanges,
Exchange,
} from "https://npm.tfl.dev/@urql/core@^3.0.0";
import {
getPackageContext,
setPackageContext,
} from "https://tfl.dev/@truffle/global-context@^1.0.0/package-context.ts";
import isSsr from "https://tfl.dev/@truffle/utils@~0.0.22/ssr/is-ssr.ts";
import config from "https://tfl.dev/@truffle/config@^1.0.0/index.ts";
import { getAuthExchange } from "./auth-exchange.ts";
import { getSubscriptionExchange } from "./subscription-exchange.ts";
export function getClient() {
const context = getPackageContext("@truffle/api@0");
if (!context.client) {
setPackageContext("@truffle/api@0", {
...context,
client: makeClient(),
});
}
return context.client as Client;
}
export function makeClient() {
return createClient({
url: `${config.API_URL}/graphql`,
exchanges: [
getAuthExchange(),
!isSsr && getSubscriptionExchange(),
...defaultExchanges,
].filter(Boolean) as Exchange[],
});
}