@graphql-tools/stitching-directives 3.1.13 → 3.1.14-alpha-09de1d3da51074c5901decc7efd24f43af24378f

Sign up to get free protection for your applications and to get access to all the features.
Files changed (59) hide show
  1. package/CHANGELOG.md +929 -0
  2. package/dist/index.cjs +1196 -0
  3. package/dist/index.d.cts +58 -0
  4. package/dist/index.d.ts +58 -0
  5. package/dist/index.js +1193 -0
  6. package/package.json +32 -42
  7. package/cjs/defaultStitchingDirectiveOptions.js +0 -10
  8. package/cjs/extractVariables.js +0 -52
  9. package/cjs/federationToStitchingSDL.js +0 -116
  10. package/cjs/getSourcePaths.js +0 -25
  11. package/cjs/index.js +0 -6
  12. package/cjs/package.json +0 -1
  13. package/cjs/parseMergeArgsExpr.js +0 -77
  14. package/cjs/pathsFromSelectionSet.js +0 -29
  15. package/cjs/preparseMergeArgsExpr.js +0 -31
  16. package/cjs/properties.js +0 -70
  17. package/cjs/stitchingDirectives.js +0 -78
  18. package/cjs/stitchingDirectivesTransformer.js +0 -490
  19. package/cjs/stitchingDirectivesValidator.js +0 -119
  20. package/cjs/types.js +0 -0
  21. package/esm/defaultStitchingDirectiveOptions.js +0 -7
  22. package/esm/extractVariables.js +0 -48
  23. package/esm/federationToStitchingSDL.js +0 -112
  24. package/esm/getSourcePaths.js +0 -21
  25. package/esm/index.js +0 -3
  26. package/esm/parseMergeArgsExpr.js +0 -73
  27. package/esm/pathsFromSelectionSet.js +0 -25
  28. package/esm/preparseMergeArgsExpr.js +0 -27
  29. package/esm/properties.js +0 -63
  30. package/esm/stitchingDirectives.js +0 -74
  31. package/esm/stitchingDirectivesTransformer.js +0 -486
  32. package/esm/stitchingDirectivesValidator.js +0 -115
  33. package/esm/types.js +0 -0
  34. package/typings/defaultStitchingDirectiveOptions.d.cts +0 -2
  35. package/typings/defaultStitchingDirectiveOptions.d.ts +0 -2
  36. package/typings/extractVariables.d.cts +0 -7
  37. package/typings/extractVariables.d.ts +0 -7
  38. package/typings/federationToStitchingSDL.d.cts +0 -2
  39. package/typings/federationToStitchingSDL.d.ts +0 -2
  40. package/typings/getSourcePaths.d.cts +0 -3
  41. package/typings/getSourcePaths.d.ts +0 -3
  42. package/typings/index.d.cts +0 -3
  43. package/typings/index.d.ts +0 -3
  44. package/typings/parseMergeArgsExpr.d.cts +0 -3
  45. package/typings/parseMergeArgsExpr.d.ts +0 -3
  46. package/typings/pathsFromSelectionSet.d.cts +0 -2
  47. package/typings/pathsFromSelectionSet.d.ts +0 -2
  48. package/typings/preparseMergeArgsExpr.d.cts +0 -6
  49. package/typings/preparseMergeArgsExpr.d.ts +0 -6
  50. package/typings/properties.d.cts +0 -5
  51. package/typings/properties.d.ts +0 -5
  52. package/typings/stitchingDirectives.d.cts +0 -19
  53. package/typings/stitchingDirectives.d.ts +0 -19
  54. package/typings/stitchingDirectivesTransformer.d.cts +0 -3
  55. package/typings/stitchingDirectivesTransformer.d.ts +0 -3
  56. package/typings/stitchingDirectivesValidator.d.cts +0 -3
  57. package/typings/stitchingDirectivesValidator.d.ts +0 -3
  58. package/typings/types.d.cts +0 -35
  59. package/typings/types.d.ts +0 -35
@@ -1,490 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.stitchingDirectivesTransformer = void 0;
4
- const graphql_1 = require("graphql");
5
- const delegate_1 = require("@graphql-tools/delegate");
6
- const utils_1 = require("@graphql-tools/utils");
7
- const defaultStitchingDirectiveOptions_js_1 = require("./defaultStitchingDirectiveOptions.js");
8
- const parseMergeArgsExpr_js_1 = require("./parseMergeArgsExpr.js");
9
- const properties_js_1 = require("./properties.js");
10
- function stitchingDirectivesTransformer(options = {}) {
11
- const { keyDirectiveName, computedDirectiveName, mergeDirectiveName, canonicalDirectiveName, pathToDirectivesInExtensions, } = {
12
- ...defaultStitchingDirectiveOptions_js_1.defaultStitchingDirectiveOptions,
13
- ...options,
14
- };
15
- return (subschemaConfig) => {
16
- const newSubschemaConfig = (0, delegate_1.cloneSubschemaConfig)(subschemaConfig);
17
- const selectionSetsByType = Object.create(null);
18
- const computedFieldSelectionSets = Object.create(null);
19
- const mergedTypesResolversInfo = Object.create(null);
20
- const canonicalTypesInfo = Object.create(null);
21
- const selectionSetsByTypeAndEntryField = Object.create(null);
22
- const mergedTypesResolversInfoByEntryField = Object.create(null);
23
- const schema = subschemaConfig.schema;
24
- function setCanonicalDefinition(typeName, fieldName) {
25
- canonicalTypesInfo[typeName] = canonicalTypesInfo[typeName] || Object.create(null);
26
- if (fieldName) {
27
- const fields = canonicalTypesInfo[typeName].fields ?? Object.create(null);
28
- canonicalTypesInfo[typeName].fields = fields;
29
- fields[fieldName] = true;
30
- }
31
- else {
32
- canonicalTypesInfo[typeName].canonical = true;
33
- }
34
- }
35
- (0, utils_1.mapSchema)(schema, {
36
- [utils_1.MapperKind.OBJECT_TYPE]: type => {
37
- const keyDirective = (0, utils_1.getDirective)(schema, type, keyDirectiveName, pathToDirectivesInExtensions)?.[0];
38
- if (keyDirective != null) {
39
- const selectionSet = (0, utils_1.parseSelectionSet)(keyDirective['selectionSet'], {
40
- noLocation: true,
41
- });
42
- selectionSetsByType[type.name] = selectionSet;
43
- }
44
- const canonicalDirective = (0, utils_1.getDirective)(schema, type, canonicalDirectiveName, pathToDirectivesInExtensions)?.[0];
45
- if (canonicalDirective != null) {
46
- setCanonicalDefinition(type.name);
47
- }
48
- return undefined;
49
- },
50
- [utils_1.MapperKind.OBJECT_FIELD]: (fieldConfig, fieldName, typeName) => {
51
- const computedDirective = (0, utils_1.getDirective)(schema, fieldConfig, computedDirectiveName, pathToDirectivesInExtensions)?.[0];
52
- if (computedDirective != null) {
53
- const selectionSet = (0, utils_1.parseSelectionSet)(computedDirective['selectionSet'], {
54
- noLocation: true,
55
- });
56
- if (!computedFieldSelectionSets[typeName]) {
57
- computedFieldSelectionSets[typeName] = Object.create(null);
58
- }
59
- computedFieldSelectionSets[typeName][fieldName] = selectionSet;
60
- }
61
- const mergeDirective = (0, utils_1.getDirective)(schema, fieldConfig, mergeDirectiveName, pathToDirectivesInExtensions)?.[0];
62
- if (mergeDirective?.['keyField'] != null) {
63
- const mergeDirectiveKeyField = mergeDirective['keyField'];
64
- const selectionSet = (0, utils_1.parseSelectionSet)(`{ ${mergeDirectiveKeyField}}`, {
65
- noLocation: true,
66
- });
67
- const typeNames = mergeDirective['types'];
68
- const returnType = (0, graphql_1.getNamedType)(fieldConfig.type);
69
- forEachConcreteType(schema, returnType, typeNames, typeName => {
70
- if (typeNames == null || typeNames.includes(typeName)) {
71
- let existingEntryFieldMap = selectionSetsByTypeAndEntryField[typeName];
72
- if (existingEntryFieldMap == null) {
73
- existingEntryFieldMap = Object.create(null);
74
- selectionSetsByTypeAndEntryField[typeName] = existingEntryFieldMap;
75
- }
76
- let existingSelectionSet = existingEntryFieldMap[fieldName];
77
- if (existingSelectionSet == null) {
78
- existingSelectionSet = selectionSetsByType[typeName];
79
- }
80
- existingEntryFieldMap[fieldName] = existingSelectionSet
81
- ? mergeSelectionSets(existingSelectionSet, selectionSet)
82
- : selectionSet;
83
- }
84
- });
85
- }
86
- const canonicalDirective = (0, utils_1.getDirective)(schema, fieldConfig, canonicalDirectiveName, pathToDirectivesInExtensions)?.[0];
87
- if (canonicalDirective != null) {
88
- setCanonicalDefinition(typeName, fieldName);
89
- }
90
- return undefined;
91
- },
92
- [utils_1.MapperKind.INTERFACE_TYPE]: type => {
93
- const canonicalDirective = (0, utils_1.getDirective)(schema, type, canonicalDirectiveName, pathToDirectivesInExtensions)?.[0];
94
- if (canonicalDirective) {
95
- setCanonicalDefinition(type.name);
96
- }
97
- return undefined;
98
- },
99
- [utils_1.MapperKind.INTERFACE_FIELD]: (fieldConfig, fieldName, typeName) => {
100
- const canonicalDirective = (0, utils_1.getDirective)(schema, fieldConfig, canonicalDirectiveName, pathToDirectivesInExtensions)?.[0];
101
- if (canonicalDirective) {
102
- setCanonicalDefinition(typeName, fieldName);
103
- }
104
- return undefined;
105
- },
106
- [utils_1.MapperKind.INPUT_OBJECT_TYPE]: type => {
107
- const canonicalDirective = (0, utils_1.getDirective)(schema, type, canonicalDirectiveName, pathToDirectivesInExtensions)?.[0];
108
- if (canonicalDirective) {
109
- setCanonicalDefinition(type.name);
110
- }
111
- return undefined;
112
- },
113
- [utils_1.MapperKind.INPUT_OBJECT_FIELD]: (inputFieldConfig, fieldName, typeName) => {
114
- const canonicalDirective = (0, utils_1.getDirective)(schema, inputFieldConfig, canonicalDirectiveName, pathToDirectivesInExtensions)?.[0];
115
- if (canonicalDirective != null) {
116
- setCanonicalDefinition(typeName, fieldName);
117
- }
118
- return undefined;
119
- },
120
- [utils_1.MapperKind.UNION_TYPE]: type => {
121
- const canonicalDirective = (0, utils_1.getDirective)(schema, type, canonicalDirectiveName, pathToDirectivesInExtensions)?.[0];
122
- if (canonicalDirective != null) {
123
- setCanonicalDefinition(type.name);
124
- }
125
- return undefined;
126
- },
127
- [utils_1.MapperKind.ENUM_TYPE]: type => {
128
- const canonicalDirective = (0, utils_1.getDirective)(schema, type, canonicalDirectiveName, pathToDirectivesInExtensions)?.[0];
129
- if (canonicalDirective != null) {
130
- setCanonicalDefinition(type.name);
131
- }
132
- return undefined;
133
- },
134
- [utils_1.MapperKind.SCALAR_TYPE]: type => {
135
- const canonicalDirective = (0, utils_1.getDirective)(schema, type, canonicalDirectiveName, pathToDirectivesInExtensions)?.[0];
136
- if (canonicalDirective != null) {
137
- setCanonicalDefinition(type.name);
138
- }
139
- return undefined;
140
- },
141
- });
142
- if (subschemaConfig.merge) {
143
- for (const typeName in subschemaConfig.merge) {
144
- const mergedTypeConfig = subschemaConfig.merge[typeName];
145
- if (mergedTypeConfig.selectionSet) {
146
- const selectionSet = (0, utils_1.parseSelectionSet)(mergedTypeConfig.selectionSet, {
147
- noLocation: true,
148
- });
149
- if (selectionSet) {
150
- if (selectionSetsByType[typeName]) {
151
- selectionSetsByType[typeName] = mergeSelectionSets(selectionSetsByType[typeName], selectionSet);
152
- }
153
- else {
154
- selectionSetsByType[typeName] = selectionSet;
155
- }
156
- }
157
- }
158
- if (mergedTypeConfig.fields) {
159
- for (const fieldName in mergedTypeConfig.fields) {
160
- const fieldConfig = mergedTypeConfig.fields[fieldName];
161
- if (!fieldConfig.selectionSet)
162
- continue;
163
- const selectionSet = (0, utils_1.parseSelectionSet)(fieldConfig.selectionSet, { noLocation: true });
164
- if (selectionSet) {
165
- if (computedFieldSelectionSets[typeName]?.[fieldName]) {
166
- computedFieldSelectionSets[typeName][fieldName] = mergeSelectionSets(computedFieldSelectionSets[typeName][fieldName], selectionSet);
167
- }
168
- else {
169
- if (computedFieldSelectionSets[typeName] == null) {
170
- computedFieldSelectionSets[typeName] = Object.create(null);
171
- }
172
- computedFieldSelectionSets[typeName][fieldName] = selectionSet;
173
- }
174
- }
175
- }
176
- }
177
- }
178
- }
179
- const allSelectionSetsByType = Object.create(null);
180
- for (const typeName in selectionSetsByType) {
181
- allSelectionSetsByType[typeName] = allSelectionSetsByType[typeName] || [];
182
- const selectionSet = selectionSetsByType[typeName];
183
- allSelectionSetsByType[typeName].push(selectionSet);
184
- }
185
- for (const typeName in computedFieldSelectionSets) {
186
- const selectionSets = computedFieldSelectionSets[typeName];
187
- for (const i in selectionSets) {
188
- allSelectionSetsByType[typeName] = allSelectionSetsByType[typeName] || [];
189
- const selectionSet = selectionSets[i];
190
- allSelectionSetsByType[typeName].push(selectionSet);
191
- }
192
- }
193
- (0, utils_1.mapSchema)(schema, {
194
- [utils_1.MapperKind.OBJECT_FIELD]: function objectFieldMapper(fieldConfig, fieldName) {
195
- const mergeDirective = (0, utils_1.getDirective)(schema, fieldConfig, mergeDirectiveName, pathToDirectivesInExtensions)?.[0];
196
- if (mergeDirective != null) {
197
- const returnType = (0, graphql_1.getNullableType)(fieldConfig.type);
198
- const returnsList = (0, graphql_1.isListType)(returnType);
199
- const namedType = (0, graphql_1.getNamedType)(returnType);
200
- let mergeArgsExpr = mergeDirective['argsExpr'];
201
- if (mergeArgsExpr == null) {
202
- const key = mergeDirective['key'];
203
- const keyField = mergeDirective['keyField'];
204
- const keyExpr = key != null ? buildKeyExpr(key) : keyField != null ? `$key.${keyField}` : '$key';
205
- const keyArg = mergeDirective['keyArg'];
206
- const argNames = keyArg == null ? [Object.keys(fieldConfig.args ?? {})[0]] : keyArg.split('.');
207
- const lastArgName = argNames.pop();
208
- mergeArgsExpr = returnsList
209
- ? `${lastArgName}: [[${keyExpr}]]`
210
- : `${lastArgName}: ${keyExpr}`;
211
- for (const argName of argNames.reverse()) {
212
- mergeArgsExpr = `${argName}: { ${mergeArgsExpr} }`;
213
- }
214
- }
215
- const typeNames = mergeDirective['types'];
216
- forEachConcreteTypeName(namedType, schema, typeNames, function generateResolveInfo(typeName) {
217
- const mergedSelectionSets = [];
218
- if (allSelectionSetsByType[typeName]) {
219
- mergedSelectionSets.push(...allSelectionSetsByType[typeName]);
220
- }
221
- if (selectionSetsByTypeAndEntryField[typeName]?.[fieldName]) {
222
- mergedSelectionSets.push(selectionSetsByTypeAndEntryField[typeName][fieldName]);
223
- }
224
- const parsedMergeArgsExpr = (0, parseMergeArgsExpr_js_1.parseMergeArgsExpr)(mergeArgsExpr, allSelectionSetsByType[typeName] == null
225
- ? undefined
226
- : mergeSelectionSets(...mergedSelectionSets));
227
- const additionalArgs = mergeDirective['additionalArgs'];
228
- if (additionalArgs != null) {
229
- parsedMergeArgsExpr.args = (0, utils_1.mergeDeep)([
230
- parsedMergeArgsExpr.args,
231
- (0, graphql_1.valueFromASTUntyped)((0, graphql_1.parseValue)(`{ ${additionalArgs} }`, { noLocation: true })),
232
- ]);
233
- }
234
- if (selectionSetsByTypeAndEntryField[typeName]?.[fieldName] != null) {
235
- const typeConfigByField = (mergedTypesResolversInfoByEntryField[typeName] ||=
236
- Object.create(null));
237
- typeConfigByField[fieldName] = {
238
- fieldName,
239
- returnsList,
240
- ...parsedMergeArgsExpr,
241
- };
242
- }
243
- else {
244
- mergedTypesResolversInfo[typeName] = {
245
- fieldName,
246
- returnsList,
247
- ...parsedMergeArgsExpr,
248
- };
249
- }
250
- });
251
- }
252
- return undefined;
253
- },
254
- });
255
- for (const typeName in selectionSetsByType) {
256
- const selectionSet = selectionSetsByType[typeName];
257
- const mergeConfig = newSubschemaConfig.merge ?? Object.create(null);
258
- newSubschemaConfig.merge = mergeConfig;
259
- if (mergeConfig[typeName] == null) {
260
- newSubschemaConfig.merge[typeName] = Object.create(null);
261
- }
262
- const mergeTypeConfig = mergeConfig[typeName];
263
- mergeTypeConfig.selectionSet = (0, graphql_1.print)(selectionSet);
264
- }
265
- for (const typeName in computedFieldSelectionSets) {
266
- const selectionSets = computedFieldSelectionSets[typeName];
267
- const mergeConfig = newSubschemaConfig.merge ?? Object.create(null);
268
- newSubschemaConfig.merge = mergeConfig;
269
- if (mergeConfig[typeName] == null) {
270
- mergeConfig[typeName] = Object.create(null);
271
- }
272
- const mergeTypeConfig = newSubschemaConfig.merge[typeName];
273
- const mergeTypeConfigFields = mergeTypeConfig.fields ?? Object.create(null);
274
- mergeTypeConfig.fields = mergeTypeConfigFields;
275
- for (const fieldName in selectionSets) {
276
- const selectionSet = selectionSets[fieldName];
277
- const fieldConfig = mergeTypeConfigFields[fieldName] ?? Object.create(null);
278
- mergeTypeConfigFields[fieldName] = fieldConfig;
279
- fieldConfig.selectionSet = (0, graphql_1.print)(selectionSet);
280
- fieldConfig.computed = true;
281
- }
282
- }
283
- for (const typeName in mergedTypesResolversInfo) {
284
- const mergedTypeResolverInfo = mergedTypesResolversInfo[typeName];
285
- const mergeConfig = newSubschemaConfig.merge ?? Object.create(null);
286
- newSubschemaConfig.merge = mergeConfig;
287
- if (newSubschemaConfig.merge[typeName] == null) {
288
- newSubschemaConfig.merge[typeName] = Object.create(null);
289
- }
290
- const mergeTypeConfig = newSubschemaConfig.merge[typeName];
291
- mergeTypeConfig.fieldName = mergedTypeResolverInfo.fieldName;
292
- if (mergedTypeResolverInfo.returnsList) {
293
- mergeTypeConfig.key = generateKeyFn(mergedTypeResolverInfo);
294
- mergeTypeConfig.argsFromKeys = generateArgsFromKeysFn(mergedTypeResolverInfo);
295
- }
296
- else {
297
- mergeTypeConfig.args = generateArgsFn(mergedTypeResolverInfo);
298
- }
299
- }
300
- for (const typeName in canonicalTypesInfo) {
301
- const canonicalTypeInfo = canonicalTypesInfo[typeName];
302
- const mergeConfig = newSubschemaConfig.merge ?? Object.create(null);
303
- newSubschemaConfig.merge = mergeConfig;
304
- if (newSubschemaConfig.merge[typeName] == null) {
305
- newSubschemaConfig.merge[typeName] = Object.create(null);
306
- }
307
- const mergeTypeConfig = newSubschemaConfig.merge[typeName];
308
- if (canonicalTypeInfo.canonical) {
309
- mergeTypeConfig.canonical = true;
310
- }
311
- if (canonicalTypeInfo.fields) {
312
- const mergeTypeConfigFields = mergeTypeConfig.fields ?? Object.create(null);
313
- mergeTypeConfig.fields = mergeTypeConfigFields;
314
- for (const fieldName in canonicalTypeInfo.fields) {
315
- if (mergeTypeConfigFields[fieldName] == null) {
316
- mergeTypeConfigFields[fieldName] = Object.create(null);
317
- }
318
- mergeTypeConfigFields[fieldName].canonical = true;
319
- }
320
- }
321
- }
322
- for (const typeName in mergedTypesResolversInfoByEntryField) {
323
- const entryPoints = [];
324
- const existingMergeConfig = newSubschemaConfig.merge?.[typeName];
325
- const newMergeConfig = (newSubschemaConfig.merge ||= Object.create(null));
326
- if (existingMergeConfig) {
327
- const { fields, canonical, ...baseEntryPoint } = existingMergeConfig;
328
- newMergeConfig[typeName] = {
329
- fields,
330
- canonical,
331
- entryPoints,
332
- };
333
- entryPoints.push(baseEntryPoint);
334
- }
335
- else {
336
- newMergeConfig[typeName] = {
337
- entryPoints,
338
- };
339
- }
340
- for (const fieldName in mergedTypesResolversInfoByEntryField[typeName]) {
341
- const mergedTypeResolverInfo = mergedTypesResolversInfoByEntryField[typeName][fieldName];
342
- const newEntryPoint = {
343
- fieldName,
344
- selectionSet: (0, graphql_1.print)(selectionSetsByTypeAndEntryField[typeName][fieldName]),
345
- };
346
- if (mergedTypeResolverInfo.returnsList) {
347
- newEntryPoint.key = generateKeyFn(mergedTypeResolverInfo);
348
- newEntryPoint.argsFromKeys = generateArgsFromKeysFn(mergedTypeResolverInfo);
349
- }
350
- else {
351
- newEntryPoint.args = generateArgsFn(mergedTypeResolverInfo);
352
- }
353
- entryPoints.push(newEntryPoint);
354
- }
355
- if (entryPoints.length === 1) {
356
- const [entryPoint] = entryPoints;
357
- const { fields, canonical } = newMergeConfig[typeName];
358
- newMergeConfig[typeName] = {
359
- ...entryPoint,
360
- fields,
361
- canonical,
362
- };
363
- }
364
- }
365
- return newSubschemaConfig;
366
- };
367
- }
368
- exports.stitchingDirectivesTransformer = stitchingDirectivesTransformer;
369
- function forEachConcreteType(schema, type, typeNames, fn) {
370
- if ((0, graphql_1.isInterfaceType)(type)) {
371
- for (const typeName of (0, utils_1.getImplementingTypes)(type.name, schema)) {
372
- if (typeNames == null || typeNames.includes(typeName)) {
373
- fn(typeName);
374
- }
375
- }
376
- }
377
- else if ((0, graphql_1.isUnionType)(type)) {
378
- for (const { name: typeName } of type.getTypes()) {
379
- if (typeNames == null || typeNames.includes(typeName)) {
380
- fn(typeName);
381
- }
382
- }
383
- }
384
- else if ((0, graphql_1.isObjectType)(type)) {
385
- fn(type.name);
386
- }
387
- }
388
- function generateKeyFn(mergedTypeResolverInfo) {
389
- return function keyFn(originalResult) {
390
- return (0, properties_js_1.getProperties)(originalResult, mergedTypeResolverInfo.usedProperties);
391
- };
392
- }
393
- function generateArgsFromKeysFn(mergedTypeResolverInfo) {
394
- const { expansions, args } = mergedTypeResolverInfo;
395
- return function generateArgsFromKeys(keys) {
396
- const newArgs = (0, utils_1.mergeDeep)([{}, args]);
397
- if (expansions) {
398
- for (const expansion of expansions) {
399
- const mappingInstructions = expansion.mappingInstructions;
400
- const expanded = [];
401
- for (const key of keys) {
402
- let newValue = (0, utils_1.mergeDeep)([{}, expansion.valuePath]);
403
- for (const { destinationPath, sourcePath } of mappingInstructions) {
404
- if (destinationPath.length) {
405
- (0, properties_js_1.addProperty)(newValue, destinationPath, (0, properties_js_1.getProperty)(key, sourcePath));
406
- }
407
- else {
408
- newValue = (0, properties_js_1.getProperty)(key, sourcePath);
409
- }
410
- }
411
- expanded.push(newValue);
412
- }
413
- (0, properties_js_1.addProperty)(newArgs, expansion.valuePath, expanded);
414
- }
415
- }
416
- return newArgs;
417
- };
418
- }
419
- function generateArgsFn(mergedTypeResolverInfo) {
420
- const { mappingInstructions, args, usedProperties } = mergedTypeResolverInfo;
421
- return function generateArgs(originalResult) {
422
- const newArgs = (0, utils_1.mergeDeep)([{}, args]);
423
- const filteredResult = (0, properties_js_1.getProperties)(originalResult, usedProperties);
424
- if (mappingInstructions) {
425
- for (const mappingInstruction of mappingInstructions) {
426
- const { destinationPath, sourcePath } = mappingInstruction;
427
- (0, properties_js_1.addProperty)(newArgs, destinationPath, (0, properties_js_1.getProperty)(filteredResult, sourcePath));
428
- }
429
- }
430
- return newArgs;
431
- };
432
- }
433
- function buildKeyExpr(key) {
434
- let mergedObject = {};
435
- for (const keyDef of key) {
436
- let [aliasOrKeyPath, keyPath] = keyDef.split(':');
437
- let aliasPath;
438
- if (keyPath == null) {
439
- keyPath = aliasPath = aliasOrKeyPath;
440
- }
441
- else {
442
- aliasPath = aliasOrKeyPath;
443
- }
444
- const aliasParts = aliasPath.split('.');
445
- const lastAliasPart = aliasParts.pop();
446
- if (lastAliasPart == null) {
447
- throw new Error(`Key "${key}" is invalid, no path provided.`);
448
- }
449
- let object = { [lastAliasPart]: `$key.${keyPath}` };
450
- for (const aliasPart of aliasParts.reverse()) {
451
- object = { [aliasPart]: object };
452
- }
453
- mergedObject = (0, utils_1.mergeDeep)([mergedObject, object]);
454
- }
455
- return JSON.stringify(mergedObject).replace(/"/g, '');
456
- }
457
- function mergeSelectionSets(...selectionSets) {
458
- const normalizedSelections = Object.create(null);
459
- for (const selectionSet of selectionSets) {
460
- for (const selection of selectionSet.selections) {
461
- const normalizedSelection = (0, graphql_1.print)(selection);
462
- normalizedSelections[normalizedSelection] = selection;
463
- }
464
- }
465
- const newSelectionSet = {
466
- kind: graphql_1.Kind.SELECTION_SET,
467
- selections: Object.values(normalizedSelections),
468
- };
469
- return newSelectionSet;
470
- }
471
- function forEachConcreteTypeName(returnType, schema, typeNames, fn) {
472
- if ((0, graphql_1.isInterfaceType)(returnType)) {
473
- for (const typeName of (0, utils_1.getImplementingTypes)(returnType.name, schema)) {
474
- if (typeNames == null || typeNames.includes(typeName)) {
475
- fn(typeName);
476
- }
477
- }
478
- }
479
- else if ((0, graphql_1.isUnionType)(returnType)) {
480
- for (const type of returnType.getTypes()) {
481
- if (typeNames == null || typeNames.includes(type.name)) {
482
- fn(type.name);
483
- }
484
- }
485
- }
486
- else if ((0, graphql_1.isObjectType)(returnType) &&
487
- (typeNames == null || typeNames.includes(returnType.name))) {
488
- fn(returnType.name);
489
- }
490
- }
@@ -1,119 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.stitchingDirectivesValidator = void 0;
4
- const graphql_1 = require("graphql");
5
- const utils_1 = require("@graphql-tools/utils");
6
- const defaultStitchingDirectiveOptions_js_1 = require("./defaultStitchingDirectiveOptions.js");
7
- const parseMergeArgsExpr_js_1 = require("./parseMergeArgsExpr.js");
8
- const dottedNameRegEx = /^[_A-Za-z][_0-9A-Za-z]*(.[_A-Za-z][_0-9A-Za-z]*)*$/;
9
- function stitchingDirectivesValidator(options = {}) {
10
- const { keyDirectiveName, computedDirectiveName, mergeDirectiveName, pathToDirectivesInExtensions, } = {
11
- ...defaultStitchingDirectiveOptions_js_1.defaultStitchingDirectiveOptions,
12
- ...options,
13
- };
14
- return (schema) => {
15
- const queryTypeName = schema.getQueryType()?.name;
16
- (0, utils_1.mapSchema)(schema, {
17
- [utils_1.MapperKind.OBJECT_TYPE]: type => {
18
- const keyDirective = (0, utils_1.getDirective)(schema, type, keyDirectiveName, pathToDirectivesInExtensions)?.[0];
19
- if (keyDirective != null) {
20
- (0, utils_1.parseSelectionSet)(keyDirective['selectionSet']);
21
- }
22
- return undefined;
23
- },
24
- [utils_1.MapperKind.OBJECT_FIELD]: (fieldConfig, _fieldName, typeName) => {
25
- const computedDirective = (0, utils_1.getDirective)(schema, fieldConfig, computedDirectiveName, pathToDirectivesInExtensions)?.[0];
26
- if (computedDirective != null) {
27
- (0, utils_1.parseSelectionSet)(computedDirective['selectionSet']);
28
- }
29
- const mergeDirective = (0, utils_1.getDirective)(schema, fieldConfig, mergeDirectiveName, pathToDirectivesInExtensions)?.[0];
30
- if (mergeDirective != null) {
31
- if (typeName !== queryTypeName) {
32
- throw new Error('@merge directive may be used only for root fields of the root Query type.');
33
- }
34
- let returnType = (0, graphql_1.getNullableType)(fieldConfig.type);
35
- if ((0, graphql_1.isListType)(returnType)) {
36
- returnType = (0, graphql_1.getNullableType)(returnType.ofType);
37
- }
38
- if (!(0, graphql_1.isNamedType)(returnType)) {
39
- throw new Error('@merge directive must be used on a field that returns an object or a list of objects.');
40
- }
41
- const mergeArgsExpr = mergeDirective['argsExpr'];
42
- if (mergeArgsExpr != null) {
43
- (0, parseMergeArgsExpr_js_1.parseMergeArgsExpr)(mergeArgsExpr);
44
- }
45
- const args = Object.keys(fieldConfig.args ?? {});
46
- const keyArg = mergeDirective['keyArg'];
47
- if (keyArg == null) {
48
- if (!mergeArgsExpr && args.length !== 1) {
49
- throw new Error('Cannot use @merge directive without `keyArg` argument if resolver takes more than one argument.');
50
- }
51
- }
52
- else if (!keyArg.match(dottedNameRegEx)) {
53
- throw new Error('`keyArg` argument for @merge directive must be a set of valid GraphQL SDL names separated by periods.');
54
- // TODO: ideally we should check that the arg exists for the resolver
55
- }
56
- const keyField = mergeDirective['keyField'];
57
- if (keyField != null && !keyField.match(dottedNameRegEx)) {
58
- throw new Error('`keyField` argument for @merge directive must be a set of valid GraphQL SDL names separated by periods.');
59
- // TODO: ideally we should check that it is part of the key
60
- }
61
- const key = mergeDirective['key'];
62
- if (key != null) {
63
- if (keyField != null) {
64
- throw new Error('Cannot use @merge directive with both `keyField` and `key` arguments.');
65
- }
66
- for (const keyDef of key) {
67
- let [aliasOrKeyPath, keyPath] = keyDef.split(':');
68
- let aliasPath;
69
- if (keyPath == null) {
70
- keyPath = aliasPath = aliasOrKeyPath;
71
- }
72
- else {
73
- aliasPath = aliasOrKeyPath;
74
- }
75
- if (keyPath != null && !keyPath.match(dottedNameRegEx)) {
76
- throw new Error('Each partial key within the `key` argument for @merge directive must be a set of valid GraphQL SDL names separated by periods.');
77
- // TODO: ideally we should check that it is part of the key
78
- }
79
- if (aliasPath != null && !aliasOrKeyPath.match(dottedNameRegEx)) {
80
- throw new Error('Each alias within the `key` argument for @merge directive must be a set of valid GraphQL SDL names separated by periods.');
81
- // TODO: ideally we should check that the arg exists within the resolver
82
- }
83
- }
84
- }
85
- const additionalArgs = mergeDirective['additionalArgs'];
86
- if (additionalArgs != null) {
87
- (0, graphql_1.parseValue)(`{ ${additionalArgs} }`, { noLocation: true });
88
- }
89
- if (mergeArgsExpr != null && (keyArg != null || additionalArgs != null)) {
90
- throw new Error('Cannot use @merge directive with both `argsExpr` argument and any additional argument.');
91
- }
92
- if (!(0, graphql_1.isInterfaceType)(returnType) &&
93
- !(0, graphql_1.isUnionType)(returnType) &&
94
- !(0, graphql_1.isObjectType)(returnType)) {
95
- throw new Error('@merge directive may be used only with resolver that return an object, interface, or union.');
96
- }
97
- const typeNames = mergeDirective['types'];
98
- if (typeNames != null) {
99
- if (!(0, graphql_1.isAbstractType)(returnType)) {
100
- throw new Error('Types argument can only be used with a field that returns an abstract type.');
101
- }
102
- const implementingTypes = (0, graphql_1.isInterfaceType)(returnType)
103
- ? (0, utils_1.getImplementingTypes)(returnType.name, schema).map(typeName => schema.getType(typeName))
104
- : returnType.getTypes();
105
- const implementingTypeNames = implementingTypes.map(type => type?.name).filter(utils_1.isSome);
106
- for (const typeName of typeNames) {
107
- if (!implementingTypeNames.includes(typeName)) {
108
- throw new Error(`Types argument can only include only type names that implement the field return type's abstract type.`);
109
- }
110
- }
111
- }
112
- }
113
- return undefined;
114
- },
115
- });
116
- return schema;
117
- };
118
- }
119
- exports.stitchingDirectivesValidator = stitchingDirectivesValidator;
package/cjs/types.js DELETED
File without changes
@@ -1,7 +0,0 @@
1
- export const defaultStitchingDirectiveOptions = {
2
- keyDirectiveName: 'key',
3
- computedDirectiveName: 'computed',
4
- canonicalDirectiveName: 'canonical',
5
- mergeDirectiveName: 'merge',
6
- pathToDirectivesInExtensions: ['directives'],
7
- };
@@ -1,48 +0,0 @@
1
- import { Kind, visit } from 'graphql';
2
- export function extractVariables(inputValue) {
3
- const path = [];
4
- const variablePaths = Object.create(null);
5
- const keyPathVisitor = {
6
- enter: (_node, key) => {
7
- if (typeof key === 'number') {
8
- path.push(key);
9
- }
10
- },
11
- leave: (_node, key) => {
12
- if (typeof key === 'number') {
13
- path.pop();
14
- }
15
- },
16
- };
17
- const fieldPathVisitor = {
18
- enter: (node) => {
19
- path.push(node.name.value);
20
- },
21
- leave: () => {
22
- path.pop();
23
- },
24
- };
25
- const variableVisitor = {
26
- enter: (node, key) => {
27
- if (typeof key === 'number') {
28
- variablePaths[node.name.value] = path.concat([key]);
29
- }
30
- else {
31
- variablePaths[node.name.value] = path.slice();
32
- }
33
- return {
34
- kind: Kind.NULL,
35
- };
36
- },
37
- };
38
- const newInputValue = visit(inputValue, {
39
- [Kind.OBJECT]: keyPathVisitor,
40
- [Kind.LIST]: keyPathVisitor,
41
- [Kind.OBJECT_FIELD]: fieldPathVisitor,
42
- [Kind.VARIABLE]: variableVisitor,
43
- });
44
- return {
45
- inputValue: newInputValue,
46
- variablePaths,
47
- };
48
- }