-
Notifications
You must be signed in to change notification settings - Fork 517
/
index.js
498 lines (456 loc) · 15.3 KB
/
index.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
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
/**
* Copyright (c) 2019-present, GraphQL Foundation
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict
*/
// A Function, which when given an Array of keys, returns a Promise of an Array
// of values or Errors.
export type BatchLoadFn<K, V> = (
keys: $ReadOnlyArray<K>,
) => Promise<$ReadOnlyArray<V | Error>>;
// Optionally turn off batching or caching or provide a cache key function or a
// custom cache instance.
export type Options<K, V, C = K> = {
batch?: boolean,
maxBatchSize?: number,
batchScheduleFn?: (callback: () => void) => void,
cache?: boolean,
cacheKeyFn?: (key: K) => C,
cacheMap?: CacheMap<C, Promise<V>> | null,
name?: string,
};
// If a custom cache is provided, it must be of this type (a subset of ES6 Map).
export type CacheMap<K, V> = {
get(key: K): V | void,
set(key: K, value: V): any,
delete(key: K): any,
clear(): any,
};
/**
* A `DataLoader` creates a public API for loading data from a particular
* data back-end with unique keys such as the `id` column of a SQL table or
* document name in a MongoDB database, given a batch loading function.
*
* Each `DataLoader` instance contains a unique memoized cache. Use caution when
* used in long-lived applications or those which serve many users with
* different access permissions and consider creating a new instance per
* web request.
*/
class DataLoader<K, V, C = K> {
constructor(batchLoadFn: BatchLoadFn<K, V>, options?: Options<K, V, C>) {
if (typeof batchLoadFn !== 'function') {
throw new TypeError(
'DataLoader must be constructed with a function which accepts ' +
`Array<key> and returns Promise<Array<value>>, but got: ${batchLoadFn}.`,
);
}
this._batchLoadFn = batchLoadFn;
this._maxBatchSize = getValidMaxBatchSize(options);
this._batchScheduleFn = getValidBatchScheduleFn(options);
this._cacheKeyFn = getValidCacheKeyFn(options);
this._cacheMap = getValidCacheMap(options);
this._batch = null;
this.name = getValidName(options);
}
// Private
_batchLoadFn: BatchLoadFn<K, V>;
_maxBatchSize: number;
_batchScheduleFn: (() => void) => void;
_cacheKeyFn: K => C;
_cacheMap: CacheMap<C, Promise<V>> | null;
_batch: Batch<K, V> | null;
/**
* Loads a key, returning a `Promise` for the value represented by that key.
*/
load(key: K): Promise<V> {
if (key === null || key === undefined) {
throw new TypeError(
'The loader.load() function must be called with a value, ' +
`but got: ${String(key)}.`,
);
}
const batch = getCurrentBatch(this);
const cacheMap = this._cacheMap;
let cacheKey: ?C;
// If caching and there is a cache-hit, return cached Promise.
if (cacheMap) {
cacheKey = this._cacheKeyFn(key);
const cachedPromise = cacheMap.get(cacheKey);
if (cachedPromise) {
const cacheHits = batch.cacheHits || (batch.cacheHits = []);
return new Promise(resolve => {
cacheHits.push(() => {
resolve(cachedPromise);
});
});
}
}
// Otherwise, produce a new Promise for this key, and enqueue it to be
// dispatched along with the current batch.
batch.keys.push(key);
const promise = new Promise((resolve, reject) => {
batch.callbacks.push({ resolve, reject });
});
// If caching, cache this promise.
if (cacheMap) {
cacheMap.set((cacheKey: any), promise);
}
return promise;
}
/**
* Loads multiple keys, promising an array of values:
*
* var [ a, b ] = await myLoader.loadMany([ 'a', 'b' ]);
*
* This is similar to the more verbose:
*
* var [ a, b ] = await Promise.all([
* myLoader.load('a'),
* myLoader.load('b')
* ]);
*
* However it is different in the case where any load fails. Where
* Promise.all() would reject, loadMany() always resolves, however each result
* is either a value or an Error instance.
*
* var [ a, b, c ] = await myLoader.loadMany([ 'a', 'b', 'badkey' ]);
* // c instanceof Error
*
*/
loadMany(keys: $ReadOnlyArray<K>): Promise<Array<V | Error>> {
if (!isArrayLike(keys)) {
throw new TypeError(
'The loader.loadMany() function must be called with Array<key> ' +
`but got: ${(keys: any)}.`,
);
}
// Support ArrayLike by using only minimal property access
const loadPromises = [];
for (let i = 0; i < keys.length; i++) {
loadPromises.push(this.load(keys[i]).catch(error => error));
}
return Promise.all(loadPromises);
}
/**
* Clears the value at `key` from the cache, if it exists. Returns itself for
* method chaining.
*/
clear(key: K): this {
const cacheMap = this._cacheMap;
if (cacheMap) {
const cacheKey = this._cacheKeyFn(key);
cacheMap.delete(cacheKey);
}
return this;
}
/**
* Clears the entire cache. To be used when some event results in unknown
* invalidations across this particular `DataLoader`. Returns itself for
* method chaining.
*/
clearAll(): this {
const cacheMap = this._cacheMap;
if (cacheMap) {
cacheMap.clear();
}
return this;
}
/**
* Adds the provided key and value to the cache. If the key already
* exists, no change is made. Returns itself for method chaining.
*
* To prime the cache with an error at a key, provide an Error instance.
*/
prime(key: K, value: V | Promise<V> | Error): this {
const cacheMap = this._cacheMap;
if (cacheMap) {
const cacheKey = this._cacheKeyFn(key);
// Only add the key if it does not already exist.
if (cacheMap.get(cacheKey) === undefined) {
// Cache a rejected promise if the value is an Error, in order to match
// the behavior of load(key).
let promise;
if (value instanceof Error) {
promise = Promise.reject(value);
// Since this is a case where an Error is intentionally being primed
// for a given key, we want to disable unhandled promise rejection.
promise.catch(() => {});
} else {
promise = Promise.resolve(value);
}
cacheMap.set(cacheKey, promise);
}
}
return this;
}
/**
* The name given to this `DataLoader` instance. Useful for APM tools.
*
* Is `null` if not set in the constructor.
*/
name: string | null;
}
// Private: Enqueue a Job to be executed after all "PromiseJobs" Jobs.
//
// ES6 JavaScript uses the concepts Job and JobQueue to schedule work to occur
// after the current execution context has completed:
// http://www.ecma-international.org/ecma-262/6.0/#sec-jobs-and-job-queues
//
// Node.js uses the `process.nextTick` mechanism to implement the concept of a
// Job, maintaining a global FIFO JobQueue for all Jobs, which is flushed after
// the current call stack ends.
//
// When calling `then` on a Promise, it enqueues a Job on a specific
// "PromiseJobs" JobQueue which is flushed in Node as a single Job on the
// global JobQueue.
//
// DataLoader batches all loads which occur in a single frame of execution, but
// should include in the batch all loads which occur during the flushing of the
// "PromiseJobs" JobQueue after that same execution frame.
//
// In order to avoid the DataLoader dispatch Job occuring before "PromiseJobs",
// A Promise Job is created with the sole purpose of enqueuing a global Job,
// ensuring that it always occurs after "PromiseJobs" ends.
//
// Node.js's job queue is unique. Browsers do not have an equivalent mechanism
// for enqueuing a job to be performed after promise microtasks and before the
// next macrotask. For browser environments, a macrotask is used (via
// setImmediate or setTimeout) at a potential performance penalty.
const enqueuePostPromiseJob =
typeof process === 'object' && typeof process.nextTick === 'function'
? function (fn) {
if (!resolvedPromise) {
resolvedPromise = Promise.resolve();
}
resolvedPromise.then(() => {
process.nextTick(fn);
});
}
: typeof setImmediate === 'function'
? function (fn) {
setImmediate(fn);
}
: function (fn) {
setTimeout(fn);
};
// Private: cached resolved Promise instance
let resolvedPromise;
// Private: Describes a batch of requests
type Batch<K, V> = {
hasDispatched: boolean,
keys: Array<K>,
callbacks: Array<{
resolve: (value: V) => void,
reject: (error: Error) => void,
}>,
cacheHits?: Array<() => void>,
};
// Private: Either returns the current batch, or creates and schedules a
// dispatch of a new batch for the given loader.
function getCurrentBatch<K, V>(loader: DataLoader<K, V, any>): Batch<K, V> {
// If there is an existing batch which has not yet dispatched and is within
// the limit of the batch size, then return it.
const existingBatch = loader._batch;
if (
existingBatch !== null &&
!existingBatch.hasDispatched &&
existingBatch.keys.length < loader._maxBatchSize
) {
return existingBatch;
}
// Otherwise, create a new batch for this loader.
const newBatch = { hasDispatched: false, keys: [], callbacks: [] };
// Store it on the loader so it may be reused.
loader._batch = newBatch;
// Then schedule a task to dispatch this batch of requests.
loader._batchScheduleFn(() => {
dispatchBatch(loader, newBatch);
});
return newBatch;
}
function dispatchBatch<K, V>(
loader: DataLoader<K, V, any>,
batch: Batch<K, V>,
) {
// Mark this batch as having been dispatched.
batch.hasDispatched = true;
// If there's nothing to load, resolve any cache hits and return early.
if (batch.keys.length === 0) {
resolveCacheHits(batch);
return;
}
// Call the provided batchLoadFn for this loader with the batch's keys and
// with the loader as the `this` context.
let batchPromise;
try {
batchPromise = loader._batchLoadFn(batch.keys);
} catch (e) {
return failedDispatch(
loader,
batch,
new TypeError(
'DataLoader must be constructed with a function which accepts ' +
'Array<key> and returns Promise<Array<value>>, but the function ' +
`errored synchronously: ${String(e)}.`,
),
);
}
// Assert the expected response from batchLoadFn
if (!batchPromise || typeof batchPromise.then !== 'function') {
return failedDispatch(
loader,
batch,
new TypeError(
'DataLoader must be constructed with a function which accepts ' +
'Array<key> and returns Promise<Array<value>>, but the function did ' +
`not return a Promise: ${String(batchPromise)}.`,
),
);
}
// Await the resolution of the call to batchLoadFn.
batchPromise
.then(values => {
// Assert the expected resolution from batchLoadFn.
if (!isArrayLike(values)) {
throw new TypeError(
'DataLoader must be constructed with a function which accepts ' +
'Array<key> and returns Promise<Array<value>>, but the function did ' +
`not return a Promise of an Array: ${String(values)}.`,
);
}
if (values.length !== batch.keys.length) {
throw new TypeError(
'DataLoader must be constructed with a function which accepts ' +
'Array<key> and returns Promise<Array<value>>, but the function did ' +
'not return a Promise of an Array of the same length as the Array ' +
'of keys.' +
`\n\nKeys:\n${String(batch.keys)}` +
`\n\nValues:\n${String(values)}`,
);
}
// Resolve all cache hits in the same micro-task as freshly loaded values.
resolveCacheHits(batch);
// Step through values, resolving or rejecting each Promise in the batch.
for (let i = 0; i < batch.callbacks.length; i++) {
const value = values[i];
if (value instanceof Error) {
batch.callbacks[i].reject(value);
} else {
batch.callbacks[i].resolve(value);
}
}
})
.catch(error => {
failedDispatch(loader, batch, error);
});
}
// Private: do not cache individual loads if the entire batch dispatch fails,
// but still reject each request so they do not hang.
function failedDispatch<K, V>(
loader: DataLoader<K, V, any>,
batch: Batch<K, V>,
error: Error,
) {
// Cache hits are resolved, even though the batch failed.
resolveCacheHits(batch);
for (let i = 0; i < batch.keys.length; i++) {
loader.clear(batch.keys[i]);
batch.callbacks[i].reject(error);
}
}
// Private: Resolves the Promises for any cache hits in this batch.
function resolveCacheHits(batch: Batch<any, any>) {
if (batch.cacheHits) {
for (let i = 0; i < batch.cacheHits.length; i++) {
batch.cacheHits[i]();
}
}
}
// Private: given the DataLoader's options, produce a valid max batch size.
function getValidMaxBatchSize(options: ?Options<any, any, any>): number {
const shouldBatch = !options || options.batch !== false;
if (!shouldBatch) {
return 1;
}
const maxBatchSize = options && options.maxBatchSize;
if (maxBatchSize === undefined) {
return Infinity;
}
if (typeof maxBatchSize !== 'number' || maxBatchSize < 1) {
throw new TypeError(
`maxBatchSize must be a positive number: ${(maxBatchSize: any)}`,
);
}
return maxBatchSize;
}
// Private
function getValidBatchScheduleFn(
options: ?Options<any, any, any>,
): (() => void) => void {
const batchScheduleFn = options && options.batchScheduleFn;
if (batchScheduleFn === undefined) {
return enqueuePostPromiseJob;
}
if (typeof batchScheduleFn !== 'function') {
throw new TypeError(
`batchScheduleFn must be a function: ${(batchScheduleFn: any)}`,
);
}
return batchScheduleFn;
}
// Private: given the DataLoader's options, produce a cache key function.
function getValidCacheKeyFn<K, C>(options: ?Options<K, any, C>): K => C {
const cacheKeyFn = options && options.cacheKeyFn;
if (cacheKeyFn === undefined) {
return (key => key: any);
}
if (typeof cacheKeyFn !== 'function') {
throw new TypeError(`cacheKeyFn must be a function: ${(cacheKeyFn: any)}`);
}
return cacheKeyFn;
}
// Private: given the DataLoader's options, produce a CacheMap to be used.
function getValidCacheMap<K, V, C>(
options: ?Options<K, V, C>,
): CacheMap<C, Promise<V>> | null {
const shouldCache = !options || options.cache !== false;
if (!shouldCache) {
return null;
}
const cacheMap = options && options.cacheMap;
if (cacheMap === undefined) {
return new Map();
}
if (cacheMap !== null) {
const cacheFunctions = ['get', 'set', 'delete', 'clear'];
const missingFunctions = cacheFunctions.filter(
fnName => cacheMap && typeof cacheMap[fnName] !== 'function',
);
if (missingFunctions.length !== 0) {
throw new TypeError(
'Custom cacheMap missing methods: ' + missingFunctions.join(', '),
);
}
}
return cacheMap;
}
function getValidName<K, V, C>(options: ?Options<K, V, C>): string | null {
if (options && options.name) {
return options.name;
}
return null;
}
// Private
function isArrayLike(x: mixed): boolean {
return (
typeof x === 'object' &&
x !== null &&
typeof x.length === 'number' &&
(x.length === 0 ||
(x.length > 0 && Object.prototype.hasOwnProperty.call(x, x.length - 1)))
);
}
module.exports = DataLoader;