@graphql-tools/wrap 8.4.21 → 8.5.0-alpha-6c480b2d.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (120) hide show
  1. package/cjs/generateProxyingResolvers.js +74 -0
  2. package/cjs/index.js +12 -0
  3. package/cjs/introspect.js +30 -0
  4. package/cjs/package.json +1 -0
  5. package/cjs/transforms/ExtractField.js +51 -0
  6. package/cjs/transforms/FilterInputObjectFields.js +16 -0
  7. package/cjs/transforms/FilterInterfaceFields.js +13 -0
  8. package/cjs/transforms/FilterObjectFieldDirectives.js +33 -0
  9. package/cjs/transforms/FilterObjectFields.js +13 -0
  10. package/cjs/transforms/FilterRootFields.js +18 -0
  11. package/cjs/transforms/FilterTypes.js +19 -0
  12. package/cjs/transforms/HoistField.js +176 -0
  13. package/cjs/transforms/MapFields.js +61 -0
  14. package/cjs/transforms/MapLeafValues.js +111 -0
  15. package/cjs/transforms/PruneSchema.js +12 -0
  16. package/cjs/transforms/RemoveObjectFieldDeprecations.js +25 -0
  17. package/cjs/transforms/RemoveObjectFieldDirectives.js +16 -0
  18. package/cjs/transforms/RemoveObjectFieldsWithDeprecation.js +19 -0
  19. package/cjs/transforms/RemoveObjectFieldsWithDirective.js +19 -0
  20. package/cjs/transforms/RenameInputObjectFields.js +57 -0
  21. package/cjs/transforms/RenameInterfaceFields.js +19 -0
  22. package/cjs/transforms/RenameObjectFieldArguments.js +72 -0
  23. package/cjs/transforms/RenameObjectFields.js +19 -0
  24. package/cjs/transforms/RenameRootFields.js +16 -0
  25. package/cjs/transforms/RenameRootTypes.js +57 -0
  26. package/cjs/transforms/RenameTypes.js +69 -0
  27. package/cjs/transforms/TransformCompositeFields.js +143 -0
  28. package/cjs/transforms/TransformEnumValues.js +55 -0
  29. package/cjs/transforms/TransformInputObjectFields.js +160 -0
  30. package/cjs/transforms/TransformInterfaceFields.js +35 -0
  31. package/cjs/transforms/TransformObjectFields.js +35 -0
  32. package/cjs/transforms/TransformQuery.js +89 -0
  33. package/cjs/transforms/TransformRootFields.js +41 -0
  34. package/cjs/transforms/WrapFields.js +220 -0
  35. package/cjs/transforms/WrapQuery.js +62 -0
  36. package/cjs/transforms/WrapType.js +19 -0
  37. package/cjs/transforms/index.js +71 -0
  38. package/cjs/types.js +2 -0
  39. package/cjs/wrapSchema.js +58 -0
  40. package/esm/generateProxyingResolvers.js +69 -0
  41. package/esm/index.js +5 -0
  42. package/esm/introspect.js +26 -0
  43. package/esm/transforms/ExtractField.js +48 -0
  44. package/esm/transforms/FilterInputObjectFields.js +12 -0
  45. package/esm/transforms/FilterInterfaceFields.js +9 -0
  46. package/esm/transforms/FilterObjectFieldDirectives.js +29 -0
  47. package/esm/transforms/FilterObjectFields.js +9 -0
  48. package/esm/transforms/FilterRootFields.js +14 -0
  49. package/esm/transforms/FilterTypes.js +16 -0
  50. package/esm/transforms/HoistField.js +168 -0
  51. package/esm/transforms/MapFields.js +57 -0
  52. package/esm/transforms/MapLeafValues.js +108 -0
  53. package/esm/transforms/PruneSchema.js +9 -0
  54. package/esm/transforms/RemoveObjectFieldDeprecations.js +21 -0
  55. package/esm/transforms/RemoveObjectFieldDirectives.js +12 -0
  56. package/esm/transforms/RemoveObjectFieldsWithDeprecation.js +15 -0
  57. package/esm/transforms/RemoveObjectFieldsWithDirective.js +15 -0
  58. package/esm/transforms/RenameInputObjectFields.js +53 -0
  59. package/esm/transforms/RenameInterfaceFields.js +15 -0
  60. package/esm/transforms/RenameObjectFieldArguments.js +68 -0
  61. package/esm/transforms/RenameObjectFields.js +15 -0
  62. package/esm/transforms/RenameRootFields.js +12 -0
  63. package/esm/transforms/RenameRootTypes.js +54 -0
  64. package/esm/transforms/RenameTypes.js +66 -0
  65. package/esm/transforms/TransformCompositeFields.js +140 -0
  66. package/esm/transforms/TransformEnumValues.js +51 -0
  67. package/esm/transforms/TransformInputObjectFields.js +157 -0
  68. package/esm/transforms/TransformInterfaceFields.js +31 -0
  69. package/esm/transforms/TransformObjectFields.js +31 -0
  70. package/esm/transforms/TransformQuery.js +86 -0
  71. package/esm/transforms/TransformRootFields.js +37 -0
  72. package/esm/transforms/WrapFields.js +214 -0
  73. package/esm/transforms/WrapQuery.js +59 -0
  74. package/esm/transforms/WrapType.js +15 -0
  75. package/esm/transforms/index.js +33 -0
  76. package/esm/types.js +1 -0
  77. package/esm/wrapSchema.js +54 -0
  78. package/package.json +34 -13
  79. package/{generateProxyingResolvers.d.ts → typings/generateProxyingResolvers.d.ts} +0 -0
  80. package/typings/index.d.ts +5 -0
  81. package/{introspect.d.ts → typings/introspect.d.ts} +0 -0
  82. package/{transforms → typings/transforms}/ExtractField.d.ts +0 -0
  83. package/{transforms → typings/transforms}/FilterInputObjectFields.d.ts +1 -1
  84. package/{transforms → typings/transforms}/FilterInterfaceFields.d.ts +0 -0
  85. package/{transforms → typings/transforms}/FilterObjectFieldDirectives.d.ts +0 -0
  86. package/{transforms → typings/transforms}/FilterObjectFields.d.ts +0 -0
  87. package/{transforms → typings/transforms}/FilterRootFields.d.ts +0 -0
  88. package/{transforms → typings/transforms}/FilterTypes.d.ts +0 -0
  89. package/{transforms → typings/transforms}/HoistField.d.ts +0 -0
  90. package/{transforms → typings/transforms}/MapFields.d.ts +1 -1
  91. package/{transforms → typings/transforms}/MapLeafValues.d.ts +1 -1
  92. package/{transforms → typings/transforms}/PruneSchema.d.ts +0 -0
  93. package/{transforms → typings/transforms}/RemoveObjectFieldDeprecations.d.ts +0 -0
  94. package/{transforms → typings/transforms}/RemoveObjectFieldDirectives.d.ts +0 -0
  95. package/{transforms → typings/transforms}/RemoveObjectFieldsWithDeprecation.d.ts +0 -0
  96. package/{transforms → typings/transforms}/RemoveObjectFieldsWithDirective.d.ts +0 -0
  97. package/{transforms → typings/transforms}/RenameInputObjectFields.d.ts +0 -0
  98. package/{transforms → typings/transforms}/RenameInterfaceFields.d.ts +0 -0
  99. package/{transforms → typings/transforms}/RenameObjectFieldArguments.d.ts +0 -0
  100. package/{transforms → typings/transforms}/RenameObjectFields.d.ts +0 -0
  101. package/{transforms → typings/transforms}/RenameRootFields.d.ts +0 -0
  102. package/{transforms → typings/transforms}/RenameRootTypes.d.ts +0 -0
  103. package/{transforms → typings/transforms}/RenameTypes.d.ts +0 -0
  104. package/{transforms → typings/transforms}/TransformCompositeFields.d.ts +1 -1
  105. package/{transforms → typings/transforms}/TransformEnumValues.d.ts +2 -2
  106. package/{transforms → typings/transforms}/TransformInputObjectFields.d.ts +1 -1
  107. package/{transforms → typings/transforms}/TransformInterfaceFields.d.ts +1 -1
  108. package/{transforms → typings/transforms}/TransformObjectFields.d.ts +1 -1
  109. package/{transforms → typings/transforms}/TransformQuery.d.ts +0 -0
  110. package/{transforms → typings/transforms}/TransformRootFields.d.ts +1 -1
  111. package/{transforms → typings/transforms}/WrapFields.d.ts +0 -0
  112. package/{transforms → typings/transforms}/WrapQuery.d.ts +0 -0
  113. package/{transforms → typings/transforms}/WrapType.d.ts +0 -0
  114. package/typings/transforms/index.d.ts +32 -0
  115. package/{types.d.ts → typings/types.d.ts} +0 -0
  116. package/{wrapSchema.d.ts → typings/wrapSchema.d.ts} +0 -0
  117. package/index.d.ts +0 -5
  118. package/index.js +0 -1806
  119. package/index.mjs +0 -1767
  120. package/transforms/index.d.ts +0 -32
@@ -0,0 +1,74 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.defaultCreateProxyingResolver = exports.generateProxyingResolvers = void 0;
4
+ const utils_1 = require("@graphql-tools/utils");
5
+ const delegate_1 = require("@graphql-tools/delegate");
6
+ function generateProxyingResolvers(subschemaConfig) {
7
+ var _a;
8
+ const targetSchema = subschemaConfig.schema;
9
+ const createProxyingResolver = (_a = subschemaConfig.createProxyingResolver) !== null && _a !== void 0 ? _a : defaultCreateProxyingResolver;
10
+ const transformedSchema = (0, delegate_1.applySchemaTransforms)(targetSchema, subschemaConfig);
11
+ const rootTypeMap = (0, utils_1.getRootTypeMap)(targetSchema);
12
+ const resolvers = {};
13
+ for (const [operation, rootType] of rootTypeMap.entries()) {
14
+ const typeName = rootType.name;
15
+ const fields = rootType.getFields();
16
+ resolvers[typeName] = {};
17
+ for (const fieldName in fields) {
18
+ const proxyingResolver = createProxyingResolver({
19
+ subschemaConfig,
20
+ transformedSchema,
21
+ operation,
22
+ fieldName,
23
+ });
24
+ const finalResolver = createPossiblyNestedProxyingResolver(subschemaConfig, proxyingResolver);
25
+ if (operation === 'subscription') {
26
+ resolvers[typeName][fieldName] = {
27
+ subscribe: finalResolver,
28
+ resolve: identical,
29
+ };
30
+ }
31
+ else {
32
+ resolvers[typeName][fieldName] = {
33
+ resolve: finalResolver,
34
+ };
35
+ }
36
+ }
37
+ }
38
+ return resolvers;
39
+ }
40
+ exports.generateProxyingResolvers = generateProxyingResolvers;
41
+ function identical(value) {
42
+ return value;
43
+ }
44
+ function createPossiblyNestedProxyingResolver(subschemaConfig, proxyingResolver) {
45
+ return function possiblyNestedProxyingResolver(parent, args, context, info) {
46
+ if (parent != null) {
47
+ const responseKey = (0, utils_1.getResponseKeyFromInfo)(info);
48
+ // Check to see if the parent contains a proxied result
49
+ if ((0, delegate_1.isExternalObject)(parent)) {
50
+ const unpathedErrors = (0, delegate_1.getUnpathedErrors)(parent);
51
+ const subschema = (0, delegate_1.getSubschema)(parent, responseKey);
52
+ // If there is a proxied result from this subschema, return it
53
+ // This can happen even for a root field when the root type ia
54
+ // also nested as a field within a different type.
55
+ if (subschemaConfig === subschema && parent[responseKey] !== undefined) {
56
+ return (0, delegate_1.resolveExternalValue)(parent[responseKey], unpathedErrors, subschema, context, info);
57
+ }
58
+ }
59
+ }
60
+ return proxyingResolver(parent, args, context, info);
61
+ };
62
+ }
63
+ function defaultCreateProxyingResolver({ subschemaConfig, operation, transformedSchema, }) {
64
+ return function proxyingResolver(_parent, _args, context, info) {
65
+ return (0, delegate_1.delegateToSchema)({
66
+ schema: subschemaConfig,
67
+ operation,
68
+ context,
69
+ info,
70
+ transformedSchema,
71
+ });
72
+ };
73
+ }
74
+ exports.defaultCreateProxyingResolver = defaultCreateProxyingResolver;
package/cjs/index.js ADDED
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.generateProxyingResolvers = exports.defaultCreateProxyingResolver = exports.wrapSchema = void 0;
4
+ const tslib_1 = require("tslib");
5
+ var wrapSchema_js_1 = require("./wrapSchema.js");
6
+ Object.defineProperty(exports, "wrapSchema", { enumerable: true, get: function () { return wrapSchema_js_1.wrapSchema; } });
7
+ var generateProxyingResolvers_js_1 = require("./generateProxyingResolvers.js");
8
+ Object.defineProperty(exports, "defaultCreateProxyingResolver", { enumerable: true, get: function () { return generateProxyingResolvers_js_1.defaultCreateProxyingResolver; } });
9
+ Object.defineProperty(exports, "generateProxyingResolvers", { enumerable: true, get: function () { return generateProxyingResolvers_js_1.generateProxyingResolvers; } });
10
+ tslib_1.__exportStar(require("./transforms/index.js"), exports);
11
+ tslib_1.__exportStar(require("./types.js"), exports);
12
+ tslib_1.__exportStar(require("./introspect.js"), exports);
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.introspectSchema = void 0;
4
+ const graphql_1 = require("graphql");
5
+ const value_or_promise_1 = require("value-or-promise");
6
+ const utils_1 = require("@graphql-tools/utils");
7
+ function getSchemaFromIntrospection(introspectionResult, options) {
8
+ var _a;
9
+ if ((_a = introspectionResult === null || introspectionResult === void 0 ? void 0 : introspectionResult.data) === null || _a === void 0 ? void 0 : _a.__schema) {
10
+ return (0, graphql_1.buildClientSchema)(introspectionResult.data, options);
11
+ }
12
+ throw new Error('Could not obtain introspection result, received: ' + JSON.stringify(introspectionResult));
13
+ }
14
+ function introspectSchema(executor, context, options) {
15
+ const parsedIntrospectionQuery = (0, graphql_1.parse)((0, graphql_1.getIntrospectionQuery)(options), options);
16
+ return new value_or_promise_1.ValueOrPromise(() => executor({
17
+ document: parsedIntrospectionQuery,
18
+ context,
19
+ }))
20
+ .then(introspection => {
21
+ if ((0, utils_1.isAsyncIterable)(introspection)) {
22
+ const iterator = introspection[Symbol.asyncIterator]();
23
+ return iterator.next().then(({ value }) => value);
24
+ }
25
+ return introspection;
26
+ })
27
+ .then(introspection => getSchemaFromIntrospection(introspection, options))
28
+ .resolve();
29
+ }
30
+ exports.introspectSchema = introspectSchema;
@@ -0,0 +1 @@
1
+ {"type":"commonjs"}
@@ -0,0 +1,51 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const graphql_1 = require("graphql");
4
+ class ExtractField {
5
+ constructor({ from, to }) {
6
+ this.from = from;
7
+ this.to = to;
8
+ }
9
+ transformRequest(originalRequest, _delegationContext, _transformationContext) {
10
+ let fromSelection;
11
+ const ourPathFrom = JSON.stringify(this.from);
12
+ const ourPathTo = JSON.stringify(this.to);
13
+ let fieldPath = [];
14
+ (0, graphql_1.visit)(originalRequest.document, {
15
+ [graphql_1.Kind.FIELD]: {
16
+ enter: (node) => {
17
+ fieldPath.push(node.name.value);
18
+ if (ourPathFrom === JSON.stringify(fieldPath)) {
19
+ fromSelection = node.selectionSet;
20
+ return graphql_1.BREAK;
21
+ }
22
+ },
23
+ leave: () => {
24
+ fieldPath.pop();
25
+ },
26
+ },
27
+ });
28
+ fieldPath = [];
29
+ const document = (0, graphql_1.visit)(originalRequest.document, {
30
+ [graphql_1.Kind.FIELD]: {
31
+ enter: (node) => {
32
+ fieldPath.push(node.name.value);
33
+ if (ourPathTo === JSON.stringify(fieldPath) && fromSelection != null) {
34
+ return {
35
+ ...node,
36
+ selectionSet: fromSelection,
37
+ };
38
+ }
39
+ },
40
+ leave: () => {
41
+ fieldPath.pop();
42
+ },
43
+ },
44
+ });
45
+ return {
46
+ ...originalRequest,
47
+ document,
48
+ };
49
+ }
50
+ }
51
+ exports.default = ExtractField;
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const tslib_1 = require("tslib");
4
+ const TransformInputObjectFields_js_1 = tslib_1.__importDefault(require("./TransformInputObjectFields.js"));
5
+ class FilterInputObjectFields {
6
+ constructor(filter, inputObjectNodeTransformer) {
7
+ this.transformer = new TransformInputObjectFields_js_1.default((typeName, fieldName, inputFieldConfig) => filter(typeName, fieldName, inputFieldConfig) ? undefined : null, undefined, inputObjectNodeTransformer);
8
+ }
9
+ transformSchema(originalWrappingSchema, subschemaConfig, transformedSchema) {
10
+ return this.transformer.transformSchema(originalWrappingSchema, subschemaConfig, transformedSchema);
11
+ }
12
+ transformRequest(originalRequest, delegationContext, transformationContext) {
13
+ return this.transformer.transformRequest(originalRequest, delegationContext, transformationContext);
14
+ }
15
+ }
16
+ exports.default = FilterInputObjectFields;
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const tslib_1 = require("tslib");
4
+ const TransformInterfaceFields_js_1 = tslib_1.__importDefault(require("./TransformInterfaceFields.js"));
5
+ class FilterInterfaceFields {
6
+ constructor(filter) {
7
+ this.transformer = new TransformInterfaceFields_js_1.default((typeName, fieldName, fieldConfig) => filter(typeName, fieldName, fieldConfig) ? undefined : null);
8
+ }
9
+ transformSchema(originalWrappingSchema, subschemaConfig, transformedSchema) {
10
+ return this.transformer.transformSchema(originalWrappingSchema, subschemaConfig, transformedSchema);
11
+ }
12
+ }
13
+ exports.default = FilterInterfaceFields;
@@ -0,0 +1,33 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const tslib_1 = require("tslib");
4
+ const utils_1 = require("@graphql-tools/utils");
5
+ const TransformObjectFields_js_1 = tslib_1.__importDefault(require("./TransformObjectFields.js"));
6
+ class FilterObjectFieldDirectives {
7
+ constructor(filter) {
8
+ this.filter = filter;
9
+ }
10
+ transformSchema(originalWrappingSchema, subschemaConfig, transformedSchema) {
11
+ const transformer = new TransformObjectFields_js_1.default((_typeName, _fieldName, fieldConfig) => {
12
+ var _a, _b, _c, _d;
13
+ const keepDirectives = (_c = (_b = (_a = fieldConfig.astNode) === null || _a === void 0 ? void 0 : _a.directives) === null || _b === void 0 ? void 0 : _b.filter(dir => {
14
+ const directiveDef = originalWrappingSchema.getDirective(dir.name.value);
15
+ const directiveValue = directiveDef ? (0, utils_1.getArgumentValues)(directiveDef, dir) : undefined;
16
+ return this.filter(dir.name.value, directiveValue);
17
+ })) !== null && _c !== void 0 ? _c : [];
18
+ if (((_d = fieldConfig.astNode) === null || _d === void 0 ? void 0 : _d.directives) != null &&
19
+ keepDirectives.length !== fieldConfig.astNode.directives.length) {
20
+ fieldConfig = {
21
+ ...fieldConfig,
22
+ astNode: {
23
+ ...fieldConfig.astNode,
24
+ directives: keepDirectives,
25
+ },
26
+ };
27
+ return fieldConfig;
28
+ }
29
+ });
30
+ return transformer.transformSchema(originalWrappingSchema, subschemaConfig, transformedSchema);
31
+ }
32
+ }
33
+ exports.default = FilterObjectFieldDirectives;
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const tslib_1 = require("tslib");
4
+ const TransformObjectFields_js_1 = tslib_1.__importDefault(require("./TransformObjectFields.js"));
5
+ class FilterObjectFields {
6
+ constructor(filter) {
7
+ this.transformer = new TransformObjectFields_js_1.default((typeName, fieldName, fieldConfig) => filter(typeName, fieldName, fieldConfig) ? undefined : null);
8
+ }
9
+ transformSchema(originalWrappingSchema, subschemaConfig, transformedSchema) {
10
+ return this.transformer.transformSchema(originalWrappingSchema, subschemaConfig, transformedSchema);
11
+ }
12
+ }
13
+ exports.default = FilterObjectFields;
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const tslib_1 = require("tslib");
4
+ const TransformRootFields_js_1 = tslib_1.__importDefault(require("./TransformRootFields.js"));
5
+ class FilterRootFields {
6
+ constructor(filter) {
7
+ this.transformer = new TransformRootFields_js_1.default((operation, fieldName, fieldConfig) => {
8
+ if (filter(operation, fieldName, fieldConfig)) {
9
+ return undefined;
10
+ }
11
+ return null;
12
+ });
13
+ }
14
+ transformSchema(originalWrappingSchema, subschemaConfig, transformedSchema) {
15
+ return this.transformer.transformSchema(originalWrappingSchema, subschemaConfig, transformedSchema);
16
+ }
17
+ }
18
+ exports.default = FilterRootFields;
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const utils_1 = require("@graphql-tools/utils");
4
+ class FilterTypes {
5
+ constructor(filter) {
6
+ this.filter = filter;
7
+ }
8
+ transformSchema(originalWrappingSchema, _subschemaConfig, _transformedSchema) {
9
+ return (0, utils_1.mapSchema)(originalWrappingSchema, {
10
+ [utils_1.MapperKind.TYPE]: (type) => {
11
+ if (this.filter(type)) {
12
+ return undefined;
13
+ }
14
+ return null;
15
+ },
16
+ });
17
+ }
18
+ }
19
+ exports.default = FilterTypes;
@@ -0,0 +1,176 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.unwrapValue = exports.renameFieldNode = exports.wrapFieldNode = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const graphql_1 = require("graphql");
6
+ const utils_1 = require("@graphql-tools/utils");
7
+ const delegate_1 = require("@graphql-tools/delegate");
8
+ const generateProxyingResolvers_js_1 = require("../generateProxyingResolvers.js");
9
+ const MapFields_js_1 = tslib_1.__importDefault(require("./MapFields.js"));
10
+ class HoistField {
11
+ constructor(typeName, pathConfig, newFieldName, alias = '__gqtlw__') {
12
+ this.typeName = typeName;
13
+ this.newFieldName = newFieldName;
14
+ const path = pathConfig.map(segment => (typeof segment === 'string' ? segment : segment.fieldName));
15
+ this.argFilters = pathConfig.map((segment, index) => {
16
+ if (typeof segment === 'string' || segment.argFilter == null) {
17
+ return index === pathConfig.length - 1 ? () => true : () => false;
18
+ }
19
+ return segment.argFilter;
20
+ });
21
+ const pathToField = path.slice();
22
+ const oldFieldName = pathToField.pop();
23
+ if (oldFieldName == null) {
24
+ throw new Error(`Cannot hoist field to ${newFieldName} on type ${typeName}, no path provided.`);
25
+ }
26
+ this.oldFieldName = oldFieldName;
27
+ this.pathToField = pathToField;
28
+ const argLevels = Object.create(null);
29
+ this.transformer = new MapFields_js_1.default({
30
+ [typeName]: {
31
+ [newFieldName]: fieldNode => wrapFieldNode(renameFieldNode(fieldNode, oldFieldName), pathToField, alias, argLevels),
32
+ },
33
+ }, {
34
+ [typeName]: value => unwrapValue(value, alias),
35
+ }, errors => (errors != null ? unwrapErrors(errors, alias) : undefined));
36
+ this.argLevels = argLevels;
37
+ }
38
+ transformSchema(originalWrappingSchema, subschemaConfig, transformedSchema) {
39
+ var _a, _b, _c, _d;
40
+ const argsMap = Object.create(null);
41
+ const innerType = this.pathToField.reduce((acc, pathSegment, index) => {
42
+ const field = acc.getFields()[pathSegment];
43
+ for (const arg of field.args) {
44
+ if (this.argFilters[index](arg)) {
45
+ argsMap[arg.name] = arg;
46
+ this.argLevels[arg.name] = index;
47
+ }
48
+ }
49
+ return (0, graphql_1.getNullableType)(field.type);
50
+ }, originalWrappingSchema.getType(this.typeName));
51
+ let [newSchema, targetFieldConfigMap] = (0, utils_1.removeObjectFields)(originalWrappingSchema, innerType.name, fieldName => fieldName === this.oldFieldName);
52
+ const targetField = targetFieldConfigMap[this.oldFieldName];
53
+ let resolve;
54
+ if (transformedSchema) {
55
+ const hoistingToRootField = this.typeName === ((_a = originalWrappingSchema.getQueryType()) === null || _a === void 0 ? void 0 : _a.name) ||
56
+ this.typeName === ((_b = originalWrappingSchema.getMutationType()) === null || _b === void 0 ? void 0 : _b.name);
57
+ if (hoistingToRootField) {
58
+ const targetSchema = subschemaConfig.schema;
59
+ const operation = this.typeName === ((_c = targetSchema.getQueryType()) === null || _c === void 0 ? void 0 : _c.name) ? 'query' : 'mutation';
60
+ const createProxyingResolver = (_d = subschemaConfig.createProxyingResolver) !== null && _d !== void 0 ? _d : generateProxyingResolvers_js_1.defaultCreateProxyingResolver;
61
+ resolve = createProxyingResolver({
62
+ subschemaConfig,
63
+ transformedSchema,
64
+ operation: operation,
65
+ fieldName: this.newFieldName,
66
+ });
67
+ }
68
+ else {
69
+ resolve = delegate_1.defaultMergedResolver;
70
+ }
71
+ }
72
+ const newTargetField = {
73
+ ...targetField,
74
+ resolve: resolve,
75
+ };
76
+ const level = this.pathToField.length;
77
+ const args = targetField.args;
78
+ if (args != null) {
79
+ for (const argName in args) {
80
+ const argConfig = args[argName];
81
+ if (argConfig == null) {
82
+ continue;
83
+ }
84
+ const arg = {
85
+ ...argConfig,
86
+ name: argName,
87
+ description: argConfig.description,
88
+ defaultValue: argConfig.defaultValue,
89
+ extensions: argConfig.extensions,
90
+ astNode: argConfig.astNode,
91
+ };
92
+ if (this.argFilters[level](arg)) {
93
+ argsMap[argName] = arg;
94
+ this.argLevels[arg.name] = level;
95
+ }
96
+ }
97
+ }
98
+ newTargetField.args = argsMap;
99
+ newSchema = (0, utils_1.appendObjectFields)(newSchema, this.typeName, {
100
+ [this.newFieldName]: newTargetField,
101
+ });
102
+ return this.transformer.transformSchema(newSchema, subschemaConfig, transformedSchema);
103
+ }
104
+ transformRequest(originalRequest, delegationContext, transformationContext) {
105
+ return this.transformer.transformRequest(originalRequest, delegationContext, transformationContext);
106
+ }
107
+ transformResult(originalResult, delegationContext, transformationContext) {
108
+ return this.transformer.transformResult(originalResult, delegationContext, transformationContext);
109
+ }
110
+ }
111
+ exports.default = HoistField;
112
+ function wrapFieldNode(fieldNode, path, alias, argLevels) {
113
+ return path.reduceRight((acc, fieldName, index) => ({
114
+ kind: graphql_1.Kind.FIELD,
115
+ alias: {
116
+ kind: graphql_1.Kind.NAME,
117
+ value: alias,
118
+ },
119
+ name: {
120
+ kind: graphql_1.Kind.NAME,
121
+ value: fieldName,
122
+ },
123
+ selectionSet: {
124
+ kind: graphql_1.Kind.SELECTION_SET,
125
+ selections: [acc],
126
+ },
127
+ arguments: fieldNode.arguments != null
128
+ ? fieldNode.arguments.filter(arg => argLevels[arg.name.value] === index)
129
+ : undefined,
130
+ }), {
131
+ ...fieldNode,
132
+ arguments: fieldNode.arguments != null
133
+ ? fieldNode.arguments.filter(arg => argLevels[arg.name.value] === path.length)
134
+ : undefined,
135
+ });
136
+ }
137
+ exports.wrapFieldNode = wrapFieldNode;
138
+ function renameFieldNode(fieldNode, name) {
139
+ return {
140
+ ...fieldNode,
141
+ alias: {
142
+ kind: graphql_1.Kind.NAME,
143
+ value: fieldNode.alias != null ? fieldNode.alias.value : fieldNode.name.value,
144
+ },
145
+ name: {
146
+ kind: graphql_1.Kind.NAME,
147
+ value: name,
148
+ },
149
+ };
150
+ }
151
+ exports.renameFieldNode = renameFieldNode;
152
+ function unwrapValue(originalValue, alias) {
153
+ let newValue = originalValue;
154
+ let object = newValue[alias];
155
+ while (object != null) {
156
+ newValue = object;
157
+ object = newValue[alias];
158
+ }
159
+ delete originalValue[alias];
160
+ Object.assign(originalValue, newValue);
161
+ return originalValue;
162
+ }
163
+ exports.unwrapValue = unwrapValue;
164
+ function unwrapErrors(errors, alias) {
165
+ if (errors === undefined) {
166
+ return undefined;
167
+ }
168
+ return errors.map(error => {
169
+ const originalPath = error.path;
170
+ if (originalPath == null) {
171
+ return error;
172
+ }
173
+ const newPath = originalPath.filter(pathSegment => pathSegment !== alias);
174
+ return (0, utils_1.relocatedError)(error, newPath);
175
+ });
176
+ }
@@ -0,0 +1,61 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const tslib_1 = require("tslib");
4
+ const TransformCompositeFields_js_1 = tslib_1.__importDefault(require("./TransformCompositeFields.js"));
5
+ class MapFields {
6
+ constructor(fieldNodeTransformerMap, objectValueTransformerMap, errorsTransformer) {
7
+ this.fieldNodeTransformerMap = fieldNodeTransformerMap;
8
+ this.objectValueTransformerMap = objectValueTransformerMap;
9
+ this.errorsTransformer = errorsTransformer;
10
+ }
11
+ _getTransformer() {
12
+ const transformer = this.transformer;
13
+ if (transformer === undefined) {
14
+ throw new Error(`The MapFields transform's "transformRequest" and "transformResult" methods cannot be used without first calling "transformSchema".`);
15
+ }
16
+ return transformer;
17
+ }
18
+ transformSchema(originalWrappingSchema, subschemaConfig, transformedSchema) {
19
+ var _a;
20
+ const subscriptionTypeName = (_a = originalWrappingSchema.getSubscriptionType()) === null || _a === void 0 ? void 0 : _a.name;
21
+ const objectValueTransformerMap = this.objectValueTransformerMap;
22
+ this.transformer = new TransformCompositeFields_js_1.default(() => undefined, (typeName, fieldName, fieldNode, fragments, transformationContext) => {
23
+ const typeTransformers = this.fieldNodeTransformerMap[typeName];
24
+ if (typeTransformers == null) {
25
+ return undefined;
26
+ }
27
+ const fieldNodeTransformer = typeTransformers[fieldName];
28
+ if (fieldNodeTransformer == null) {
29
+ return undefined;
30
+ }
31
+ return fieldNodeTransformer(fieldNode, fragments, transformationContext);
32
+ }, objectValueTransformerMap != null
33
+ ? (data, transformationContext) => {
34
+ if (data == null) {
35
+ return data;
36
+ }
37
+ let typeName = data.__typename;
38
+ if (typeName == null) {
39
+ // see https://github.com/ardatan/graphql-tools/issues/2282
40
+ typeName = subscriptionTypeName;
41
+ if (typeName == null) {
42
+ return data;
43
+ }
44
+ }
45
+ const transformer = objectValueTransformerMap[typeName];
46
+ if (transformer == null) {
47
+ return data;
48
+ }
49
+ return transformer(data, transformationContext);
50
+ }
51
+ : undefined, this.errorsTransformer != null ? this.errorsTransformer : undefined);
52
+ return this.transformer.transformSchema(originalWrappingSchema, subschemaConfig, transformedSchema);
53
+ }
54
+ transformRequest(originalRequest, delegationContext, transformationContext) {
55
+ return this._getTransformer().transformRequest(originalRequest, delegationContext, transformationContext);
56
+ }
57
+ transformResult(originalResult, delegationContext, transformationContext) {
58
+ return this._getTransformer().transformResult(originalResult, delegationContext, transformationContext);
59
+ }
60
+ }
61
+ exports.default = MapFields;
@@ -0,0 +1,111 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const graphql_1 = require("graphql");
4
+ const utils_1 = require("@graphql-tools/utils");
5
+ class MapLeafValues {
6
+ constructor(inputValueTransformer, outputValueTransformer) {
7
+ this.inputValueTransformer = inputValueTransformer;
8
+ this.outputValueTransformer = outputValueTransformer;
9
+ this.resultVisitorMap = Object.create(null);
10
+ }
11
+ _getTypeInfo() {
12
+ const typeInfo = this.typeInfo;
13
+ if (typeInfo === undefined) {
14
+ throw new Error(`The MapLeafValues transform's "transformRequest" and "transformResult" methods cannot be used without first calling "transformSchema".`);
15
+ }
16
+ return typeInfo;
17
+ }
18
+ _getOriginalWrappingSchema() {
19
+ const originalWrappingSchema = this.originalWrappingSchema;
20
+ if (originalWrappingSchema === undefined) {
21
+ throw new Error(`The MapLeafValues transform's "transformRequest" and "transformResult" methods cannot be used without first calling "transformSchema".`);
22
+ }
23
+ return originalWrappingSchema;
24
+ }
25
+ transformSchema(originalWrappingSchema, _subschemaConfig, _transformedSchema) {
26
+ this.originalWrappingSchema = originalWrappingSchema;
27
+ const typeMap = originalWrappingSchema.getTypeMap();
28
+ for (const typeName in typeMap) {
29
+ const type = typeMap[typeName];
30
+ if (!typeName.startsWith('__')) {
31
+ if ((0, graphql_1.isLeafType)(type)) {
32
+ this.resultVisitorMap[typeName] = (value) => this.outputValueTransformer(typeName, value);
33
+ }
34
+ }
35
+ }
36
+ this.typeInfo = new graphql_1.TypeInfo(originalWrappingSchema);
37
+ return originalWrappingSchema;
38
+ }
39
+ transformRequest(originalRequest, _delegationContext, transformationContext) {
40
+ var _a;
41
+ const document = originalRequest.document;
42
+ const variableValues = (_a = originalRequest.variables) !== null && _a !== void 0 ? _a : {};
43
+ const operations = document.definitions.filter(def => def.kind === graphql_1.Kind.OPERATION_DEFINITION);
44
+ const fragments = document.definitions.filter(def => def.kind === graphql_1.Kind.FRAGMENT_DEFINITION);
45
+ const newOperations = this.transformOperations(operations, variableValues);
46
+ const transformedRequest = {
47
+ ...originalRequest,
48
+ document: {
49
+ ...document,
50
+ definitions: [...newOperations, ...fragments],
51
+ },
52
+ variables: variableValues,
53
+ };
54
+ transformationContext.transformedRequest = transformedRequest;
55
+ return transformedRequest;
56
+ }
57
+ transformResult(originalResult, _delegationContext, transformationContext) {
58
+ return (0, utils_1.visitResult)(originalResult, transformationContext.transformedRequest, this._getOriginalWrappingSchema(), this.resultVisitorMap);
59
+ }
60
+ transformOperations(operations, variableValues) {
61
+ return operations.map((operation) => {
62
+ var _a;
63
+ const variableDefinitionMap = ((_a = operation.variableDefinitions) !== null && _a !== void 0 ? _a : []).reduce((prev, def) => ({
64
+ ...prev,
65
+ [def.variable.name.value]: def,
66
+ }), {});
67
+ const newOperation = (0, graphql_1.visit)(operation, (0, graphql_1.visitWithTypeInfo)(this._getTypeInfo(), {
68
+ [graphql_1.Kind.FIELD]: node => this.transformFieldNode(node, variableDefinitionMap, variableValues),
69
+ }));
70
+ return {
71
+ ...newOperation,
72
+ variableDefinitions: Object.values(variableDefinitionMap),
73
+ };
74
+ });
75
+ }
76
+ transformFieldNode(field, variableDefinitionMap, variableValues) {
77
+ const targetField = this._getTypeInfo().getFieldDef();
78
+ if (!targetField) {
79
+ return;
80
+ }
81
+ const generateVariableName = (0, utils_1.createVariableNameGenerator)(variableDefinitionMap);
82
+ if (!targetField.name.startsWith('__')) {
83
+ const argumentNodes = field.arguments;
84
+ if (argumentNodes != null) {
85
+ const argumentNodeMap = argumentNodes.reduce((prev, argument) => ({
86
+ ...prev,
87
+ [argument.name.value]: argument,
88
+ }), Object.create(null));
89
+ for (const argument of targetField.args) {
90
+ const argName = argument.name;
91
+ const argType = argument.type;
92
+ const argumentNode = argumentNodeMap[argName];
93
+ const argValue = argumentNode === null || argumentNode === void 0 ? void 0 : argumentNode.value;
94
+ let value;
95
+ if (argValue != null) {
96
+ value = (0, graphql_1.valueFromAST)(argValue, argType, variableValues);
97
+ }
98
+ (0, utils_1.updateArgument)(argumentNodeMap, variableDefinitionMap, variableValues, argName, generateVariableName(argName), argType, (0, utils_1.transformInputValue)(argType, value, (t, v) => {
99
+ const newValue = this.inputValueTransformer(t.name, v);
100
+ return newValue === undefined ? v : newValue;
101
+ }));
102
+ }
103
+ return {
104
+ ...field,
105
+ arguments: Object.values(argumentNodeMap),
106
+ };
107
+ }
108
+ }
109
+ }
110
+ }
111
+ exports.default = MapLeafValues;
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const utils_1 = require("@graphql-tools/utils");
4
+ class PruneTypes {
5
+ constructor(options = {}) {
6
+ this.options = options;
7
+ }
8
+ transformSchema(originalWrappingSchema, _subschemaConfig, _transformedSchema) {
9
+ return (0, utils_1.pruneSchema)(originalWrappingSchema, this.options);
10
+ }
11
+ }
12
+ exports.default = PruneTypes;