-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.tsx
110 lines (98 loc) · 2.91 KB
/
client.tsx
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import { HttpLink, InMemoryCache, ApolloClient } from '@apollo/client';
import { getMainDefinition } from '@apollo/client/utilities';
import { ApolloLink, concat, split } from 'apollo-link';
import { WebSocketLink } from 'apollo-link-ws';
import fetch from 'node-fetch';
export interface IApolloClientGeneratorOptions {
initialStore?: any;
token?: string;
client?: string;
secret?: string;
ssl?: boolean;
path?: string;
headers?: any;
ws?: boolean;
}
export function generateHeaders(options: IApolloClientGeneratorOptions) {
const headers: IApolloClientGeneratorOptions['headers'] = { ...options.headers };
if (options.token) headers.Authorization = `Bearer ${options.token}`;
if (options.secret) headers['x-hasura-admin-secret'] = options.secret;
if (options.client) headers['x-hasura-client'] = options.client;
return headers;
}
export interface IApolloClient<T> extends ApolloClient<T> {
jwt_token?: string;
}
export function generateApolloClient(
options: IApolloClientGeneratorOptions,
forwardingArguments?: {
ApolloClient?: any;
InMemoryCache?: any;
},
): ApolloClient<any> {
const headers = generateHeaders(options);
const httpLink = new HttpLink({
uri: `http${options.ssl ? 's' : ''}://${options.path || ''}`,
// @ts-ignore
fetch,
headers,
});
const wsLink = options.ws
? new WebSocketLink({
uri: `ws${options.ssl ? 's' : ''}://${options.path || ''}`,
options: {
lazy: true,
reconnect: true,
connectionParams: () => ({
headers,
}),
},
})
: null;
const authMiddleware = new ApolloLink((operation, forward) => {
operation.setContext({
headers,
});
return forward(operation);
});
const link = !options.ws
? httpLink
: split(
({ query }) => {
// return true;
// if you need ws only for subscriptions:
const def = getMainDefinition(query);
return def?.kind === 'OperationDefinition' && def?.operation === 'subscription';
},
wsLink,
// @ts-ignore
httpLink,
);
const client: IApolloClient<any> = new ApolloClient({
ssrMode: true,
// @ts-ignore
link: concat(authMiddleware, link),
connectToDevTools: true,
cache: new InMemoryCache({
...forwardingArguments?.InMemoryCache,
freezeResults: false,
resultCaching: false,
}).restore(options.initialStore || {}),
...forwardingArguments?.ApolloClient,
defaultOptions: {
watchQuery: {
fetchPolicy: 'no-cache',
errorPolicy: 'ignore',
...forwardingArguments?.ApolloClient?.defaultOptions?.watchQuery,
},
query: {
fetchPolicy: 'no-cache',
errorPolicy: 'ignore',
...forwardingArguments?.ApolloClient?.defaultOptions?.query,
},
...forwardingArguments?.ApolloClient?.defaultOptions,
},
});
client.jwt_token = options.token;
return client;
}