1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.getLoader = void 0;
|
4 | const tslib_1 = require("tslib");
|
5 | const graphql_1 = require("graphql");
|
6 | const dataloader_1 = tslib_1.__importDefault(require("dataloader"));
|
7 | const delegate_1 = require("@graphql-tools/delegate");
|
8 | const utils_1 = require("@graphql-tools/utils");
|
9 | function createBatchFn(options) {
|
10 | var _a, _b;
|
11 | const argsFromKeys = (_a = options.argsFromKeys) !== null && _a !== void 0 ? _a : ((keys) => ({ ids: keys }));
|
12 | const fieldName = (_b = options.fieldName) !== null && _b !== void 0 ? _b : options.info.fieldName;
|
13 | const { valuesFromResults, lazyOptionsFn } = options;
|
14 | return async function batchFn(keys) {
|
15 | const results = await (0, delegate_1.delegateToSchema)({
|
16 | returnType: new graphql_1.GraphQLList((0, graphql_1.getNamedType)(options.info.returnType)),
|
17 | onLocatedError: originalError => {
|
18 | if (originalError.path == null) {
|
19 | return originalError;
|
20 | }
|
21 | const [pathFieldName, pathNumber] = originalError.path;
|
22 | if (pathFieldName !== fieldName) {
|
23 | return originalError;
|
24 | }
|
25 | const pathNumberType = typeof pathNumber;
|
26 | if (pathNumberType !== 'number') {
|
27 | return originalError;
|
28 | }
|
29 | return (0, utils_1.relocatedError)(originalError, originalError.path.slice(0, 0).concat(originalError.path.slice(2)));
|
30 | },
|
31 | args: argsFromKeys(keys),
|
32 | ...(lazyOptionsFn == null ? options : lazyOptionsFn(options)),
|
33 | });
|
34 | if (results instanceof Error) {
|
35 | return keys.map(() => results);
|
36 | }
|
37 | const values = valuesFromResults == null ? results : valuesFromResults(results, keys);
|
38 | return Array.isArray(values) ? values : keys.map(() => values);
|
39 | };
|
40 | }
|
41 | const getLoadersMap = (0, utils_1.memoize2)(function getLoadersMap(_context, _schema) {
|
42 | return new Map();
|
43 | });
|
44 | const GLOBAL_CONTEXT = {};
|
45 | const memoizedJsonStringify = (0, utils_1.memoize1)(function jsonStringify(value) {
|
46 | return JSON.stringify(value);
|
47 | });
|
48 | const memoizedPrint = (0, utils_1.memoize1)(graphql_1.print);
|
49 | function defaultCacheKeyFn(key) {
|
50 | if (typeof key === 'object') {
|
51 | return memoizedJsonStringify(key);
|
52 | }
|
53 | return key;
|
54 | }
|
55 | function getLoader(options) {
|
56 | const { schema, context, info, fieldName = info.fieldName, dataLoaderOptions, fieldNodes = info.fieldNodes, selectionSet = fieldNodes[0].selectionSet, } = options;
|
57 | const loaders = getLoadersMap(context !== null && context !== void 0 ? context : GLOBAL_CONTEXT, schema);
|
58 | let cacheKey = fieldName;
|
59 | if (selectionSet != null) {
|
60 | cacheKey += memoizedPrint(selectionSet);
|
61 | }
|
62 | let loader = loaders.get(cacheKey);
|
63 | if (loader === undefined) {
|
64 | const batchFn = createBatchFn(options);
|
65 | loader = new dataloader_1.default(batchFn, {
|
66 |
|
67 | cacheKeyFn: defaultCacheKeyFn,
|
68 | ...dataLoaderOptions,
|
69 | });
|
70 | loaders.set(cacheKey, loader);
|
71 | }
|
72 | return loader;
|
73 | }
|
74 | exports.getLoader = getLoader;
|