This repository was archived by the owner on Aug 22, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApolloClient.js
More file actions
332 lines (307 loc) · 11.3 KB
/
ApolloClient.js
File metadata and controls
332 lines (307 loc) · 11.3 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
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/* @flow */
const { ApolloClient: Client } = require('apollo-client')
const { from, split } = require('apollo-link')
const { onError } = require('apollo-link-error')
const { RetryLink } = require('apollo-link-retry')
const { BatchHttpLink } = require('apollo-link-batch-http')
const fetch = require('isomorphic-unfetch')
const { WebSocketLink } = require('apollo-link-ws')
const { getMainDefinition } = require('apollo-utilities')
const { InMemoryCache } = require('apollo-cache-inmemory')
const compact = require('lodash/compact')
const noop = require('lodash/noop')
const { NativeWebSocket, isNode } = require('./constants')
/*::
import ApolloLink from 'apollo-link'
import { ApolloCache } from 'apollo-cache'
export type ApolloClientOptions = {
uri?: string;
browserUri?: string;
serverUri?: string;
wsUri?: string;
cache?: Object;
onError?: (error?: any) => void;
ssrForceFetchDelay?: number;
connectToDevTools?: boolean;
queryDeduplication?: boolean;
name?: string;
version?: string;
defaultOptions?: { watchQuery?: Object; query?: Object; mutate?: Object; };
wsOptions?: Object;
webSocketImpl?: WebSocket;
delay?: { initial?: number; max?: number; jitter?: number; };
attempts?: { max?: number; retryIf?: (error?: any, operation?: any) => boolean; };
includeExtensions?: boolean;
headers?: Object;
credentials?: string;
fetchOptions?: Object;
useGETForQueries?: boolean;
batchMax?: number;
batchInterval?: number;
batchKey?: () => string;
}
*/
/**
* `ApolloLink` is a standard interface for modifying control flow of GraphQL
* requests and fetching GraphQL results.
*
* @external ApolloLink
* @see {@link https://www.apollographql.com/docs/link/index.html|ApolloLink}
*/
/**
* @external ApolloCache
* @see {@link https://www.apollographql.com/docs/angular/basics/caching.html|ApolloCache}
*/
/**
* {@link ApolloClient} configuration options
* @typedef {Object} ApolloClientOptions
* @param {string} [uri='/graphql']
* @param {string} [browserUri=uri] - used in the browser
* @param {string} [serverUri=browserUri] - used in Node.js
* @param {string} [wsUri] - if provided a WebSocketLink will be configured
* @param {Object} [cache={}] - use on the client to restore server state
* @param {Function} [onError] - by default prints errors to console
* @param {number} [ssrForceFetchDelay] - see {@link https://www.apollographql.com/docs/react/api/apollo-client.html|apollo-client}
* @param {boolean} [connectToDevTools] - see {@link https://www.apollographql.com/docs/react/api/apollo-client.html|apollo-client}
* @param {boolean} [queryDeduplication] - see {@link https://www.apollographql.com/docs/react/api/apollo-client.html|apollo-client}
* @param {string} [name] - see {@link https://www.apollographql.com/docs/react/api/apollo-client.html|apollo-client}
* @param {string} [version] - see {@link https://www.apollographql.com/docs/react/api/apollo-client.html|apollo-client}
* @param {Object} [defaultOptions] - see {@link https://www.apollographql.com/docs/react/api/apollo-client.html|apollo-client}
* @param {Object} [wsOptions={ reconnect: true }] - see {@link https://www.npmjs.com/package/apollo-link-ws|apollo-link-ws}
* @param {WebSocket} [webSocketImpl] - see {@link https://www.npmjs.com/package/apollo-link-ws|apollo-link-ws}
* @param {Object} [delay] - see {@link https://www.npmjs.com/package/apollo-link-retry|apollo-link-retry}
* @param {Object} [attempts] - see {@link https://www.npmjs.com/package/apollo-link-retry|apollo-link-retry}
* @param {boolean} [includeExtensions] - see {@link https://www.npmjs.com/package/apollo-link-batch-http|apollo-link-batch-http}
* @param {Object} [headers={}] - see {@link https://www.npmjs.com/package/apollo-link-batch-http|apollo-link-batch-http}
* @param {string} [credentials='same-origin'] - see {@link https://www.npmjs.com/package/apollo-link-batch-http|apollo-link-batch-http}
* @param {Object} [fetchOptions={}] - see {@link https://www.npmjs.com/package/apollo-link-batch-http|apollo-link-batch-http}
* @param {boolean} [useGETForQueries] - see {@link https://www.npmjs.com/package/apollo-link-batch-http|apollo-link-batch-http}
* @param {number} [batchMax] - see {@link https://www.npmjs.com/package/apollo-link-batch-http|apollo-link-batch-http}
* @param {number} [batchInterval] - see {@link https://www.npmjs.com/package/apollo-link-batch-http|apollo-link-batch-http}
* @param {Function} [batchKey] - see {@link https://www.npmjs.com/package/apollo-link-batch-http|apollo-link-batch-http}
* @example
* import type { ApolloClientOptions } from 'secondwheel/ApolloClient'
*/
/**
* - like {@link https://www.npmjs.com/package/apollo-boost|apollo-boost}, but
* allows for a greater degree of configurability
* - augments {@link https://www.npmjs.com/package/apollo-client|apollo-client}
* with usefull defaults
* - batches requests, retries failed requests
* - works on Node.js and in the browser alike
* - subscriptions baked in
* - provides static methods, overriding which any aspect of the default
* functionality can be changed
*
* @example
* import ApolloClient from 'secondwheel/ApolloClient'
*
* // connecting to a simple "graphql-yoga" server with default options
* const client = new ApolloClient({
* uri: 'http://localhost:4000',
* wsUri: 'ws://localhost:4000'
* })
*/
class ApolloClient extends Client {
constructor (options/*: ApolloClientOptions */ = {}) {
const {
ssrForceFetchDelay,
connectToDevTools,
queryDeduplication,
name,
version,
defaultOptions
} = options
super({
link: ApolloClient.createLink(options),
cache: ApolloClient.createCache(options),
ssrMode: isNode,
ssrForceFetchDelay,
connectToDevTools,
queryDeduplication,
name,
version,
defaultOptions
})
}
static testOperation ({ query }/*: Object */) {
const { kind, operation } = getMainDefinition(query)
return kind === 'OperationDefinition' && operation === 'subscription'
}
/**
* creates the final instance of ApolloLink
*
* @example
* // override to a default behaviour (will have no effect)
* import { split } from 'apollo-link'
*
* ApolloClient.createLink = options => {
* const wsLink = ApolloClient.createWSLink(options)
* const httpLink = ApolloClient.createHttpLink(options)
*
* return wsLink ? split(ApolloClient.testOperation, wsLink, httpLink) : httpLink
* }
*/
static createLink (options/*: ApolloClientOptions */)/*: ApolloLink */ {
const wsLink = ApolloClient.createWSLink(options)
const httpLink = ApolloClient.createHttpLink(options)
return wsLink ? split(ApolloClient.testOperation, wsLink, httpLink) : httpLink
}
/**
* creates an instance of ApolloCache
*
* @example
* // override to a default behaviour (will have no effect)
* import { InMemoryCache } from 'apollo-cache-inmemory'
*
* ApolloClient.createCache = ({ cache = {} }) => new InMemoryCache().restore(cache)
*/
static createCache (options/*: ApolloClientOptions */)/*: ApolloCache */ {
const { cache = {} } = options
return new InMemoryCache().restore(cache)
}
/**
* creates an instance of ApolloLink
*
* @example
* // override to a default behaviour (will have no effect)
* import { from } from 'apollo-link'
* import compact from 'lodash/compact'
*
* ApolloClient.createHttpLink = options => from(compact(ApolloClient.getLinks(options)))
*/
static createHttpLink (options/*: ApolloClientOptions */)/*: ApolloLink */ {
return from(compact(ApolloClient.getLinks(options)))
}
/**
* creates an instance of WebSocketLink
*
* @example
* // override to a default behaviour (will have no effect)
* import { NativeWebSocket } from 'secondwheel/constants'
* import { WebSocketLink } from 'apollo-link-ws'
*
* ApolloClient.createWSLink = ({ wsUri, wsOptions = { reconnect: true }, webSocketImpl = NativeWebSocket }) =>
* wsUri && webSocketImpl && new WebSocketLink({
* uri: wsUri,
* options: wsOptions,
* webSocketImpl
* })
*/
static createWSLink (options/*: ApolloClientOptions */)/*: ?ApolloLink */ {
const { wsUri, wsOptions = { reconnect: true }, webSocketImpl = NativeWebSocket } = options
return wsUri && webSocketImpl && new WebSocketLink({
uri: wsUri,
options: wsOptions,
webSocketImpl
})
}
/**
* returns an array of ApolloLink to be used in the creation of the final link
* falsy values are filtered out
*
* @example
* // override to a default behaviour (will have no effect)
* ApolloClient.getLinks = options => [
* ApolloClient.createErrorLink(options),
* ApolloClient.createRetryLink(options),
* ApolloClient.createBatchLink(options)
* ]
*/
static getLinks (options/*: ApolloClientOptions */)/*: Array<?ApolloLink> */ {
return [
ApolloClient.createErrorLink(options),
ApolloClient.createRetryLink(options),
ApolloClient.createBatchLink(options)
]
}
/**
* creates an instance of ErrorLink
*
* @example
* // override to a default behaviour (will have no effect)
* import noop from 'lodash/noop'
* import { onError } from 'apollo-link-error'
*
* ApolloClient.createErrorLink = ({ onError: errorCallback = noop }) => onError(errorCallback)
*/
static createErrorLink (options/*: ApolloClientOptions */)/*: ?ApolloLink */ {
const { onError: errorCallback = noop } = options
return onError(errorCallback)
}
/**
* creates an instance of RetryLink
*
* @example
* // override to a default behaviour (will have no effect)
* import { RetryLink } from 'apollo-link-retry'
*
* ApolloClient.createRetryLink = ({ delay, attempts }) => new RetryLink({ delay, attempts })
*/
static createRetryLink (options/*: ApolloClientOptions */)/*: ?ApolloLink */ {
const { delay, attempts } = options
return new RetryLink({ delay, attempts })
}
/**
* creates an instance of HttpLink
*
* @example
* // override to a default behaviour (will have no effect)
* import { isNode } from 'secondwheel/constants'
* import { BatchHttpLink } from 'apollo-link-batch-http'
*
* ApolloClient.createBatchLink = ({
* uri = '/graphql',
* browserUri = uri,
* serverUri = browserUri,
* includeExtensions,
* headers = {},
* credentials = 'same-origin',
* fetchOptions = {},
* useGETForQueries,
* batchMax,
* batchInterval,
* batchKey
* }) =>
* new BatchHttpLink({
* uri: isNode ? serverUri : browserUri,
* includeExtensions,
* fetch,
* headers,
* credentials,
* fetchOptions,
* useGETForQueries,
* batchMax,
* batchInterval,
* batchKey
* })
*/
static createBatchLink (options/*: ApolloClientOptions */)/*: ?ApolloLink */ {
const {
uri = '/graphql',
browserUri = uri,
serverUri = browserUri,
includeExtensions,
headers = {},
credentials = 'same-origin',
fetchOptions = {},
useGETForQueries,
batchMax,
batchInterval,
batchKey
} = options
return new BatchHttpLink({
uri: isNode ? serverUri : browserUri,
includeExtensions,
fetch,
headers,
credentials,
fetchOptions,
useGETForQueries,
batchMax,
batchInterval,
batchKey
})
}
}
module.exports = ApolloClient