@graphql-tools/utils 8.7.0 → 8.8.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 (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 +229 -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 +223 -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
@@ -0,0 +1,494 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.makeDirectiveNodes = exports.makeDirectiveNode = exports.makeDeprecatedDirective = exports.astFromEnumValue = exports.astFromInputField = exports.astFromField = exports.astFromScalarType = exports.astFromEnumType = exports.astFromInputObjectType = exports.astFromUnionType = exports.astFromInterfaceType = exports.astFromObjectType = exports.astFromArg = exports.getDeprecatableDirectiveNodes = exports.getDirectiveNodes = exports.astFromDirective = exports.astFromSchema = exports.printSchemaWithDirectives = exports.getDocumentNodeFromSchema = void 0;
4
+ const graphql_1 = require("graphql");
5
+ const astFromType_js_1 = require("./astFromType.js");
6
+ const get_directives_js_1 = require("./get-directives.js");
7
+ const astFromValueUntyped_js_1 = require("./astFromValueUntyped.js");
8
+ const helpers_js_1 = require("./helpers.js");
9
+ const rootTypes_js_1 = require("./rootTypes.js");
10
+ function getDocumentNodeFromSchema(schema, options = {}) {
11
+ const pathToDirectivesInExtensions = options.pathToDirectivesInExtensions;
12
+ const typesMap = schema.getTypeMap();
13
+ const schemaNode = astFromSchema(schema, pathToDirectivesInExtensions);
14
+ const definitions = schemaNode != null ? [schemaNode] : [];
15
+ const directives = schema.getDirectives();
16
+ for (const directive of directives) {
17
+ if ((0, graphql_1.isSpecifiedDirective)(directive)) {
18
+ continue;
19
+ }
20
+ definitions.push(astFromDirective(directive, schema, pathToDirectivesInExtensions));
21
+ }
22
+ for (const typeName in typesMap) {
23
+ const type = typesMap[typeName];
24
+ const isPredefinedScalar = (0, graphql_1.isSpecifiedScalarType)(type);
25
+ const isIntrospection = (0, graphql_1.isIntrospectionType)(type);
26
+ if (isPredefinedScalar || isIntrospection) {
27
+ continue;
28
+ }
29
+ if ((0, graphql_1.isObjectType)(type)) {
30
+ definitions.push(astFromObjectType(type, schema, pathToDirectivesInExtensions));
31
+ }
32
+ else if ((0, graphql_1.isInterfaceType)(type)) {
33
+ definitions.push(astFromInterfaceType(type, schema, pathToDirectivesInExtensions));
34
+ }
35
+ else if ((0, graphql_1.isUnionType)(type)) {
36
+ definitions.push(astFromUnionType(type, schema, pathToDirectivesInExtensions));
37
+ }
38
+ else if ((0, graphql_1.isInputObjectType)(type)) {
39
+ definitions.push(astFromInputObjectType(type, schema, pathToDirectivesInExtensions));
40
+ }
41
+ else if ((0, graphql_1.isEnumType)(type)) {
42
+ definitions.push(astFromEnumType(type, schema, pathToDirectivesInExtensions));
43
+ }
44
+ else if ((0, graphql_1.isScalarType)(type)) {
45
+ definitions.push(astFromScalarType(type, schema, pathToDirectivesInExtensions));
46
+ }
47
+ else {
48
+ throw new Error(`Unknown type ${type}.`);
49
+ }
50
+ }
51
+ return {
52
+ kind: graphql_1.Kind.DOCUMENT,
53
+ definitions,
54
+ };
55
+ }
56
+ exports.getDocumentNodeFromSchema = getDocumentNodeFromSchema;
57
+ // this approach uses the default schema printer rather than a custom solution, so may be more backwards compatible
58
+ // currently does not allow customization of printSchema options having to do with comments.
59
+ function printSchemaWithDirectives(schema, options = {}) {
60
+ const documentNode = getDocumentNodeFromSchema(schema, options);
61
+ return (0, graphql_1.print)(documentNode);
62
+ }
63
+ exports.printSchemaWithDirectives = printSchemaWithDirectives;
64
+ function astFromSchema(schema, pathToDirectivesInExtensions) {
65
+ var _a, _b;
66
+ const operationTypeMap = new Map([
67
+ ['query', undefined],
68
+ ['mutation', undefined],
69
+ ['subscription', undefined],
70
+ ]);
71
+ const nodes = [];
72
+ if (schema.astNode != null) {
73
+ nodes.push(schema.astNode);
74
+ }
75
+ if (schema.extensionASTNodes != null) {
76
+ for (const extensionASTNode of schema.extensionASTNodes) {
77
+ nodes.push(extensionASTNode);
78
+ }
79
+ }
80
+ for (const node of nodes) {
81
+ if (node.operationTypes) {
82
+ for (const operationTypeDefinitionNode of node.operationTypes) {
83
+ operationTypeMap.set(operationTypeDefinitionNode.operation, operationTypeDefinitionNode);
84
+ }
85
+ }
86
+ }
87
+ const rootTypeMap = (0, rootTypes_js_1.getRootTypeMap)(schema);
88
+ for (const [operationTypeNode, operationTypeDefinitionNode] of operationTypeMap) {
89
+ const rootType = rootTypeMap.get(operationTypeNode);
90
+ if (rootType != null) {
91
+ const rootTypeAST = (0, astFromType_js_1.astFromType)(rootType);
92
+ if (operationTypeDefinitionNode != null) {
93
+ operationTypeDefinitionNode.type = rootTypeAST;
94
+ }
95
+ else {
96
+ operationTypeMap.set(operationTypeNode, {
97
+ kind: graphql_1.Kind.OPERATION_TYPE_DEFINITION,
98
+ operation: operationTypeNode,
99
+ type: rootTypeAST,
100
+ });
101
+ }
102
+ }
103
+ }
104
+ const operationTypes = [...operationTypeMap.values()].filter(helpers_js_1.isSome);
105
+ const directives = getDirectiveNodes(schema, schema, pathToDirectivesInExtensions);
106
+ if (!operationTypes.length && !directives.length) {
107
+ return null;
108
+ }
109
+ const schemaNode = {
110
+ kind: operationTypes != null ? graphql_1.Kind.SCHEMA_DEFINITION : graphql_1.Kind.SCHEMA_EXTENSION,
111
+ operationTypes,
112
+ // ConstXNode has been introduced in v16 but it is not compatible with XNode so we do `as any` for backwards compatibility
113
+ directives: directives,
114
+ };
115
+ // This code is so weird because it needs to support GraphQL.js 14
116
+ // In GraphQL.js 14 there is no `description` value on schemaNode
117
+ schemaNode.description =
118
+ ((_b = (_a = schema.astNode) === null || _a === void 0 ? void 0 : _a.description) !== null && _b !== void 0 ? _b : schema.description != null)
119
+ ? {
120
+ kind: graphql_1.Kind.STRING,
121
+ value: schema.description,
122
+ block: true,
123
+ }
124
+ : undefined;
125
+ return schemaNode;
126
+ }
127
+ exports.astFromSchema = astFromSchema;
128
+ function astFromDirective(directive, schema, pathToDirectivesInExtensions) {
129
+ var _a, _b, _c, _d;
130
+ return {
131
+ kind: graphql_1.Kind.DIRECTIVE_DEFINITION,
132
+ description: (_b = (_a = directive.astNode) === null || _a === void 0 ? void 0 : _a.description) !== null && _b !== void 0 ? _b : (directive.description
133
+ ? {
134
+ kind: graphql_1.Kind.STRING,
135
+ value: directive.description,
136
+ }
137
+ : undefined),
138
+ name: {
139
+ kind: graphql_1.Kind.NAME,
140
+ value: directive.name,
141
+ },
142
+ arguments: (_c = directive.args) === null || _c === void 0 ? void 0 : _c.map(arg => astFromArg(arg, schema, pathToDirectivesInExtensions)),
143
+ repeatable: directive.isRepeatable,
144
+ locations: ((_d = directive.locations) === null || _d === void 0 ? void 0 : _d.map(location => ({
145
+ kind: graphql_1.Kind.NAME,
146
+ value: location,
147
+ }))) || [],
148
+ };
149
+ }
150
+ exports.astFromDirective = astFromDirective;
151
+ function getDirectiveNodes(entity, schema, pathToDirectivesInExtensions) {
152
+ const directivesInExtensions = (0, get_directives_js_1.getDirectivesInExtensions)(entity, pathToDirectivesInExtensions);
153
+ let nodes = [];
154
+ if (entity.astNode != null) {
155
+ nodes.push(entity.astNode);
156
+ }
157
+ if ('extensionASTNodes' in entity && entity.extensionASTNodes != null) {
158
+ nodes = nodes.concat(entity.extensionASTNodes);
159
+ }
160
+ let directives;
161
+ if (directivesInExtensions != null) {
162
+ directives = makeDirectiveNodes(schema, directivesInExtensions);
163
+ }
164
+ else {
165
+ directives = [];
166
+ for (const node of nodes) {
167
+ if (node.directives) {
168
+ directives.push(...node.directives);
169
+ }
170
+ }
171
+ }
172
+ return directives;
173
+ }
174
+ exports.getDirectiveNodes = getDirectiveNodes;
175
+ function getDeprecatableDirectiveNodes(entity, schema, pathToDirectivesInExtensions) {
176
+ var _a, _b;
177
+ let directiveNodesBesidesDeprecated = [];
178
+ let deprecatedDirectiveNode = null;
179
+ const directivesInExtensions = (0, get_directives_js_1.getDirectivesInExtensions)(entity, pathToDirectivesInExtensions);
180
+ let directives;
181
+ if (directivesInExtensions != null) {
182
+ directives = makeDirectiveNodes(schema, directivesInExtensions);
183
+ }
184
+ else {
185
+ directives = (_a = entity.astNode) === null || _a === void 0 ? void 0 : _a.directives;
186
+ }
187
+ if (directives != null) {
188
+ directiveNodesBesidesDeprecated = directives.filter(directive => directive.name.value !== 'deprecated');
189
+ if (entity.deprecationReason != null) {
190
+ deprecatedDirectiveNode = (_b = directives.filter(directive => directive.name.value === 'deprecated')) === null || _b === void 0 ? void 0 : _b[0];
191
+ }
192
+ }
193
+ if (entity.deprecationReason != null &&
194
+ deprecatedDirectiveNode == null) {
195
+ deprecatedDirectiveNode = makeDeprecatedDirective(entity.deprecationReason);
196
+ }
197
+ return deprecatedDirectiveNode == null
198
+ ? directiveNodesBesidesDeprecated
199
+ : [deprecatedDirectiveNode].concat(directiveNodesBesidesDeprecated);
200
+ }
201
+ exports.getDeprecatableDirectiveNodes = getDeprecatableDirectiveNodes;
202
+ function astFromArg(arg, schema, pathToDirectivesInExtensions) {
203
+ var _a, _b, _c;
204
+ return {
205
+ kind: graphql_1.Kind.INPUT_VALUE_DEFINITION,
206
+ description: (_b = (_a = arg.astNode) === null || _a === void 0 ? void 0 : _a.description) !== null && _b !== void 0 ? _b : (arg.description
207
+ ? {
208
+ kind: graphql_1.Kind.STRING,
209
+ value: arg.description,
210
+ block: true,
211
+ }
212
+ : undefined),
213
+ name: {
214
+ kind: graphql_1.Kind.NAME,
215
+ value: arg.name,
216
+ },
217
+ type: (0, astFromType_js_1.astFromType)(arg.type),
218
+ // ConstXNode has been introduced in v16 but it is not compatible with XNode so we do `as any` for backwards compatibility
219
+ defaultValue: arg.defaultValue !== undefined ? (_c = (0, graphql_1.astFromValue)(arg.defaultValue, arg.type)) !== null && _c !== void 0 ? _c : undefined : undefined,
220
+ directives: getDeprecatableDirectiveNodes(arg, schema, pathToDirectivesInExtensions),
221
+ };
222
+ }
223
+ exports.astFromArg = astFromArg;
224
+ function astFromObjectType(type, schema, pathToDirectivesInExtensions) {
225
+ var _a, _b;
226
+ return {
227
+ kind: graphql_1.Kind.OBJECT_TYPE_DEFINITION,
228
+ description: (_b = (_a = type.astNode) === null || _a === void 0 ? void 0 : _a.description) !== null && _b !== void 0 ? _b : (type.description
229
+ ? {
230
+ kind: graphql_1.Kind.STRING,
231
+ value: type.description,
232
+ block: true,
233
+ }
234
+ : undefined),
235
+ name: {
236
+ kind: graphql_1.Kind.NAME,
237
+ value: type.name,
238
+ },
239
+ fields: Object.values(type.getFields()).map(field => astFromField(field, schema, pathToDirectivesInExtensions)),
240
+ interfaces: Object.values(type.getInterfaces()).map(iFace => (0, astFromType_js_1.astFromType)(iFace)),
241
+ directives: getDirectiveNodes(type, schema, pathToDirectivesInExtensions),
242
+ };
243
+ }
244
+ exports.astFromObjectType = astFromObjectType;
245
+ function astFromInterfaceType(type, schema, pathToDirectivesInExtensions) {
246
+ var _a, _b;
247
+ const node = {
248
+ kind: graphql_1.Kind.INTERFACE_TYPE_DEFINITION,
249
+ description: (_b = (_a = type.astNode) === null || _a === void 0 ? void 0 : _a.description) !== null && _b !== void 0 ? _b : (type.description
250
+ ? {
251
+ kind: graphql_1.Kind.STRING,
252
+ value: type.description,
253
+ block: true,
254
+ }
255
+ : undefined),
256
+ name: {
257
+ kind: graphql_1.Kind.NAME,
258
+ value: type.name,
259
+ },
260
+ fields: Object.values(type.getFields()).map(field => astFromField(field, schema, pathToDirectivesInExtensions)),
261
+ directives: getDirectiveNodes(type, schema, pathToDirectivesInExtensions),
262
+ };
263
+ if ('getInterfaces' in type) {
264
+ node.interfaces = Object.values(type.getInterfaces()).map(iFace => (0, astFromType_js_1.astFromType)(iFace));
265
+ }
266
+ return node;
267
+ }
268
+ exports.astFromInterfaceType = astFromInterfaceType;
269
+ function astFromUnionType(type, schema, pathToDirectivesInExtensions) {
270
+ var _a, _b;
271
+ return {
272
+ kind: graphql_1.Kind.UNION_TYPE_DEFINITION,
273
+ description: (_b = (_a = type.astNode) === null || _a === void 0 ? void 0 : _a.description) !== null && _b !== void 0 ? _b : (type.description
274
+ ? {
275
+ kind: graphql_1.Kind.STRING,
276
+ value: type.description,
277
+ block: true,
278
+ }
279
+ : undefined),
280
+ name: {
281
+ kind: graphql_1.Kind.NAME,
282
+ value: type.name,
283
+ },
284
+ // ConstXNode has been introduced in v16 but it is not compatible with XNode so we do `as any` for backwards compatibility
285
+ directives: getDirectiveNodes(type, schema, pathToDirectivesInExtensions),
286
+ types: type.getTypes().map(type => (0, astFromType_js_1.astFromType)(type)),
287
+ };
288
+ }
289
+ exports.astFromUnionType = astFromUnionType;
290
+ function astFromInputObjectType(type, schema, pathToDirectivesInExtensions) {
291
+ var _a, _b;
292
+ return {
293
+ kind: graphql_1.Kind.INPUT_OBJECT_TYPE_DEFINITION,
294
+ description: (_b = (_a = type.astNode) === null || _a === void 0 ? void 0 : _a.description) !== null && _b !== void 0 ? _b : (type.description
295
+ ? {
296
+ kind: graphql_1.Kind.STRING,
297
+ value: type.description,
298
+ block: true,
299
+ }
300
+ : undefined),
301
+ name: {
302
+ kind: graphql_1.Kind.NAME,
303
+ value: type.name,
304
+ },
305
+ fields: Object.values(type.getFields()).map(field => astFromInputField(field, schema, pathToDirectivesInExtensions)),
306
+ // ConstXNode has been introduced in v16 but it is not compatible with XNode so we do `as any` for backwards compatibility
307
+ directives: getDirectiveNodes(type, schema, pathToDirectivesInExtensions),
308
+ };
309
+ }
310
+ exports.astFromInputObjectType = astFromInputObjectType;
311
+ function astFromEnumType(type, schema, pathToDirectivesInExtensions) {
312
+ var _a, _b;
313
+ return {
314
+ kind: graphql_1.Kind.ENUM_TYPE_DEFINITION,
315
+ description: (_b = (_a = type.astNode) === null || _a === void 0 ? void 0 : _a.description) !== null && _b !== void 0 ? _b : (type.description
316
+ ? {
317
+ kind: graphql_1.Kind.STRING,
318
+ value: type.description,
319
+ block: true,
320
+ }
321
+ : undefined),
322
+ name: {
323
+ kind: graphql_1.Kind.NAME,
324
+ value: type.name,
325
+ },
326
+ values: Object.values(type.getValues()).map(value => astFromEnumValue(value, schema, pathToDirectivesInExtensions)),
327
+ // ConstXNode has been introduced in v16 but it is not compatible with XNode so we do `as any` for backwards compatibility
328
+ directives: getDirectiveNodes(type, schema, pathToDirectivesInExtensions),
329
+ };
330
+ }
331
+ exports.astFromEnumType = astFromEnumType;
332
+ function astFromScalarType(type, schema, pathToDirectivesInExtensions) {
333
+ var _a, _b, _c;
334
+ const directivesInExtensions = (0, get_directives_js_1.getDirectivesInExtensions)(type, pathToDirectivesInExtensions);
335
+ const directives = directivesInExtensions
336
+ ? makeDirectiveNodes(schema, directivesInExtensions)
337
+ : ((_a = type.astNode) === null || _a === void 0 ? void 0 : _a.directives) || [];
338
+ const specifiedByValue = (type['specifiedByUrl'] || type['specifiedByURL']);
339
+ if (specifiedByValue && !directives.some(directiveNode => directiveNode.name.value === 'specifiedBy')) {
340
+ const specifiedByArgs = {
341
+ url: specifiedByValue,
342
+ };
343
+ directives.push(makeDirectiveNode('specifiedBy', specifiedByArgs));
344
+ }
345
+ return {
346
+ kind: graphql_1.Kind.SCALAR_TYPE_DEFINITION,
347
+ description: (_c = (_b = type.astNode) === null || _b === void 0 ? void 0 : _b.description) !== null && _c !== void 0 ? _c : (type.description
348
+ ? {
349
+ kind: graphql_1.Kind.STRING,
350
+ value: type.description,
351
+ block: true,
352
+ }
353
+ : undefined),
354
+ name: {
355
+ kind: graphql_1.Kind.NAME,
356
+ value: type.name,
357
+ },
358
+ // ConstXNode has been introduced in v16 but it is not compatible with XNode so we do `as any` for backwards compatibility
359
+ directives: directives,
360
+ };
361
+ }
362
+ exports.astFromScalarType = astFromScalarType;
363
+ function astFromField(field, schema, pathToDirectivesInExtensions) {
364
+ var _a, _b;
365
+ return {
366
+ kind: graphql_1.Kind.FIELD_DEFINITION,
367
+ description: (_b = (_a = field.astNode) === null || _a === void 0 ? void 0 : _a.description) !== null && _b !== void 0 ? _b : (field.description
368
+ ? {
369
+ kind: graphql_1.Kind.STRING,
370
+ value: field.description,
371
+ block: true,
372
+ }
373
+ : undefined),
374
+ name: {
375
+ kind: graphql_1.Kind.NAME,
376
+ value: field.name,
377
+ },
378
+ arguments: field.args.map(arg => astFromArg(arg, schema, pathToDirectivesInExtensions)),
379
+ type: (0, astFromType_js_1.astFromType)(field.type),
380
+ // ConstXNode has been introduced in v16 but it is not compatible with XNode so we do `as any` for backwards compatibility
381
+ directives: getDeprecatableDirectiveNodes(field, schema, pathToDirectivesInExtensions),
382
+ };
383
+ }
384
+ exports.astFromField = astFromField;
385
+ function astFromInputField(field, schema, pathToDirectivesInExtensions) {
386
+ var _a, _b, _c;
387
+ return {
388
+ kind: graphql_1.Kind.INPUT_VALUE_DEFINITION,
389
+ description: (_b = (_a = field.astNode) === null || _a === void 0 ? void 0 : _a.description) !== null && _b !== void 0 ? _b : (field.description
390
+ ? {
391
+ kind: graphql_1.Kind.STRING,
392
+ value: field.description,
393
+ block: true,
394
+ }
395
+ : undefined),
396
+ name: {
397
+ kind: graphql_1.Kind.NAME,
398
+ value: field.name,
399
+ },
400
+ type: (0, astFromType_js_1.astFromType)(field.type),
401
+ // ConstXNode has been introduced in v16 but it is not compatible with XNode so we do `as any` for backwards compatibility
402
+ directives: getDeprecatableDirectiveNodes(field, schema, pathToDirectivesInExtensions),
403
+ defaultValue: (_c = (0, graphql_1.astFromValue)(field.defaultValue, field.type)) !== null && _c !== void 0 ? _c : undefined,
404
+ };
405
+ }
406
+ exports.astFromInputField = astFromInputField;
407
+ function astFromEnumValue(value, schema, pathToDirectivesInExtensions) {
408
+ var _a, _b;
409
+ return {
410
+ kind: graphql_1.Kind.ENUM_VALUE_DEFINITION,
411
+ description: (_b = (_a = value.astNode) === null || _a === void 0 ? void 0 : _a.description) !== null && _b !== void 0 ? _b : (value.description
412
+ ? {
413
+ kind: graphql_1.Kind.STRING,
414
+ value: value.description,
415
+ block: true,
416
+ }
417
+ : undefined),
418
+ name: {
419
+ kind: graphql_1.Kind.NAME,
420
+ value: value.name,
421
+ },
422
+ // ConstXNode has been introduced in v16 but it is not compatible with XNode so we do `as any` for backwards compatibility
423
+ directives: getDeprecatableDirectiveNodes(value, schema, pathToDirectivesInExtensions),
424
+ };
425
+ }
426
+ exports.astFromEnumValue = astFromEnumValue;
427
+ function makeDeprecatedDirective(deprecationReason) {
428
+ return makeDirectiveNode('deprecated', { reason: deprecationReason }, graphql_1.GraphQLDeprecatedDirective);
429
+ }
430
+ exports.makeDeprecatedDirective = makeDeprecatedDirective;
431
+ function makeDirectiveNode(name, args, directive) {
432
+ const directiveArguments = [];
433
+ if (directive != null) {
434
+ for (const arg of directive.args) {
435
+ const argName = arg.name;
436
+ const argValue = args[argName];
437
+ if (argValue !== undefined) {
438
+ const value = (0, graphql_1.astFromValue)(argValue, arg.type);
439
+ if (value) {
440
+ directiveArguments.push({
441
+ kind: graphql_1.Kind.ARGUMENT,
442
+ name: {
443
+ kind: graphql_1.Kind.NAME,
444
+ value: argName,
445
+ },
446
+ value,
447
+ });
448
+ }
449
+ }
450
+ }
451
+ }
452
+ else {
453
+ for (const argName in args) {
454
+ const argValue = args[argName];
455
+ const value = (0, astFromValueUntyped_js_1.astFromValueUntyped)(argValue);
456
+ if (value) {
457
+ directiveArguments.push({
458
+ kind: graphql_1.Kind.ARGUMENT,
459
+ name: {
460
+ kind: graphql_1.Kind.NAME,
461
+ value: argName,
462
+ },
463
+ value,
464
+ });
465
+ }
466
+ }
467
+ }
468
+ return {
469
+ kind: graphql_1.Kind.DIRECTIVE,
470
+ name: {
471
+ kind: graphql_1.Kind.NAME,
472
+ value: name,
473
+ },
474
+ arguments: directiveArguments,
475
+ };
476
+ }
477
+ exports.makeDirectiveNode = makeDirectiveNode;
478
+ function makeDirectiveNodes(schema, directiveValues) {
479
+ const directiveNodes = [];
480
+ for (const directiveName in directiveValues) {
481
+ const arrayOrSingleValue = directiveValues[directiveName];
482
+ const directive = schema === null || schema === void 0 ? void 0 : schema.getDirective(directiveName);
483
+ if (Array.isArray(arrayOrSingleValue)) {
484
+ for (const value of arrayOrSingleValue) {
485
+ directiveNodes.push(makeDirectiveNode(directiveName, value, directive));
486
+ }
487
+ }
488
+ else {
489
+ directiveNodes.push(makeDirectiveNode(directiveName, arrayOrSingleValue, directive));
490
+ }
491
+ }
492
+ return directiveNodes;
493
+ }
494
+ exports.makeDirectiveNodes = makeDirectiveNodes;
package/cjs/prune.js ADDED
@@ -0,0 +1,133 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.pruneSchema = void 0;
4
+ const graphql_1 = require("graphql");
5
+ const mapSchema_js_1 = require("./mapSchema.js");
6
+ const Interfaces_js_1 = require("./Interfaces.js");
7
+ const rootTypes_js_1 = require("./rootTypes.js");
8
+ const get_implementing_types_js_1 = require("./get-implementing-types.js");
9
+ /**
10
+ * Prunes the provided schema, removing unused and empty types
11
+ * @param schema The schema to prune
12
+ * @param options Additional options for removing unused types from the schema
13
+ */
14
+ function pruneSchema(schema, options = {}) {
15
+ const { skipEmptyCompositeTypePruning, skipEmptyUnionPruning, skipPruning, skipUnimplementedInterfacesPruning, skipUnusedTypesPruning, } = options;
16
+ let prunedTypes = []; // Pruned types during mapping
17
+ let prunedSchema = schema;
18
+ do {
19
+ let visited = visitSchema(prunedSchema);
20
+ // Custom pruning was defined, so we need to pre-emptively revisit the schema accounting for this
21
+ if (skipPruning) {
22
+ const revisit = [];
23
+ for (const typeName in prunedSchema.getTypeMap()) {
24
+ if (typeName.startsWith('__')) {
25
+ continue;
26
+ }
27
+ const type = prunedSchema.getType(typeName);
28
+ // if we want to skip pruning for this type, add it to the list of types to revisit
29
+ if (type && skipPruning(type)) {
30
+ revisit.push(typeName);
31
+ }
32
+ }
33
+ visited = visitQueue(revisit, prunedSchema, visited); // visit again
34
+ }
35
+ prunedTypes = [];
36
+ prunedSchema = (0, mapSchema_js_1.mapSchema)(prunedSchema, {
37
+ [Interfaces_js_1.MapperKind.TYPE]: type => {
38
+ if (!visited.has(type.name) && !(0, graphql_1.isSpecifiedScalarType)(type)) {
39
+ if ((0, graphql_1.isUnionType)(type) ||
40
+ (0, graphql_1.isInputObjectType)(type) ||
41
+ (0, graphql_1.isInterfaceType)(type) ||
42
+ (0, graphql_1.isObjectType)(type) ||
43
+ (0, graphql_1.isScalarType)(type)) {
44
+ // skipUnusedTypesPruning: skip pruning unused types
45
+ if (skipUnusedTypesPruning) {
46
+ return type;
47
+ }
48
+ // skipEmptyUnionPruning: skip pruning empty unions
49
+ if ((0, graphql_1.isUnionType)(type) && skipEmptyUnionPruning && !Object.keys(type.getTypes()).length) {
50
+ return type;
51
+ }
52
+ if ((0, graphql_1.isInputObjectType)(type) || (0, graphql_1.isInterfaceType)(type) || (0, graphql_1.isObjectType)(type)) {
53
+ // skipEmptyCompositeTypePruning: skip pruning object types or interfaces with no fields
54
+ if (skipEmptyCompositeTypePruning && !Object.keys(type.getFields()).length) {
55
+ return type;
56
+ }
57
+ }
58
+ // skipUnimplementedInterfacesPruning: skip pruning interfaces that are not implemented by any other types
59
+ if ((0, graphql_1.isInterfaceType)(type) && skipUnimplementedInterfacesPruning) {
60
+ return type;
61
+ }
62
+ }
63
+ prunedTypes.push(type.name);
64
+ visited.delete(type.name);
65
+ return null;
66
+ }
67
+ return type;
68
+ },
69
+ });
70
+ } while (prunedTypes.length); // Might have empty types and need to prune again
71
+ return prunedSchema;
72
+ }
73
+ exports.pruneSchema = pruneSchema;
74
+ function visitSchema(schema) {
75
+ const queue = []; // queue of nodes to visit
76
+ // Grab the root types and start there
77
+ for (const type of (0, rootTypes_js_1.getRootTypes)(schema)) {
78
+ queue.push(type.name);
79
+ }
80
+ return visitQueue(queue, schema);
81
+ }
82
+ function visitQueue(queue, schema, visited = new Set()) {
83
+ // Interfaces encountered that are field return types need to be revisited to add their implementations
84
+ const revisit = new Map();
85
+ // Navigate all types starting with pre-queued types (root types)
86
+ while (queue.length) {
87
+ const typeName = queue.pop();
88
+ // Skip types we already visited unless it is an interface type that needs revisiting
89
+ if (visited.has(typeName) && revisit[typeName] !== true) {
90
+ continue;
91
+ }
92
+ const type = schema.getType(typeName);
93
+ if (type) {
94
+ // Get types for union
95
+ if ((0, graphql_1.isUnionType)(type)) {
96
+ queue.push(...type.getTypes().map(type => type.name));
97
+ }
98
+ // If it is an interface and it is a returned type, grab all implementations so we can use proper __typename in fragments
99
+ if ((0, graphql_1.isInterfaceType)(type) && revisit[typeName] === true) {
100
+ queue.push(...(0, get_implementing_types_js_1.getImplementingTypes)(type.name, schema));
101
+ // No need to revisit this interface again
102
+ revisit[typeName] = false;
103
+ }
104
+ // Visit interfaces this type is implementing if they haven't been visited yet
105
+ if ('getInterfaces' in type) {
106
+ // Only pushes to queue to visit but not return types
107
+ queue.push(...type.getInterfaces().map(iface => iface.name));
108
+ }
109
+ // If the type has files visit those field types
110
+ if ('getFields' in type) {
111
+ const fields = type.getFields();
112
+ const entries = Object.entries(fields);
113
+ if (!entries.length) {
114
+ continue;
115
+ }
116
+ for (const [, field] of entries) {
117
+ if ((0, graphql_1.isObjectType)(type)) {
118
+ // Visit arg types
119
+ queue.push(...field.args.map(arg => (0, graphql_1.getNamedType)(arg.type).name));
120
+ }
121
+ const namedType = (0, graphql_1.getNamedType)(field.type);
122
+ queue.push(namedType.name);
123
+ // Interfaces returned on fields need to be revisited to add their implementations
124
+ if ((0, graphql_1.isInterfaceType)(namedType) && !(namedType.name in revisit)) {
125
+ revisit[namedType.name] = true;
126
+ }
127
+ }
128
+ }
129
+ visited.add(typeName); // Mark as visited (and therefore it is used and should be kept)
130
+ }
131
+ }
132
+ return visited;
133
+ }