|
1 | | -import { getNamedType, GraphQLOutputType, GraphQLList, GraphQLSchema, FieldNode } from 'graphql'; |
| 1 | +import { GraphQLSchema, FieldNode } from 'graphql'; |
2 | 2 |
|
3 | 3 | import DataLoader from 'dataloader'; |
4 | 4 |
|
5 | | -import { delegateToSchema, SubschemaConfig } from '@graphql-tools/delegate'; |
6 | | -import { relocatedError } from '@graphql-tools/utils'; |
| 5 | +import { |
| 6 | + SubschemaConfig, |
| 7 | + Transformer, |
| 8 | + DelegationContext, |
| 9 | + validateRequest, |
| 10 | + getExecutor, |
| 11 | + getDelegatingOperation, |
| 12 | + createRequestFromInfo, |
| 13 | + getDelegationContext, |
| 14 | +} from '@graphql-tools/delegate'; |
| 15 | +import { ExecutionRequest, ExecutionResult } from '@graphql-tools/utils'; |
7 | 16 |
|
8 | 17 | import { BatchDelegateOptions } from './types'; |
9 | 18 |
|
10 | 19 | const cache1: WeakMap< |
11 | 20 | ReadonlyArray<FieldNode>, |
12 | | - WeakMap<GraphQLSchema | SubschemaConfig<any, any, any, any>, Record<string, DataLoader<any, any>>> |
| 21 | + WeakMap<GraphQLSchema | SubschemaConfig, Record<string, DataLoader<any, any>>> |
13 | 22 | > = new WeakMap(); |
14 | 23 |
|
15 | | -function createBatchFn<K = any>(options: BatchDelegateOptions) { |
| 24 | +function createBatchFn<K = any>( |
| 25 | + options: BatchDelegateOptions |
| 26 | +): ( |
| 27 | + keys: ReadonlyArray<K>, |
| 28 | + request: ExecutionRequest, |
| 29 | + delegationContext: DelegationContext<any> |
| 30 | +) => Promise<Array<ExecutionResult<Record<string, any>>>> { |
16 | 31 | const argsFromKeys = options.argsFromKeys ?? ((keys: ReadonlyArray<K>) => ({ ids: keys })); |
17 | | - const fieldName = options.fieldName ?? options.info.fieldName; |
18 | | - const { valuesFromResults, lazyOptionsFn } = options; |
19 | | - |
20 | | - return async (keys: ReadonlyArray<K>) => { |
21 | | - const results = await delegateToSchema({ |
22 | | - returnType: new GraphQLList(getNamedType(options.info.returnType) as GraphQLOutputType), |
23 | | - onLocatedError: originalError => { |
24 | | - if (originalError.path == null) { |
25 | | - return originalError; |
26 | | - } |
27 | | - |
28 | | - const [pathFieldName, pathNumber] = originalError.path; |
29 | | - |
30 | | - if (pathFieldName !== fieldName) { |
31 | | - return originalError; |
32 | | - } |
33 | | - const pathNumberType = typeof pathNumber; |
34 | | - if (pathNumberType !== 'number') { |
35 | | - return originalError; |
36 | | - } |
37 | | - |
38 | | - return relocatedError(originalError, originalError.path.slice(0, 0).concat(originalError.path.slice(2))); |
39 | | - }, |
| 32 | + |
| 33 | + const { validateRequest: shouldValidateRequest } = options; |
| 34 | + |
| 35 | + return async (keys: ReadonlyArray<K>, request: ExecutionRequest, delegationContext: DelegationContext<any>) => { |
| 36 | + const { fieldName, context, info } = delegationContext; |
| 37 | + |
| 38 | + const transformer = new Transformer({ |
| 39 | + ...delegationContext, |
40 | 40 | args: argsFromKeys(keys), |
41 | | - ...(lazyOptionsFn == null ? options : lazyOptionsFn(options)), |
42 | 41 | }); |
43 | 42 |
|
44 | | - if (results instanceof Error) { |
45 | | - return keys.map(() => results); |
| 43 | + const processedRequest = transformer.transformRequest(request); |
| 44 | + |
| 45 | + if (shouldValidateRequest) { |
| 46 | + validateRequest(delegationContext, processedRequest.document); |
46 | 47 | } |
47 | 48 |
|
48 | | - const values = valuesFromResults == null ? results : valuesFromResults(results, keys); |
| 49 | + const executor = getExecutor(delegationContext); |
| 50 | + |
| 51 | + const batchResult = (await executor({ |
| 52 | + ...processedRequest, |
| 53 | + context, |
| 54 | + info, |
| 55 | + })) as ExecutionResult; |
49 | 56 |
|
50 | | - return Array.isArray(values) ? values : keys.map(() => values); |
| 57 | + return splitResult(transformer.transformResult(batchResult), fieldName, keys.length); |
51 | 58 | }; |
52 | 59 | } |
53 | 60 |
|
54 | | -const cacheKeyFn = (key: any) => (typeof key === 'object' ? JSON.stringify(key) : key); |
55 | | - |
56 | | -export function getLoader<K = any, V = any, C = K>(options: BatchDelegateOptions<any>): DataLoader<K, V, C> { |
57 | | - const fieldName = options.fieldName ?? options.info.fieldName; |
58 | | - |
59 | | - let cache2: WeakMap<GraphQLSchema | SubschemaConfig, Record<string, DataLoader<K, V, C>>> | undefined = cache1.get( |
60 | | - options.info.fieldNodes |
61 | | - ); |
| 61 | +export function getLoader<K = any, C = K>(options: BatchDelegateOptions<any>): DataLoader<K, ExecutionResult, C> { |
| 62 | + const { |
| 63 | + info, |
| 64 | + operationName, |
| 65 | + operation = getDelegatingOperation(info.parentType, info.schema), |
| 66 | + fieldName = info.fieldName, |
| 67 | + returnType = info.returnType, |
| 68 | + selectionSet, |
| 69 | + fieldNodes, |
| 70 | + } = options; |
| 71 | + |
| 72 | + if (operation !== 'query' && operation !== 'mutation') { |
| 73 | + throw new Error(`Batch delegation not possible for operation '${operation}'.`); |
| 74 | + } |
62 | 75 |
|
63 | | - // Prevents the keys to be passed with the same structure |
64 | | - const dataLoaderOptions: DataLoader.Options<any, any, any> = { |
65 | | - cacheKeyFn, |
66 | | - ...options.dataLoaderOptions, |
67 | | - }; |
| 76 | + const request = createRequestFromInfo({ |
| 77 | + info, |
| 78 | + operation, |
| 79 | + fieldName, |
| 80 | + selectionSet, |
| 81 | + fieldNodes, |
| 82 | + operationName, |
| 83 | + }); |
| 84 | + |
| 85 | + const delegationContext = getDelegationContext({ |
| 86 | + request, |
| 87 | + ...options, |
| 88 | + operation, |
| 89 | + fieldName, |
| 90 | + returnType, |
| 91 | + }); |
| 92 | + |
| 93 | + let cache2 = cache1.get(options.info.fieldNodes); |
68 | 94 |
|
69 | 95 | if (cache2 === undefined) { |
70 | 96 | cache2 = new WeakMap(); |
71 | 97 | cache1.set(options.info.fieldNodes, cache2); |
72 | 98 | const loaders = Object.create(null); |
73 | 99 | cache2.set(options.schema, loaders); |
74 | 100 | const batchFn = createBatchFn(options); |
75 | | - const loader = new DataLoader<K, V, C>(keys => batchFn(keys), dataLoaderOptions); |
| 101 | + const loader = new DataLoader<K, ExecutionResult, C>( |
| 102 | + keys => batchFn(keys, request, delegationContext), |
| 103 | + options.dataLoaderOptions |
| 104 | + ); |
76 | 105 | loaders[fieldName] = loader; |
77 | 106 | return loader; |
78 | 107 | } |
79 | 108 |
|
80 | | - let loaders = cache2.get(options.schema); |
| 109 | + const loaders = cache2.get(options.schema); |
81 | 110 |
|
82 | 111 | if (loaders === undefined) { |
83 | | - loaders = Object.create(null) as Record<string, DataLoader<K, V, C>>; |
84 | | - cache2.set(options.schema, loaders); |
| 112 | + const newLoaders = Object.create(null); |
| 113 | + cache2.set(options.schema, newLoaders); |
85 | 114 | const batchFn = createBatchFn(options); |
86 | | - const loader = new DataLoader<K, V, C>(keys => batchFn(keys), dataLoaderOptions); |
87 | | - loaders[fieldName] = loader; |
| 115 | + const loader = new DataLoader<K, ExecutionResult, C>( |
| 116 | + keys => batchFn(keys, request, delegationContext), |
| 117 | + options.dataLoaderOptions |
| 118 | + ); |
| 119 | + newLoaders[fieldName] = loader; |
88 | 120 | return loader; |
89 | 121 | } |
90 | 122 |
|
91 | 123 | let loader = loaders[fieldName]; |
92 | 124 |
|
93 | 125 | if (loader === undefined) { |
94 | 126 | const batchFn = createBatchFn(options); |
95 | | - loader = new DataLoader<K, V, C>(keys => batchFn(keys), dataLoaderOptions); |
| 127 | + loader = new DataLoader<K, ExecutionResult, C>( |
| 128 | + keys => batchFn(keys, request, delegationContext), |
| 129 | + options.dataLoaderOptions |
| 130 | + ); |
96 | 131 | loaders[fieldName] = loader; |
97 | 132 | } |
98 | 133 |
|
99 | 134 | return loader; |
100 | 135 | } |
| 136 | + |
| 137 | +function splitResult(result: ExecutionResult, fieldName: string, numItems: number): Array<ExecutionResult> { |
| 138 | + const { data, errors } = result; |
| 139 | + const fieldData = data?.[fieldName]; |
| 140 | + |
| 141 | + if (fieldData === undefined) { |
| 142 | + if (errors === undefined) { |
| 143 | + return Array(numItems).fill({}); |
| 144 | + } |
| 145 | + |
| 146 | + return Array(numItems).fill({ errors }); |
| 147 | + } |
| 148 | + |
| 149 | + return fieldData.map((value: any) => ({ |
| 150 | + data: { |
| 151 | + [fieldName]: value, |
| 152 | + }, |
| 153 | + errors, |
| 154 | + })); |
| 155 | +} |
0 commit comments