UNPKG

4.53 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, '__esModule', { value: true });
4
5function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
6
7const graphql = require('graphql');
8const DataLoader = _interopDefault(require('dataloader'));
9const delegate = require('@graphql-tools/delegate');
10const utils = require('@graphql-tools/utils');
11
12const cache1 = new WeakMap();
13function createBatchFn(options) {
14 var _a, _b;
15 const argsFromKeys = (_a = options.argsFromKeys) !== null && _a !== void 0 ? _a : ((keys) => ({ ids: keys }));
16 const fieldName = (_b = options.fieldName) !== null && _b !== void 0 ? _b : options.info.fieldName;
17 const { valuesFromResults, lazyOptionsFn } = options;
18 return async (keys) => {
19 const results = await delegate.delegateToSchema({
20 returnType: new graphql.GraphQLList(graphql.getNamedType(options.info.returnType)),
21 onLocatedError: originalError => {
22 if (originalError.path == null) {
23 return originalError;
24 }
25 const [pathFieldName, pathNumber] = originalError.path;
26 if (pathFieldName !== fieldName) {
27 return originalError;
28 }
29 const pathNumberType = typeof pathNumber;
30 if (pathNumberType !== 'number') {
31 return originalError;
32 }
33 return utils.relocatedError(originalError, originalError.path.slice(0, 0).concat(originalError.path.slice(2)));
34 },
35 args: argsFromKeys(keys),
36 ...(lazyOptionsFn == null ? options : lazyOptionsFn(options)),
37 });
38 if (results instanceof Error) {
39 return keys.map(() => results);
40 }
41 const values = valuesFromResults == null ? results : valuesFromResults(results, keys);
42 return Array.isArray(values) ? values : keys.map(() => values);
43 };
44}
45const cacheKeyFn = (key) => (typeof key === 'object' ? JSON.stringify(key) : key);
46function getLoader(options) {
47 var _a;
48 const fieldName = (_a = options.fieldName) !== null && _a !== void 0 ? _a : options.info.fieldName;
49 let cache2 = cache1.get(options.info.fieldNodes);
50 // Prevents the keys to be passed with the same structure
51 const dataLoaderOptions = {
52 cacheKeyFn,
53 ...options.dataLoaderOptions,
54 };
55 if (cache2 === undefined) {
56 cache2 = new WeakMap();
57 cache1.set(options.info.fieldNodes, cache2);
58 const loaders = Object.create(null);
59 cache2.set(options.schema, loaders);
60 const batchFn = createBatchFn(options);
61 const loader = new DataLoader(keys => batchFn(keys), dataLoaderOptions);
62 loaders[fieldName] = loader;
63 return loader;
64 }
65 let loaders = cache2.get(options.schema);
66 if (loaders === undefined) {
67 loaders = Object.create(null);
68 cache2.set(options.schema, loaders);
69 const batchFn = createBatchFn(options);
70 const loader = new DataLoader(keys => batchFn(keys), dataLoaderOptions);
71 loaders[fieldName] = loader;
72 return loader;
73 }
74 let loader = loaders[fieldName];
75 if (loader === undefined) {
76 const batchFn = createBatchFn(options);
77 loader = new DataLoader(keys => batchFn(keys), dataLoaderOptions);
78 loaders[fieldName] = loader;
79 }
80 return loader;
81}
82
83function batchDelegateToSchema(options) {
84 const key = options.key;
85 if (key == null) {
86 return null;
87 }
88 else if (Array.isArray(key) && !key.length) {
89 return [];
90 }
91 const loader = getLoader(options);
92 return Array.isArray(key) ? loader.loadMany(key) : loader.load(key);
93}
94
95function createBatchDelegateFn(optionsOrArgsFromKeys, lazyOptionsFn, dataLoaderOptions, valuesFromResults) {
96 return typeof optionsOrArgsFromKeys === 'function'
97 ? createBatchDelegateFnImpl({
98 argsFromKeys: optionsOrArgsFromKeys,
99 lazyOptionsFn,
100 dataLoaderOptions,
101 valuesFromResults,
102 })
103 : createBatchDelegateFnImpl(optionsOrArgsFromKeys);
104}
105function createBatchDelegateFnImpl(options) {
106 return batchDelegateOptions => {
107 const loader = getLoader({
108 ...options,
109 ...batchDelegateOptions,
110 });
111 return loader.load(batchDelegateOptions.key);
112 };
113}
114
115exports.batchDelegateToSchema = batchDelegateToSchema;
116exports.createBatchDelegateFn = createBatchDelegateFn;