@graphql-tools/utils 8.7.0 → 8.8.0-alpha-b76ec274.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 (161) hide show
  1. package/cjs/AggregateError.js +25 -0
  2. package/cjs/Interfaces.js +31 -0
  3. package/cjs/addTypes.js +62 -0
  4. package/cjs/astFromType.js +31 -0
  5. package/cjs/astFromValueUntyped.js +78 -0
  6. package/cjs/build-operation-for-field.js +351 -0
  7. package/cjs/collectFields.js +98 -0
  8. package/cjs/comments.js +380 -0
  9. package/cjs/errors.js +22 -0
  10. package/cjs/executor.js +2 -0
  11. package/cjs/fields.js +115 -0
  12. package/cjs/filterSchema.js +66 -0
  13. package/cjs/fixSchemaAst.js +26 -0
  14. package/cjs/forEachDefaultValue.js +29 -0
  15. package/cjs/forEachField.js +19 -0
  16. package/cjs/get-directives.js +103 -0
  17. package/cjs/get-fields-with-directives.js +52 -0
  18. package/cjs/get-implementing-types.js +19 -0
  19. package/cjs/getArgumentValues.js +76 -0
  20. package/cjs/getObjectTypeFromTypeMap.js +13 -0
  21. package/cjs/getOperationASTFromRequest.js +16 -0
  22. package/cjs/getResolversFromSchema.js +73 -0
  23. package/cjs/getResponseKeyFromInfo.js +12 -0
  24. package/cjs/heal.js +177 -0
  25. package/cjs/helpers.js +76 -0
  26. package/cjs/implementsAbstractType.js +17 -0
  27. package/cjs/index.js +53 -0
  28. package/cjs/inspect.js +107 -0
  29. package/cjs/isAsyncIterable.js +10 -0
  30. package/cjs/isDocumentNode.js +8 -0
  31. package/cjs/loaders.js +2 -0
  32. package/cjs/mapAsyncIterator.js +53 -0
  33. package/cjs/mapSchema.js +470 -0
  34. package/cjs/memoize.js +189 -0
  35. package/cjs/mergeDeep.js +45 -0
  36. package/cjs/observableToAsyncIterable.js +85 -0
  37. package/cjs/package.json +1 -0
  38. package/cjs/parse-graphql-json.js +44 -0
  39. package/cjs/parse-graphql-sdl.js +84 -0
  40. package/cjs/print-schema-with-directives.js +494 -0
  41. package/cjs/prune.js +133 -0
  42. package/cjs/renameType.js +152 -0
  43. package/cjs/rewire.js +159 -0
  44. package/cjs/rootTypes.js +37 -0
  45. package/cjs/selectionSets.js +9 -0
  46. package/cjs/stub.js +68 -0
  47. package/cjs/transformInputValue.js +55 -0
  48. package/cjs/types.js +27 -0
  49. package/cjs/updateArgument.js +54 -0
  50. package/cjs/validate-documents.js +76 -0
  51. package/cjs/valueMatchesCriteria.js +21 -0
  52. package/cjs/visitResult.js +219 -0
  53. package/cjs/withCancel.js +56 -0
  54. package/esm/AggregateError.js +21 -0
  55. package/esm/Interfaces.js +28 -0
  56. package/esm/addTypes.js +58 -0
  57. package/esm/astFromType.js +27 -0
  58. package/esm/astFromValueUntyped.js +74 -0
  59. package/esm/build-operation-for-field.js +347 -0
  60. package/esm/collectFields.js +94 -0
  61. package/esm/comments.js +367 -0
  62. package/esm/errors.js +17 -0
  63. package/esm/executor.js +1 -0
  64. package/esm/fields.js +108 -0
  65. package/esm/filterSchema.js +62 -0
  66. package/esm/fixSchemaAst.js +22 -0
  67. package/esm/forEachDefaultValue.js +25 -0
  68. package/esm/forEachField.js +15 -0
  69. package/esm/get-directives.js +96 -0
  70. package/esm/get-fields-with-directives.js +48 -0
  71. package/esm/get-implementing-types.js +15 -0
  72. package/esm/getArgumentValues.js +72 -0
  73. package/esm/getObjectTypeFromTypeMap.js +9 -0
  74. package/esm/getOperationASTFromRequest.js +12 -0
  75. package/esm/getResolversFromSchema.js +69 -0
  76. package/esm/getResponseKeyFromInfo.js +8 -0
  77. package/esm/heal.js +172 -0
  78. package/esm/helpers.js +65 -0
  79. package/esm/implementsAbstractType.js +13 -0
  80. package/esm/index.js +50 -0
  81. package/esm/inspect.js +103 -0
  82. package/esm/isAsyncIterable.js +6 -0
  83. package/esm/isDocumentNode.js +4 -0
  84. package/esm/loaders.js +1 -0
  85. package/esm/mapAsyncIterator.js +49 -0
  86. package/esm/mapSchema.js +465 -0
  87. package/esm/memoize.js +180 -0
  88. package/esm/mergeDeep.js +41 -0
  89. package/esm/observableToAsyncIterable.js +81 -0
  90. package/esm/parse-graphql-json.js +40 -0
  91. package/esm/parse-graphql-sdl.js +78 -0
  92. package/esm/print-schema-with-directives.js +472 -0
  93. package/esm/prune.js +129 -0
  94. package/esm/renameType.js +148 -0
  95. package/esm/rewire.js +155 -0
  96. package/esm/rootTypes.js +33 -0
  97. package/esm/selectionSets.js +5 -0
  98. package/esm/stub.js +61 -0
  99. package/esm/transformInputValue.js +48 -0
  100. package/esm/types.js +24 -0
  101. package/esm/updateArgument.js +49 -0
  102. package/esm/validate-documents.js +70 -0
  103. package/esm/valueMatchesCriteria.js +17 -0
  104. package/esm/visitResult.js +213 -0
  105. package/esm/withCancel.js +51 -0
  106. package/package.json +31 -10
  107. package/{AggregateError.d.ts → typings/AggregateError.d.ts} +0 -0
  108. package/{Interfaces.d.ts → typings/Interfaces.d.ts} +0 -0
  109. package/{addTypes.d.ts → typings/addTypes.d.ts} +0 -0
  110. package/{astFromType.d.ts → typings/astFromType.d.ts} +0 -0
  111. package/{astFromValueUntyped.d.ts → typings/astFromValueUntyped.d.ts} +0 -0
  112. package/{build-operation-for-field.d.ts → typings/build-operation-for-field.d.ts} +0 -0
  113. package/{collectFields.d.ts → typings/collectFields.d.ts} +0 -0
  114. package/{comments.d.ts → typings/comments.d.ts} +0 -0
  115. package/{errors.d.ts → typings/errors.d.ts} +1 -1
  116. package/{executor.d.ts → typings/executor.d.ts} +1 -1
  117. package/{fields.d.ts → typings/fields.d.ts} +0 -0
  118. package/{filterSchema.d.ts → typings/filterSchema.d.ts} +1 -1
  119. package/{fixSchemaAst.d.ts → typings/fixSchemaAst.d.ts} +1 -1
  120. package/{forEachDefaultValue.d.ts → typings/forEachDefaultValue.d.ts} +1 -1
  121. package/{forEachField.d.ts → typings/forEachField.d.ts} +1 -1
  122. package/{get-directives.d.ts → typings/get-directives.d.ts} +0 -0
  123. package/{get-fields-with-directives.d.ts → typings/get-fields-with-directives.d.ts} +0 -0
  124. package/{get-implementing-types.d.ts → typings/get-implementing-types.d.ts} +0 -0
  125. package/{getArgumentValues.d.ts → typings/getArgumentValues.d.ts} +0 -0
  126. package/{getObjectTypeFromTypeMap.d.ts → typings/getObjectTypeFromTypeMap.d.ts} +1 -1
  127. package/{getOperationASTFromRequest.d.ts → typings/getOperationASTFromRequest.d.ts} +1 -1
  128. package/{getResolversFromSchema.d.ts → typings/getResolversFromSchema.d.ts} +1 -1
  129. package/{getResponseKeyFromInfo.d.ts → typings/getResponseKeyFromInfo.d.ts} +0 -0
  130. package/{heal.d.ts → typings/heal.d.ts} +0 -0
  131. package/{helpers.d.ts → typings/helpers.d.ts} +0 -0
  132. package/{implementsAbstractType.d.ts → typings/implementsAbstractType.d.ts} +1 -1
  133. package/typings/index.d.ts +50 -0
  134. package/{inspect.d.ts → typings/inspect.d.ts} +0 -0
  135. package/{isAsyncIterable.d.ts → typings/isAsyncIterable.d.ts} +0 -0
  136. package/{isDocumentNode.d.ts → typings/isDocumentNode.d.ts} +0 -0
  137. package/{loaders.d.ts → typings/loaders.d.ts} +1 -1
  138. package/{mapAsyncIterator.d.ts → typings/mapAsyncIterator.d.ts} +0 -0
  139. package/{mapSchema.d.ts → typings/mapSchema.d.ts} +1 -1
  140. package/{memoize.d.ts → typings/memoize.d.ts} +0 -0
  141. package/{mergeDeep.d.ts → typings/mergeDeep.d.ts} +0 -0
  142. package/{observableToAsyncIterable.d.ts → typings/observableToAsyncIterable.d.ts} +0 -0
  143. package/{parse-graphql-json.d.ts → typings/parse-graphql-json.d.ts} +2 -2
  144. package/{parse-graphql-sdl.d.ts → typings/parse-graphql-sdl.d.ts} +1 -1
  145. package/{print-schema-with-directives.d.ts → typings/print-schema-with-directives.d.ts} +1 -1
  146. package/{prune.d.ts → typings/prune.d.ts} +1 -1
  147. package/{renameType.d.ts → typings/renameType.d.ts} +0 -0
  148. package/{rewire.d.ts → typings/rewire.d.ts} +0 -0
  149. package/{rootTypes.d.ts → typings/rootTypes.d.ts} +0 -0
  150. package/{selectionSets.d.ts → typings/selectionSets.d.ts} +1 -1
  151. package/{stub.d.ts → typings/stub.d.ts} +0 -0
  152. package/{transformInputValue.d.ts → typings/transformInputValue.d.ts} +1 -1
  153. package/{types.d.ts → typings/types.d.ts} +0 -0
  154. package/{updateArgument.d.ts → typings/updateArgument.d.ts} +0 -0
  155. package/{validate-documents.d.ts → typings/validate-documents.d.ts} +1 -1
  156. package/{valueMatchesCriteria.d.ts → typings/valueMatchesCriteria.d.ts} +0 -0
  157. package/{visitResult.d.ts → typings/visitResult.d.ts} +1 -1
  158. package/{withCancel.d.ts → typings/withCancel.d.ts} +0 -0
  159. package/index.d.ts +0 -50
  160. package/index.js +0 -4428
  161. package/index.mjs +0 -4315
package/esm/inspect.js ADDED
@@ -0,0 +1,103 @@
1
+ // Taken from graphql-js
2
+ // https://github.com/graphql/graphql-js/blob/main/src/jsutils/inspect.ts
3
+ import { GraphQLError } from 'graphql';
4
+ import { isAggregateError } from './AggregateError.js';
5
+ const MAX_RECURSIVE_DEPTH = 3;
6
+ /**
7
+ * Used to print values in error messages.
8
+ */
9
+ export function inspect(value) {
10
+ return formatValue(value, []);
11
+ }
12
+ function formatValue(value, seenValues) {
13
+ switch (typeof value) {
14
+ case 'string':
15
+ return JSON.stringify(value);
16
+ case 'function':
17
+ return value.name ? `[function ${value.name}]` : '[function]';
18
+ case 'object':
19
+ return formatObjectValue(value, seenValues);
20
+ default:
21
+ return String(value);
22
+ }
23
+ }
24
+ function formatError(value) {
25
+ if (value instanceof GraphQLError) {
26
+ return value.toString();
27
+ }
28
+ return `${value.name}: ${value.message};\n ${value.stack}`;
29
+ }
30
+ function formatObjectValue(value, previouslySeenValues) {
31
+ if (value === null) {
32
+ return 'null';
33
+ }
34
+ if (value instanceof Error) {
35
+ if (isAggregateError(value)) {
36
+ return formatError(value) + '\n' + formatArray(value.errors, previouslySeenValues);
37
+ }
38
+ return formatError(value);
39
+ }
40
+ if (previouslySeenValues.includes(value)) {
41
+ return '[Circular]';
42
+ }
43
+ const seenValues = [...previouslySeenValues, value];
44
+ if (isJSONable(value)) {
45
+ const jsonValue = value.toJSON();
46
+ // check for infinite recursion
47
+ if (jsonValue !== value) {
48
+ return typeof jsonValue === 'string' ? jsonValue : formatValue(jsonValue, seenValues);
49
+ }
50
+ }
51
+ else if (Array.isArray(value)) {
52
+ return formatArray(value, seenValues);
53
+ }
54
+ return formatObject(value, seenValues);
55
+ }
56
+ function isJSONable(value) {
57
+ return typeof value.toJSON === 'function';
58
+ }
59
+ function formatObject(object, seenValues) {
60
+ const entries = Object.entries(object);
61
+ if (entries.length === 0) {
62
+ return '{}';
63
+ }
64
+ if (seenValues.length > MAX_RECURSIVE_DEPTH) {
65
+ return '[' + getObjectTag(object) + ']';
66
+ }
67
+ const properties = entries.map(([key, value]) => key + ': ' + formatValue(value, seenValues));
68
+ return '{ ' + properties.join(', ') + ' }';
69
+ }
70
+ function formatArray(array, seenValues) {
71
+ if (array.length === 0) {
72
+ return '[]';
73
+ }
74
+ if (seenValues.length > MAX_RECURSIVE_DEPTH) {
75
+ return '[Array]';
76
+ }
77
+ const len = array.length;
78
+ const remaining = array.length;
79
+ const items = [];
80
+ for (let i = 0; i < len; ++i) {
81
+ items.push(formatValue(array[i], seenValues));
82
+ }
83
+ if (remaining === 1) {
84
+ items.push('... 1 more item');
85
+ }
86
+ else if (remaining > 1) {
87
+ items.push(`... ${remaining} more items`);
88
+ }
89
+ return '[' + items.join(', ') + ']';
90
+ }
91
+ function getObjectTag(object) {
92
+ const tag = Object.prototype.toString
93
+ .call(object)
94
+ .replace(/^\[object /, '')
95
+ .replace(/]$/, '');
96
+ if (tag === 'Object' && typeof object.constructor === 'function') {
97
+ const name = object.constructor.name;
98
+ if (typeof name === 'string' && name !== '') {
99
+ return name;
100
+ }
101
+ }
102
+ return tag;
103
+ }
@@ -0,0 +1,6 @@
1
+ export function isAsyncIterable(value) {
2
+ return (typeof value === 'object' &&
3
+ value != null &&
4
+ Symbol.asyncIterator in value &&
5
+ typeof value[Symbol.asyncIterator] === 'function');
6
+ }
@@ -0,0 +1,4 @@
1
+ import { Kind } from 'graphql';
2
+ export function isDocumentNode(object) {
3
+ return object && typeof object === 'object' && 'kind' in object && object.kind === Kind.DOCUMENT;
4
+ }
package/esm/loaders.js ADDED
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,49 @@
1
+ /**
2
+ * Given an AsyncIterable and a callback function, return an AsyncIterator
3
+ * which produces values mapped via calling the callback function.
4
+ */
5
+ export function mapAsyncIterator(iterator, callback, rejectCallback) {
6
+ let $return;
7
+ let abruptClose;
8
+ if (typeof iterator.return === 'function') {
9
+ $return = iterator.return;
10
+ abruptClose = (error) => {
11
+ const rethrow = () => Promise.reject(error);
12
+ return $return.call(iterator).then(rethrow, rethrow);
13
+ };
14
+ }
15
+ function mapResult(result) {
16
+ return result.done ? result : asyncMapValue(result.value, callback).then(iteratorResult, abruptClose);
17
+ }
18
+ let mapReject;
19
+ if (rejectCallback) {
20
+ // Capture rejectCallback to ensure it cannot be null.
21
+ const reject = rejectCallback;
22
+ mapReject = (error) => asyncMapValue(error, reject).then(iteratorResult, abruptClose);
23
+ }
24
+ return {
25
+ next() {
26
+ return iterator.next().then(mapResult, mapReject);
27
+ },
28
+ return() {
29
+ return $return
30
+ ? $return.call(iterator).then(mapResult, mapReject)
31
+ : Promise.resolve({ value: undefined, done: true });
32
+ },
33
+ throw(error) {
34
+ if (typeof iterator.throw === 'function') {
35
+ return iterator.throw(error).then(mapResult, mapReject);
36
+ }
37
+ return Promise.reject(error).catch(abruptClose);
38
+ },
39
+ [Symbol.asyncIterator]() {
40
+ return this;
41
+ },
42
+ };
43
+ }
44
+ function asyncMapValue(value, callback) {
45
+ return new Promise(resolve => resolve(callback(value)));
46
+ }
47
+ function iteratorResult(value) {
48
+ return { value, done: false };
49
+ }
@@ -0,0 +1,465 @@
1
+ import { GraphQLObjectType, GraphQLSchema, isInterfaceType, isEnumType, isObjectType, isScalarType, isUnionType, isInputObjectType, GraphQLInputObjectType, GraphQLInterfaceType, isLeafType, isListType, isNonNullType, isNamedType, GraphQLList, GraphQLNonNull, GraphQLEnumType, Kind, } from 'graphql';
2
+ import { getObjectTypeFromTypeMap } from './getObjectTypeFromTypeMap.js';
3
+ import { MapperKind, } from './Interfaces.js';
4
+ import { rewireTypes } from './rewire.js';
5
+ import { serializeInputValue, parseInputValue } from './transformInputValue.js';
6
+ export function mapSchema(schema, schemaMapper = {}) {
7
+ const newTypeMap = mapArguments(mapFields(mapTypes(mapDefaultValues(mapEnumValues(mapTypes(mapDefaultValues(schema.getTypeMap(), schema, serializeInputValue), schema, schemaMapper, type => isLeafType(type)), schema, schemaMapper), schema, parseInputValue), schema, schemaMapper, type => !isLeafType(type)), schema, schemaMapper), schema, schemaMapper);
8
+ const originalDirectives = schema.getDirectives();
9
+ const newDirectives = mapDirectives(originalDirectives, schema, schemaMapper);
10
+ const { typeMap, directives } = rewireTypes(newTypeMap, newDirectives);
11
+ return new GraphQLSchema({
12
+ ...schema.toConfig(),
13
+ query: getObjectTypeFromTypeMap(typeMap, getObjectTypeFromTypeMap(newTypeMap, schema.getQueryType())),
14
+ mutation: getObjectTypeFromTypeMap(typeMap, getObjectTypeFromTypeMap(newTypeMap, schema.getMutationType())),
15
+ subscription: getObjectTypeFromTypeMap(typeMap, getObjectTypeFromTypeMap(newTypeMap, schema.getSubscriptionType())),
16
+ types: Object.values(typeMap),
17
+ directives,
18
+ });
19
+ }
20
+ function mapTypes(originalTypeMap, schema, schemaMapper, testFn = () => true) {
21
+ const newTypeMap = {};
22
+ for (const typeName in originalTypeMap) {
23
+ if (!typeName.startsWith('__')) {
24
+ const originalType = originalTypeMap[typeName];
25
+ if (originalType == null || !testFn(originalType)) {
26
+ newTypeMap[typeName] = originalType;
27
+ continue;
28
+ }
29
+ const typeMapper = getTypeMapper(schema, schemaMapper, typeName);
30
+ if (typeMapper == null) {
31
+ newTypeMap[typeName] = originalType;
32
+ continue;
33
+ }
34
+ const maybeNewType = typeMapper(originalType, schema);
35
+ if (maybeNewType === undefined) {
36
+ newTypeMap[typeName] = originalType;
37
+ continue;
38
+ }
39
+ newTypeMap[typeName] = maybeNewType;
40
+ }
41
+ }
42
+ return newTypeMap;
43
+ }
44
+ function mapEnumValues(originalTypeMap, schema, schemaMapper) {
45
+ const enumValueMapper = getEnumValueMapper(schemaMapper);
46
+ if (!enumValueMapper) {
47
+ return originalTypeMap;
48
+ }
49
+ return mapTypes(originalTypeMap, schema, {
50
+ [MapperKind.ENUM_TYPE]: type => {
51
+ const config = type.toConfig();
52
+ const originalEnumValueConfigMap = config.values;
53
+ const newEnumValueConfigMap = {};
54
+ for (const externalValue in originalEnumValueConfigMap) {
55
+ const originalEnumValueConfig = originalEnumValueConfigMap[externalValue];
56
+ const mappedEnumValue = enumValueMapper(originalEnumValueConfig, type.name, schema, externalValue);
57
+ if (mappedEnumValue === undefined) {
58
+ newEnumValueConfigMap[externalValue] = originalEnumValueConfig;
59
+ }
60
+ else if (Array.isArray(mappedEnumValue)) {
61
+ const [newExternalValue, newEnumValueConfig] = mappedEnumValue;
62
+ newEnumValueConfigMap[newExternalValue] =
63
+ newEnumValueConfig === undefined ? originalEnumValueConfig : newEnumValueConfig;
64
+ }
65
+ else if (mappedEnumValue !== null) {
66
+ newEnumValueConfigMap[externalValue] = mappedEnumValue;
67
+ }
68
+ }
69
+ return correctASTNodes(new GraphQLEnumType({
70
+ ...config,
71
+ values: newEnumValueConfigMap,
72
+ }));
73
+ },
74
+ }, type => isEnumType(type));
75
+ }
76
+ function mapDefaultValues(originalTypeMap, schema, fn) {
77
+ const newTypeMap = mapArguments(originalTypeMap, schema, {
78
+ [MapperKind.ARGUMENT]: argumentConfig => {
79
+ if (argumentConfig.defaultValue === undefined) {
80
+ return argumentConfig;
81
+ }
82
+ const maybeNewType = getNewType(originalTypeMap, argumentConfig.type);
83
+ if (maybeNewType != null) {
84
+ return {
85
+ ...argumentConfig,
86
+ defaultValue: fn(maybeNewType, argumentConfig.defaultValue),
87
+ };
88
+ }
89
+ },
90
+ });
91
+ return mapFields(newTypeMap, schema, {
92
+ [MapperKind.INPUT_OBJECT_FIELD]: inputFieldConfig => {
93
+ if (inputFieldConfig.defaultValue === undefined) {
94
+ return inputFieldConfig;
95
+ }
96
+ const maybeNewType = getNewType(newTypeMap, inputFieldConfig.type);
97
+ if (maybeNewType != null) {
98
+ return {
99
+ ...inputFieldConfig,
100
+ defaultValue: fn(maybeNewType, inputFieldConfig.defaultValue),
101
+ };
102
+ }
103
+ },
104
+ });
105
+ }
106
+ function getNewType(newTypeMap, type) {
107
+ if (isListType(type)) {
108
+ const newType = getNewType(newTypeMap, type.ofType);
109
+ return newType != null ? new GraphQLList(newType) : null;
110
+ }
111
+ else if (isNonNullType(type)) {
112
+ const newType = getNewType(newTypeMap, type.ofType);
113
+ return newType != null ? new GraphQLNonNull(newType) : null;
114
+ }
115
+ else if (isNamedType(type)) {
116
+ const newType = newTypeMap[type.name];
117
+ return newType != null ? newType : null;
118
+ }
119
+ return null;
120
+ }
121
+ function mapFields(originalTypeMap, schema, schemaMapper) {
122
+ const newTypeMap = {};
123
+ for (const typeName in originalTypeMap) {
124
+ if (!typeName.startsWith('__')) {
125
+ const originalType = originalTypeMap[typeName];
126
+ if (!isObjectType(originalType) && !isInterfaceType(originalType) && !isInputObjectType(originalType)) {
127
+ newTypeMap[typeName] = originalType;
128
+ continue;
129
+ }
130
+ const fieldMapper = getFieldMapper(schema, schemaMapper, typeName);
131
+ if (fieldMapper == null) {
132
+ newTypeMap[typeName] = originalType;
133
+ continue;
134
+ }
135
+ const config = originalType.toConfig();
136
+ const originalFieldConfigMap = config.fields;
137
+ const newFieldConfigMap = {};
138
+ for (const fieldName in originalFieldConfigMap) {
139
+ const originalFieldConfig = originalFieldConfigMap[fieldName];
140
+ const mappedField = fieldMapper(originalFieldConfig, fieldName, typeName, schema);
141
+ if (mappedField === undefined) {
142
+ newFieldConfigMap[fieldName] = originalFieldConfig;
143
+ }
144
+ else if (Array.isArray(mappedField)) {
145
+ const [newFieldName, newFieldConfig] = mappedField;
146
+ if (newFieldConfig.astNode != null) {
147
+ newFieldConfig.astNode = {
148
+ ...newFieldConfig.astNode,
149
+ name: {
150
+ ...newFieldConfig.astNode.name,
151
+ value: newFieldName,
152
+ },
153
+ };
154
+ }
155
+ newFieldConfigMap[newFieldName] = newFieldConfig === undefined ? originalFieldConfig : newFieldConfig;
156
+ }
157
+ else if (mappedField !== null) {
158
+ newFieldConfigMap[fieldName] = mappedField;
159
+ }
160
+ }
161
+ if (isObjectType(originalType)) {
162
+ newTypeMap[typeName] = correctASTNodes(new GraphQLObjectType({
163
+ ...config,
164
+ fields: newFieldConfigMap,
165
+ }));
166
+ }
167
+ else if (isInterfaceType(originalType)) {
168
+ newTypeMap[typeName] = correctASTNodes(new GraphQLInterfaceType({
169
+ ...config,
170
+ fields: newFieldConfigMap,
171
+ }));
172
+ }
173
+ else {
174
+ newTypeMap[typeName] = correctASTNodes(new GraphQLInputObjectType({
175
+ ...config,
176
+ fields: newFieldConfigMap,
177
+ }));
178
+ }
179
+ }
180
+ }
181
+ return newTypeMap;
182
+ }
183
+ function mapArguments(originalTypeMap, schema, schemaMapper) {
184
+ const newTypeMap = {};
185
+ for (const typeName in originalTypeMap) {
186
+ if (!typeName.startsWith('__')) {
187
+ const originalType = originalTypeMap[typeName];
188
+ if (!isObjectType(originalType) && !isInterfaceType(originalType)) {
189
+ newTypeMap[typeName] = originalType;
190
+ continue;
191
+ }
192
+ const argumentMapper = getArgumentMapper(schemaMapper);
193
+ if (argumentMapper == null) {
194
+ newTypeMap[typeName] = originalType;
195
+ continue;
196
+ }
197
+ const config = originalType.toConfig();
198
+ const originalFieldConfigMap = config.fields;
199
+ const newFieldConfigMap = {};
200
+ for (const fieldName in originalFieldConfigMap) {
201
+ const originalFieldConfig = originalFieldConfigMap[fieldName];
202
+ const originalArgumentConfigMap = originalFieldConfig.args;
203
+ if (originalArgumentConfigMap == null) {
204
+ newFieldConfigMap[fieldName] = originalFieldConfig;
205
+ continue;
206
+ }
207
+ const argumentNames = Object.keys(originalArgumentConfigMap);
208
+ if (!argumentNames.length) {
209
+ newFieldConfigMap[fieldName] = originalFieldConfig;
210
+ continue;
211
+ }
212
+ const newArgumentConfigMap = {};
213
+ for (const argumentName of argumentNames) {
214
+ const originalArgumentConfig = originalArgumentConfigMap[argumentName];
215
+ const mappedArgument = argumentMapper(originalArgumentConfig, fieldName, typeName, schema);
216
+ if (mappedArgument === undefined) {
217
+ newArgumentConfigMap[argumentName] = originalArgumentConfig;
218
+ }
219
+ else if (Array.isArray(mappedArgument)) {
220
+ const [newArgumentName, newArgumentConfig] = mappedArgument;
221
+ newArgumentConfigMap[newArgumentName] = newArgumentConfig;
222
+ }
223
+ else if (mappedArgument !== null) {
224
+ newArgumentConfigMap[argumentName] = mappedArgument;
225
+ }
226
+ }
227
+ newFieldConfigMap[fieldName] = {
228
+ ...originalFieldConfig,
229
+ args: newArgumentConfigMap,
230
+ };
231
+ }
232
+ if (isObjectType(originalType)) {
233
+ newTypeMap[typeName] = new GraphQLObjectType({
234
+ ...config,
235
+ fields: newFieldConfigMap,
236
+ });
237
+ }
238
+ else if (isInterfaceType(originalType)) {
239
+ newTypeMap[typeName] = new GraphQLInterfaceType({
240
+ ...config,
241
+ fields: newFieldConfigMap,
242
+ });
243
+ }
244
+ else {
245
+ newTypeMap[typeName] = new GraphQLInputObjectType({
246
+ ...config,
247
+ fields: newFieldConfigMap,
248
+ });
249
+ }
250
+ }
251
+ }
252
+ return newTypeMap;
253
+ }
254
+ function mapDirectives(originalDirectives, schema, schemaMapper) {
255
+ const directiveMapper = getDirectiveMapper(schemaMapper);
256
+ if (directiveMapper == null) {
257
+ return originalDirectives.slice();
258
+ }
259
+ const newDirectives = [];
260
+ for (const directive of originalDirectives) {
261
+ const mappedDirective = directiveMapper(directive, schema);
262
+ if (mappedDirective === undefined) {
263
+ newDirectives.push(directive);
264
+ }
265
+ else if (mappedDirective !== null) {
266
+ newDirectives.push(mappedDirective);
267
+ }
268
+ }
269
+ return newDirectives;
270
+ }
271
+ function getTypeSpecifiers(schema, typeName) {
272
+ var _a, _b, _c;
273
+ const type = schema.getType(typeName);
274
+ const specifiers = [MapperKind.TYPE];
275
+ if (isObjectType(type)) {
276
+ specifiers.push(MapperKind.COMPOSITE_TYPE, MapperKind.OBJECT_TYPE);
277
+ if (typeName === ((_a = schema.getQueryType()) === null || _a === void 0 ? void 0 : _a.name)) {
278
+ specifiers.push(MapperKind.ROOT_OBJECT, MapperKind.QUERY);
279
+ }
280
+ else if (typeName === ((_b = schema.getMutationType()) === null || _b === void 0 ? void 0 : _b.name)) {
281
+ specifiers.push(MapperKind.ROOT_OBJECT, MapperKind.MUTATION);
282
+ }
283
+ else if (typeName === ((_c = schema.getSubscriptionType()) === null || _c === void 0 ? void 0 : _c.name)) {
284
+ specifiers.push(MapperKind.ROOT_OBJECT, MapperKind.SUBSCRIPTION);
285
+ }
286
+ }
287
+ else if (isInputObjectType(type)) {
288
+ specifiers.push(MapperKind.INPUT_OBJECT_TYPE);
289
+ }
290
+ else if (isInterfaceType(type)) {
291
+ specifiers.push(MapperKind.COMPOSITE_TYPE, MapperKind.ABSTRACT_TYPE, MapperKind.INTERFACE_TYPE);
292
+ }
293
+ else if (isUnionType(type)) {
294
+ specifiers.push(MapperKind.COMPOSITE_TYPE, MapperKind.ABSTRACT_TYPE, MapperKind.UNION_TYPE);
295
+ }
296
+ else if (isEnumType(type)) {
297
+ specifiers.push(MapperKind.ENUM_TYPE);
298
+ }
299
+ else if (isScalarType(type)) {
300
+ specifiers.push(MapperKind.SCALAR_TYPE);
301
+ }
302
+ return specifiers;
303
+ }
304
+ function getTypeMapper(schema, schemaMapper, typeName) {
305
+ const specifiers = getTypeSpecifiers(schema, typeName);
306
+ let typeMapper;
307
+ const stack = [...specifiers];
308
+ while (!typeMapper && stack.length > 0) {
309
+ // It is safe to use the ! operator here as we check the length.
310
+ const next = stack.pop();
311
+ typeMapper = schemaMapper[next];
312
+ }
313
+ return typeMapper != null ? typeMapper : null;
314
+ }
315
+ function getFieldSpecifiers(schema, typeName) {
316
+ var _a, _b, _c;
317
+ const type = schema.getType(typeName);
318
+ const specifiers = [MapperKind.FIELD];
319
+ if (isObjectType(type)) {
320
+ specifiers.push(MapperKind.COMPOSITE_FIELD, MapperKind.OBJECT_FIELD);
321
+ if (typeName === ((_a = schema.getQueryType()) === null || _a === void 0 ? void 0 : _a.name)) {
322
+ specifiers.push(MapperKind.ROOT_FIELD, MapperKind.QUERY_ROOT_FIELD);
323
+ }
324
+ else if (typeName === ((_b = schema.getMutationType()) === null || _b === void 0 ? void 0 : _b.name)) {
325
+ specifiers.push(MapperKind.ROOT_FIELD, MapperKind.MUTATION_ROOT_FIELD);
326
+ }
327
+ else if (typeName === ((_c = schema.getSubscriptionType()) === null || _c === void 0 ? void 0 : _c.name)) {
328
+ specifiers.push(MapperKind.ROOT_FIELD, MapperKind.SUBSCRIPTION_ROOT_FIELD);
329
+ }
330
+ }
331
+ else if (isInterfaceType(type)) {
332
+ specifiers.push(MapperKind.COMPOSITE_FIELD, MapperKind.INTERFACE_FIELD);
333
+ }
334
+ else if (isInputObjectType(type)) {
335
+ specifiers.push(MapperKind.INPUT_OBJECT_FIELD);
336
+ }
337
+ return specifiers;
338
+ }
339
+ function getFieldMapper(schema, schemaMapper, typeName) {
340
+ const specifiers = getFieldSpecifiers(schema, typeName);
341
+ let fieldMapper;
342
+ const stack = [...specifiers];
343
+ while (!fieldMapper && stack.length > 0) {
344
+ // It is safe to use the ! operator here as we check the length.
345
+ const next = stack.pop();
346
+ // TODO: fix this as unknown cast
347
+ fieldMapper = schemaMapper[next];
348
+ }
349
+ return fieldMapper !== null && fieldMapper !== void 0 ? fieldMapper : null;
350
+ }
351
+ function getArgumentMapper(schemaMapper) {
352
+ const argumentMapper = schemaMapper[MapperKind.ARGUMENT];
353
+ return argumentMapper != null ? argumentMapper : null;
354
+ }
355
+ function getDirectiveMapper(schemaMapper) {
356
+ const directiveMapper = schemaMapper[MapperKind.DIRECTIVE];
357
+ return directiveMapper != null ? directiveMapper : null;
358
+ }
359
+ function getEnumValueMapper(schemaMapper) {
360
+ const enumValueMapper = schemaMapper[MapperKind.ENUM_VALUE];
361
+ return enumValueMapper != null ? enumValueMapper : null;
362
+ }
363
+ export function correctASTNodes(type) {
364
+ if (isObjectType(type)) {
365
+ const config = type.toConfig();
366
+ if (config.astNode != null) {
367
+ const fields = [];
368
+ for (const fieldName in config.fields) {
369
+ const fieldConfig = config.fields[fieldName];
370
+ if (fieldConfig.astNode != null) {
371
+ fields.push(fieldConfig.astNode);
372
+ }
373
+ }
374
+ config.astNode = {
375
+ ...config.astNode,
376
+ kind: Kind.OBJECT_TYPE_DEFINITION,
377
+ fields,
378
+ };
379
+ }
380
+ if (config.extensionASTNodes != null) {
381
+ config.extensionASTNodes = config.extensionASTNodes.map(node => ({
382
+ ...node,
383
+ kind: Kind.OBJECT_TYPE_EXTENSION,
384
+ fields: undefined,
385
+ }));
386
+ }
387
+ return new GraphQLObjectType(config);
388
+ }
389
+ else if (isInterfaceType(type)) {
390
+ const config = type.toConfig();
391
+ if (config.astNode != null) {
392
+ const fields = [];
393
+ for (const fieldName in config.fields) {
394
+ const fieldConfig = config.fields[fieldName];
395
+ if (fieldConfig.astNode != null) {
396
+ fields.push(fieldConfig.astNode);
397
+ }
398
+ }
399
+ config.astNode = {
400
+ ...config.astNode,
401
+ kind: Kind.INTERFACE_TYPE_DEFINITION,
402
+ fields,
403
+ };
404
+ }
405
+ if (config.extensionASTNodes != null) {
406
+ config.extensionASTNodes = config.extensionASTNodes.map(node => ({
407
+ ...node,
408
+ kind: Kind.INTERFACE_TYPE_EXTENSION,
409
+ fields: undefined,
410
+ }));
411
+ }
412
+ return new GraphQLInterfaceType(config);
413
+ }
414
+ else if (isInputObjectType(type)) {
415
+ const config = type.toConfig();
416
+ if (config.astNode != null) {
417
+ const fields = [];
418
+ for (const fieldName in config.fields) {
419
+ const fieldConfig = config.fields[fieldName];
420
+ if (fieldConfig.astNode != null) {
421
+ fields.push(fieldConfig.astNode);
422
+ }
423
+ }
424
+ config.astNode = {
425
+ ...config.astNode,
426
+ kind: Kind.INPUT_OBJECT_TYPE_DEFINITION,
427
+ fields,
428
+ };
429
+ }
430
+ if (config.extensionASTNodes != null) {
431
+ config.extensionASTNodes = config.extensionASTNodes.map(node => ({
432
+ ...node,
433
+ kind: Kind.INPUT_OBJECT_TYPE_EXTENSION,
434
+ fields: undefined,
435
+ }));
436
+ }
437
+ return new GraphQLInputObjectType(config);
438
+ }
439
+ else if (isEnumType(type)) {
440
+ const config = type.toConfig();
441
+ if (config.astNode != null) {
442
+ const values = [];
443
+ for (const enumKey in config.values) {
444
+ const enumValueConfig = config.values[enumKey];
445
+ if (enumValueConfig.astNode != null) {
446
+ values.push(enumValueConfig.astNode);
447
+ }
448
+ }
449
+ config.astNode = {
450
+ ...config.astNode,
451
+ values,
452
+ };
453
+ }
454
+ if (config.extensionASTNodes != null) {
455
+ config.extensionASTNodes = config.extensionASTNodes.map(node => ({
456
+ ...node,
457
+ values: undefined,
458
+ }));
459
+ }
460
+ return new GraphQLEnumType(config);
461
+ }
462
+ else {
463
+ return type;
464
+ }
465
+ }