-
Notifications
You must be signed in to change notification settings - Fork 1
/
apollo.js
109 lines (76 loc) · 2.88 KB
/
apollo.js
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
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _apolloClient = require('apollo-client');
var _apolloLinkHttp = require('apollo-link-http');
var _apolloLink = require('apollo-link');
var _apolloCacheInmemory = require('apollo-cache-inmemory');
var _isomorphicUnfetch = require('isomorphic-unfetch');
var _isomorphicUnfetch2 = _interopRequireDefault(_isomorphicUnfetch);
var _flexibleCookies = require('flexible-cookies');
var _config = require('./config');
var _config2 = _interopRequireDefault(_config);
var _const = require('./lib/const');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
// Polyfill fetch() on the server (used by apollo-client)
if (!process.browser) {
global.fetch = _isomorphicUnfetch2.default;
}
function createApolloClient(initialState) {
const { endpoint, link } = _config2.default.get();
let httpLink;
if ('object' === typeof link && !link.request) {
const linkOpts = Object.assign({
uri: endpoint // Server URL (must be absolute)
}, link);
httpLink = new _apolloLinkHttp.HttpLink(linkOpts);
} else {
httpLink = link;
}
const authMiddleware = new _apolloLink.ApolloLink((operation, forward) => {
const headers = {};
const authorization = getAuthorization();
if (authorization) headers.authorization = authorization;
// add the authorization to the headers
operation.setContext({
headers
});
return forward(operation);
});
httpLink = authMiddleware.concat(httpLink);
return new _apolloClient.ApolloClient({
connectToDevTools: process.browser,
ssrMode: !process.browser, // Disables forceFetch on the server (so queries are only run once)
link: httpLink,
cache: new _apolloCacheInmemory.InMemoryCache().restore(initialState || {})
});
}
function getAuthorization() {
const { cookieSource } = _config2.default.get();
const { tokenType, defaultToken } = _config2.default.getAuth();
const cookieToken = _flexibleCookies.Cookies.get(_const.CONST_AUTHTOKEN_COOKIE, { source: cookieSource }) || defaultToken;
if (!cookieToken) return null;
if (cookieToken.match(`^${tokenType}`, 'i')) return cookieToken;
return `${tokenType} ${cookieToken}`;
}
class Apollo {
constructor() {
this.client = null;
}
getClient(initialState) {
// Make sure to create a new client for every server-side request so that data
// isn't shared between connections (which would be bad)
let newApolloClient;
if (!process.browser || !this.client) newApolloClient = createApolloClient(initialState);
if (!process.browser) return newApolloClient;
// Reuse client on the client-side
if (!this.client) this.client = newApolloClient;
return this.client;
}
forceCreate(initialState) {
this.client = createApolloClient(initialState);
return this.client;
}
}
exports.default = new Apollo();