forked from Sly777/ran
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapolloClient.js
More file actions
49 lines (43 loc) · 1.18 KB
/
Copy pathapolloClient.js
File metadata and controls
49 lines (43 loc) · 1.18 KB
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
import ApolloClient, { createNetworkInterface } from 'apollo-client';
import persist from './persist';
const initNetworkInterface = token => {
const networkInterface = createNetworkInterface({
uri: 'https://api.graph.cool/simple/v1/cj7ke77fv0e9i0122pflagbvx',
opts: {
credentials: 'same-origin'
}
});
networkInterface.use([
{
applyMiddleware(req, next) {
if (!req.options.headers) {
req.options.headers = {};
}
(async () => {
// eslint-disable-next-line no-param-reassign
token = token || (await persist.willGetAccessToken());
req.options.headers.authorization = token ? `Bearer ${token}` : null;
next();
})();
}
}
]);
return networkInterface;
};
let apolloClient = null;
const createClient = (headers, token) =>
new ApolloClient({
ssrMode: !process.browser,
ssrForceFetchDelay: 100,
headers,
networkInterface: initNetworkInterface(token)
});
export default (headers, token) => {
if (!process.browser) {
return createClient(headers, token);
}
if (!apolloClient) {
apolloClient = createClient(headers, token);
}
return apolloClient;
};