-
Notifications
You must be signed in to change notification settings - Fork 136
/
client.ts
183 lines (158 loc) · 5.63 KB
/
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
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import {
ApolloClient,
ApolloClientOptions,
ApolloLink,
createHttpLink,
InMemoryCache,
InMemoryCacheConfig,
NormalizedCacheObject,
} from '@apollo/client';
import merge from 'deepmerge';
import isEqual from 'lodash/isEqual.js';
import { useMemo } from 'react';
// eslint-disable-next-line import/extensions
import { setContext } from '@apollo/client/link/context';
// eslint-disable-next-line import/extensions
import { createPersistedQueryLink } from '@apollo/client/link/persisted-queries';
import { sha256 } from 'js-sha256';
// eslint-disable-next-line import/extensions
import { AppProps } from 'next/app';
import { ErrorLoggingLink } from './apollo/errorLoggingLink.js';
import { getAccessToken } from './auth/index.js';
import { getConfig } from './config/index.js';
import { getGraphqlEndpoint } from './lib/getGraphqlEndpoint.js';
import { hooks } from './wpHooks/index.js';
export const APOLLO_STATE_PROP_NAME = '__APOLLO_STATE__';
declare global {
interface Window {
[APOLLO_STATE_PROP_NAME]: NormalizedCacheObject;
}
}
const windowApolloState =
typeof window !== 'undefined' ? window[APOLLO_STATE_PROP_NAME] : {};
let apolloClient: ApolloClient<NormalizedCacheObject> | undefined;
let apolloAuthClient: ApolloClient<NormalizedCacheObject> | undefined;
export function createApolloClient(authenticated = false) {
const { possibleTypes, usePersistedQueries, useGETForQueries } = getConfig();
let inMemoryCacheObject: InMemoryCacheConfig = {
possibleTypes,
typePolicies: {
RootQuery: {
queryType: true,
fields: {
viewer: {
merge: true,
},
},
},
RootMutation: {
mutationType: true,
},
},
};
inMemoryCacheObject = hooks.applyFilters(
'apolloClientInMemoryCacheOptions',
inMemoryCacheObject,
{},
) as InMemoryCacheConfig;
let linkChain = ApolloLink.from([
new ErrorLoggingLink(),
createHttpLink({
uri: getGraphqlEndpoint(),
/**
* Only add this option if usePersistedQueries is not set/false.
* When persisted queries is enabled and this flag and useGETForHashedQueries
* are both set, there is a conflict and persisted queries does not work.
*/
useGETForQueries: useGETForQueries && !usePersistedQueries,
}),
]);
// If the user requested to use persisted queries, apply the link.
if (usePersistedQueries) {
linkChain = createPersistedQueryLink({
sha256,
useGETForHashedQueries: useGETForQueries,
}).concat(linkChain);
}
// If the request is coming from the auth client, apply the auth link.
if (authenticated) {
linkChain = setContext((_, { headers }) => {
// get the authentication token from local storage if it exists
const token = getAccessToken();
// return the headers to the context so httpLink can read them
return {
headers: {
...headers,
authorization: token ? `Bearer ${token}` : '',
},
};
}).concat(linkChain);
}
let apolloClientOptions: ApolloClientOptions<NormalizedCacheObject> = {
ssrMode: typeof window === 'undefined',
connectToDevTools: typeof window !== 'undefined',
link: linkChain,
cache: new InMemoryCache(inMemoryCacheObject).restore(windowApolloState),
};
apolloClientOptions = hooks.applyFilters(
'apolloClientOptions',
apolloClientOptions,
{},
) as ApolloClientOptions<NormalizedCacheObject>;
return new ApolloClient(apolloClientOptions);
}
export function getApolloClient(initialState = null) {
// eslint-disable-next-line @typescript-eslint/naming-convention, no-underscore-dangle
const _apolloClient = apolloClient ?? createApolloClient();
// If your page has Next.js data fetching methods that use Apollo Client, the initial state
// gets hydrated here
if (initialState) {
// Get existing cache, loaded during client side data fetching
const existingCache = _apolloClient.extract();
// Merge the initialState from getStaticProps/getServerSideProps in the existing cache
const data = merge(existingCache, initialState, {
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
arrayMerge: (destination, source) => [
...source,
...destination.filter((d) => source.every((s) => !isEqual(d, s))),
],
});
// Restore the cache with the merged data
_apolloClient.cache.restore(data);
}
// For SSG and SSR always create a new Apollo Client
if (typeof window === 'undefined') return _apolloClient;
// Create the Apollo Client once in the client
if (!apolloClient) apolloClient = _apolloClient;
return _apolloClient;
}
export function getApolloAuthClient() {
// eslint-disable-next-line @typescript-eslint/naming-convention, no-underscore-dangle
const _apolloAuthClient = apolloAuthClient ?? createApolloClient(true);
if (!apolloAuthClient) apolloAuthClient = _apolloAuthClient;
return _apolloAuthClient;
}
export function addApolloState(
client: ApolloClient<NormalizedCacheObject>,
pageProps: AppProps<{
props: { [key: string]: any };
revalidate?: number | boolean;
}>['pageProps'],
): AppProps<{
props: { [key: string]: any };
revalidate?: number | boolean;
}>['pageProps'] {
if (pageProps?.props) {
// eslint-disable-next-line no-param-reassign
pageProps.props[APOLLO_STATE_PROP_NAME] = client.cache.extract();
}
return pageProps;
}
export function useApollo(
pageProps: AppProps<{ [key: string]: any }>['pageProps'],
) {
const state = pageProps[APOLLO_STATE_PROP_NAME];
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
const store = useMemo(() => getApolloClient(state), [state]);
return store;
}