@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
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isAggregateError = exports.AggregateError = void 0;
4
+ let AggregateErrorImpl;
5
+ exports.AggregateError = AggregateErrorImpl;
6
+ if (typeof AggregateError === 'undefined') {
7
+ class AggregateErrorClass extends Error {
8
+ constructor(errors, message = '') {
9
+ super(message);
10
+ this.errors = errors;
11
+ this.name = 'AggregateError';
12
+ Error.captureStackTrace(this, AggregateErrorClass);
13
+ }
14
+ }
15
+ exports.AggregateError = AggregateErrorImpl = function (errors, message) {
16
+ return new AggregateErrorClass(errors, message);
17
+ };
18
+ }
19
+ else {
20
+ exports.AggregateError = AggregateErrorImpl = AggregateError;
21
+ }
22
+ function isAggregateError(error) {
23
+ return 'errors' in error && Array.isArray(error['errors']);
24
+ }
25
+ exports.isAggregateError = isAggregateError;
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.MapperKind = void 0;
4
+ var MapperKind;
5
+ (function (MapperKind) {
6
+ MapperKind["TYPE"] = "MapperKind.TYPE";
7
+ MapperKind["SCALAR_TYPE"] = "MapperKind.SCALAR_TYPE";
8
+ MapperKind["ENUM_TYPE"] = "MapperKind.ENUM_TYPE";
9
+ MapperKind["COMPOSITE_TYPE"] = "MapperKind.COMPOSITE_TYPE";
10
+ MapperKind["OBJECT_TYPE"] = "MapperKind.OBJECT_TYPE";
11
+ MapperKind["INPUT_OBJECT_TYPE"] = "MapperKind.INPUT_OBJECT_TYPE";
12
+ MapperKind["ABSTRACT_TYPE"] = "MapperKind.ABSTRACT_TYPE";
13
+ MapperKind["UNION_TYPE"] = "MapperKind.UNION_TYPE";
14
+ MapperKind["INTERFACE_TYPE"] = "MapperKind.INTERFACE_TYPE";
15
+ MapperKind["ROOT_OBJECT"] = "MapperKind.ROOT_OBJECT";
16
+ MapperKind["QUERY"] = "MapperKind.QUERY";
17
+ MapperKind["MUTATION"] = "MapperKind.MUTATION";
18
+ MapperKind["SUBSCRIPTION"] = "MapperKind.SUBSCRIPTION";
19
+ MapperKind["DIRECTIVE"] = "MapperKind.DIRECTIVE";
20
+ MapperKind["FIELD"] = "MapperKind.FIELD";
21
+ MapperKind["COMPOSITE_FIELD"] = "MapperKind.COMPOSITE_FIELD";
22
+ MapperKind["OBJECT_FIELD"] = "MapperKind.OBJECT_FIELD";
23
+ MapperKind["ROOT_FIELD"] = "MapperKind.ROOT_FIELD";
24
+ MapperKind["QUERY_ROOT_FIELD"] = "MapperKind.QUERY_ROOT_FIELD";
25
+ MapperKind["MUTATION_ROOT_FIELD"] = "MapperKind.MUTATION_ROOT_FIELD";
26
+ MapperKind["SUBSCRIPTION_ROOT_FIELD"] = "MapperKind.SUBSCRIPTION_ROOT_FIELD";
27
+ MapperKind["INTERFACE_FIELD"] = "MapperKind.INTERFACE_FIELD";
28
+ MapperKind["INPUT_OBJECT_FIELD"] = "MapperKind.INPUT_OBJECT_FIELD";
29
+ MapperKind["ARGUMENT"] = "MapperKind.ARGUMENT";
30
+ MapperKind["ENUM_VALUE"] = "MapperKind.ENUM_VALUE";
31
+ })(MapperKind = exports.MapperKind || (exports.MapperKind = {}));
@@ -0,0 +1,62 @@
1
+ "use strict";
2
+ // addTypes uses toConfig to create a new schema with a new or replaced
3
+ // type or directive. Rewiring is employed so that the replaced type can be
4
+ // reconnected with the existing types.
5
+ //
6
+ // Rewiring is employed even for new types or directives as a convenience, so
7
+ // that type references within the new type or directive do not have to be to
8
+ // the identical objects within the original schema.
9
+ //
10
+ // In fact, the type references could even be stub types with entirely different
11
+ // fields, as long as the type references share the same name as the desired
12
+ // type within the original schema's type map.
13
+ //
14
+ // This makes it easy to perform simple schema operations (e.g. adding a new
15
+ // type with a fiew fields removed from an existing type) that could normally be
16
+ // performed by using toConfig directly, but is blocked if any intervening
17
+ // more advanced schema operations have caused the types to be recreated via
18
+ // rewiring.
19
+ //
20
+ // Type recreation happens, for example, with every use of mapSchema, as the
21
+ // types are always rewired. If fields are selected and removed using
22
+ // mapSchema, adding those fields to a new type can no longer be simply done
23
+ // by toConfig, as the types are not the identical JavaScript objects, and
24
+ // schema creation will fail with errors referencing multiple types with the
25
+ // same names.
26
+ //
27
+ // enhanceSchema can fill this gap by adding an additional round of rewiring.
28
+ //
29
+ Object.defineProperty(exports, "__esModule", { value: true });
30
+ exports.addTypes = void 0;
31
+ const graphql_1 = require("graphql");
32
+ const getObjectTypeFromTypeMap_js_1 = require("./getObjectTypeFromTypeMap.js");
33
+ const rewire_js_1 = require("./rewire.js");
34
+ function addTypes(schema, newTypesOrDirectives) {
35
+ const config = schema.toConfig();
36
+ const originalTypeMap = {};
37
+ for (const type of config.types) {
38
+ originalTypeMap[type.name] = type;
39
+ }
40
+ const originalDirectiveMap = {};
41
+ for (const directive of config.directives) {
42
+ originalDirectiveMap[directive.name] = directive;
43
+ }
44
+ for (const newTypeOrDirective of newTypesOrDirectives) {
45
+ if ((0, graphql_1.isNamedType)(newTypeOrDirective)) {
46
+ originalTypeMap[newTypeOrDirective.name] = newTypeOrDirective;
47
+ }
48
+ else if ((0, graphql_1.isDirective)(newTypeOrDirective)) {
49
+ originalDirectiveMap[newTypeOrDirective.name] = newTypeOrDirective;
50
+ }
51
+ }
52
+ const { typeMap, directives } = (0, rewire_js_1.rewireTypes)(originalTypeMap, Object.values(originalDirectiveMap));
53
+ return new graphql_1.GraphQLSchema({
54
+ ...config,
55
+ query: (0, getObjectTypeFromTypeMap_js_1.getObjectTypeFromTypeMap)(typeMap, schema.getQueryType()),
56
+ mutation: (0, getObjectTypeFromTypeMap_js_1.getObjectTypeFromTypeMap)(typeMap, schema.getMutationType()),
57
+ subscription: (0, getObjectTypeFromTypeMap_js_1.getObjectTypeFromTypeMap)(typeMap, schema.getSubscriptionType()),
58
+ types: Object.values(typeMap),
59
+ directives,
60
+ });
61
+ }
62
+ exports.addTypes = addTypes;
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.astFromType = void 0;
4
+ const graphql_1 = require("graphql");
5
+ const inspect_js_1 = require("./inspect.js");
6
+ function astFromType(type) {
7
+ if ((0, graphql_1.isNonNullType)(type)) {
8
+ const innerType = astFromType(type.ofType);
9
+ if (innerType.kind === graphql_1.Kind.NON_NULL_TYPE) {
10
+ throw new Error(`Invalid type node ${(0, inspect_js_1.inspect)(type)}. Inner type of non-null type cannot be a non-null type.`);
11
+ }
12
+ return {
13
+ kind: graphql_1.Kind.NON_NULL_TYPE,
14
+ type: innerType,
15
+ };
16
+ }
17
+ else if ((0, graphql_1.isListType)(type)) {
18
+ return {
19
+ kind: graphql_1.Kind.LIST_TYPE,
20
+ type: astFromType(type.ofType),
21
+ };
22
+ }
23
+ return {
24
+ kind: graphql_1.Kind.NAMED_TYPE,
25
+ name: {
26
+ kind: graphql_1.Kind.NAME,
27
+ value: type.name,
28
+ },
29
+ };
30
+ }
31
+ exports.astFromType = astFromType;
@@ -0,0 +1,78 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.astFromValueUntyped = void 0;
4
+ const graphql_1 = require("graphql");
5
+ /**
6
+ * Produces a GraphQL Value AST given a JavaScript object.
7
+ * Function will match JavaScript/JSON values to GraphQL AST schema format
8
+ * by using the following mapping.
9
+ *
10
+ * | JSON Value | GraphQL Value |
11
+ * | ------------- | -------------------- |
12
+ * | Object | Input Object |
13
+ * | Array | List |
14
+ * | Boolean | Boolean |
15
+ * | String | String |
16
+ * | Number | Int / Float |
17
+ * | null | NullValue |
18
+ *
19
+ */
20
+ function astFromValueUntyped(value) {
21
+ // only explicit null, not undefined, NaN
22
+ if (value === null) {
23
+ return { kind: graphql_1.Kind.NULL };
24
+ }
25
+ // undefined
26
+ if (value === undefined) {
27
+ return null;
28
+ }
29
+ // Convert JavaScript array to GraphQL list. If the GraphQLType is a list, but
30
+ // the value is not an array, convert the value using the list's item type.
31
+ if (Array.isArray(value)) {
32
+ const valuesNodes = [];
33
+ for (const item of value) {
34
+ const itemNode = astFromValueUntyped(item);
35
+ if (itemNode != null) {
36
+ valuesNodes.push(itemNode);
37
+ }
38
+ }
39
+ return { kind: graphql_1.Kind.LIST, values: valuesNodes };
40
+ }
41
+ if (typeof value === 'object') {
42
+ const fieldNodes = [];
43
+ for (const fieldName in value) {
44
+ const fieldValue = value[fieldName];
45
+ const ast = astFromValueUntyped(fieldValue);
46
+ if (ast) {
47
+ fieldNodes.push({
48
+ kind: graphql_1.Kind.OBJECT_FIELD,
49
+ name: { kind: graphql_1.Kind.NAME, value: fieldName },
50
+ value: ast,
51
+ });
52
+ }
53
+ }
54
+ return { kind: graphql_1.Kind.OBJECT, fields: fieldNodes };
55
+ }
56
+ // Others serialize based on their corresponding JavaScript scalar types.
57
+ if (typeof value === 'boolean') {
58
+ return { kind: graphql_1.Kind.BOOLEAN, value };
59
+ }
60
+ // JavaScript numbers can be Int or Float values.
61
+ if (typeof value === 'number' && isFinite(value)) {
62
+ const stringNum = String(value);
63
+ return integerStringRegExp.test(stringNum)
64
+ ? { kind: graphql_1.Kind.INT, value: stringNum }
65
+ : { kind: graphql_1.Kind.FLOAT, value: stringNum };
66
+ }
67
+ if (typeof value === 'string') {
68
+ return { kind: graphql_1.Kind.STRING, value };
69
+ }
70
+ throw new TypeError(`Cannot convert value to AST: ${value}.`);
71
+ }
72
+ exports.astFromValueUntyped = astFromValueUntyped;
73
+ /**
74
+ * IntValue:
75
+ * - NegativeSign? 0
76
+ * - NegativeSign? NonZeroDigit ( Digit+ )?
77
+ */
78
+ const integerStringRegExp = /^-?(?:0|[1-9][0-9]*)$/;
@@ -0,0 +1,351 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.buildOperationNodeForField = void 0;
4
+ const graphql_1 = require("graphql");
5
+ const rootTypes_js_1 = require("./rootTypes.js");
6
+ let operationVariables = [];
7
+ let fieldTypeMap = new Map();
8
+ function addOperationVariable(variable) {
9
+ operationVariables.push(variable);
10
+ }
11
+ function resetOperationVariables() {
12
+ operationVariables = [];
13
+ }
14
+ function resetFieldMap() {
15
+ fieldTypeMap = new Map();
16
+ }
17
+ function buildOperationNodeForField({ schema, kind, field, models, ignore = [], depthLimit, circularReferenceDepth, argNames, selectedFields = true, }) {
18
+ resetOperationVariables();
19
+ resetFieldMap();
20
+ const rootTypeNames = (0, rootTypes_js_1.getRootTypeNames)(schema);
21
+ const operationNode = buildOperationAndCollectVariables({
22
+ schema,
23
+ fieldName: field,
24
+ kind,
25
+ models: models || [],
26
+ ignore,
27
+ depthLimit: depthLimit || Infinity,
28
+ circularReferenceDepth: circularReferenceDepth || 1,
29
+ argNames,
30
+ selectedFields,
31
+ rootTypeNames,
32
+ });
33
+ // attach variables
34
+ operationNode.variableDefinitions = [...operationVariables];
35
+ resetOperationVariables();
36
+ resetFieldMap();
37
+ return operationNode;
38
+ }
39
+ exports.buildOperationNodeForField = buildOperationNodeForField;
40
+ function buildOperationAndCollectVariables({ schema, fieldName, kind, models, ignore, depthLimit, circularReferenceDepth, argNames, selectedFields, rootTypeNames, }) {
41
+ const type = (0, rootTypes_js_1.getDefinedRootType)(schema, kind);
42
+ const field = type.getFields()[fieldName];
43
+ const operationName = `${fieldName}_${kind}`;
44
+ if (field.args) {
45
+ for (const arg of field.args) {
46
+ const argName = arg.name;
47
+ if (!argNames || argNames.includes(argName)) {
48
+ addOperationVariable(resolveVariable(arg, argName));
49
+ }
50
+ }
51
+ }
52
+ return {
53
+ kind: graphql_1.Kind.OPERATION_DEFINITION,
54
+ operation: kind,
55
+ name: {
56
+ kind: graphql_1.Kind.NAME,
57
+ value: operationName,
58
+ },
59
+ variableDefinitions: [],
60
+ selectionSet: {
61
+ kind: graphql_1.Kind.SELECTION_SET,
62
+ selections: [
63
+ resolveField({
64
+ type,
65
+ field,
66
+ models,
67
+ firstCall: true,
68
+ path: [],
69
+ ancestors: [],
70
+ ignore,
71
+ depthLimit,
72
+ circularReferenceDepth,
73
+ schema,
74
+ depth: 0,
75
+ argNames,
76
+ selectedFields,
77
+ rootTypeNames,
78
+ }),
79
+ ],
80
+ },
81
+ };
82
+ }
83
+ function resolveSelectionSet({ parent, type, models, firstCall, path, ancestors, ignore, depthLimit, circularReferenceDepth, schema, depth, argNames, selectedFields, rootTypeNames, }) {
84
+ if (typeof selectedFields === 'boolean' && depth > depthLimit) {
85
+ return;
86
+ }
87
+ if ((0, graphql_1.isUnionType)(type)) {
88
+ const types = type.getTypes();
89
+ return {
90
+ kind: graphql_1.Kind.SELECTION_SET,
91
+ selections: types
92
+ .filter(t => !hasCircularRef([...ancestors, t], {
93
+ depth: circularReferenceDepth,
94
+ }))
95
+ .map(t => {
96
+ return {
97
+ kind: graphql_1.Kind.INLINE_FRAGMENT,
98
+ typeCondition: {
99
+ kind: graphql_1.Kind.NAMED_TYPE,
100
+ name: {
101
+ kind: graphql_1.Kind.NAME,
102
+ value: t.name,
103
+ },
104
+ },
105
+ selectionSet: resolveSelectionSet({
106
+ parent: type,
107
+ type: t,
108
+ models,
109
+ path,
110
+ ancestors,
111
+ ignore,
112
+ depthLimit,
113
+ circularReferenceDepth,
114
+ schema,
115
+ depth,
116
+ argNames,
117
+ selectedFields,
118
+ rootTypeNames,
119
+ }),
120
+ };
121
+ })
122
+ .filter(fragmentNode => { var _a, _b; return ((_b = (_a = fragmentNode === null || fragmentNode === void 0 ? void 0 : fragmentNode.selectionSet) === null || _a === void 0 ? void 0 : _a.selections) === null || _b === void 0 ? void 0 : _b.length) > 0; }),
123
+ };
124
+ }
125
+ if ((0, graphql_1.isInterfaceType)(type)) {
126
+ const types = Object.values(schema.getTypeMap()).filter((t) => (0, graphql_1.isObjectType)(t) && t.getInterfaces().includes(type));
127
+ return {
128
+ kind: graphql_1.Kind.SELECTION_SET,
129
+ selections: types
130
+ .filter(t => !hasCircularRef([...ancestors, t], {
131
+ depth: circularReferenceDepth,
132
+ }))
133
+ .map(t => {
134
+ return {
135
+ kind: graphql_1.Kind.INLINE_FRAGMENT,
136
+ typeCondition: {
137
+ kind: graphql_1.Kind.NAMED_TYPE,
138
+ name: {
139
+ kind: graphql_1.Kind.NAME,
140
+ value: t.name,
141
+ },
142
+ },
143
+ selectionSet: resolveSelectionSet({
144
+ parent: type,
145
+ type: t,
146
+ models,
147
+ path,
148
+ ancestors,
149
+ ignore,
150
+ depthLimit,
151
+ circularReferenceDepth,
152
+ schema,
153
+ depth,
154
+ argNames,
155
+ selectedFields,
156
+ rootTypeNames,
157
+ }),
158
+ };
159
+ })
160
+ .filter(fragmentNode => { var _a, _b; return ((_b = (_a = fragmentNode === null || fragmentNode === void 0 ? void 0 : fragmentNode.selectionSet) === null || _a === void 0 ? void 0 : _a.selections) === null || _b === void 0 ? void 0 : _b.length) > 0; }),
161
+ };
162
+ }
163
+ if ((0, graphql_1.isObjectType)(type) && !rootTypeNames.has(type.name)) {
164
+ const isIgnored = ignore.includes(type.name) || ignore.includes(`${parent.name}.${path[path.length - 1]}`);
165
+ const isModel = models.includes(type.name);
166
+ if (!firstCall && isModel && !isIgnored) {
167
+ return {
168
+ kind: graphql_1.Kind.SELECTION_SET,
169
+ selections: [
170
+ {
171
+ kind: graphql_1.Kind.FIELD,
172
+ name: {
173
+ kind: graphql_1.Kind.NAME,
174
+ value: 'id',
175
+ },
176
+ },
177
+ ],
178
+ };
179
+ }
180
+ const fields = type.getFields();
181
+ return {
182
+ kind: graphql_1.Kind.SELECTION_SET,
183
+ selections: Object.keys(fields)
184
+ .filter(fieldName => {
185
+ return !hasCircularRef([...ancestors, (0, graphql_1.getNamedType)(fields[fieldName].type)], {
186
+ depth: circularReferenceDepth,
187
+ });
188
+ })
189
+ .map(fieldName => {
190
+ const selectedSubFields = typeof selectedFields === 'object' ? selectedFields[fieldName] : true;
191
+ if (selectedSubFields) {
192
+ return resolveField({
193
+ type,
194
+ field: fields[fieldName],
195
+ models,
196
+ path: [...path, fieldName],
197
+ ancestors,
198
+ ignore,
199
+ depthLimit,
200
+ circularReferenceDepth,
201
+ schema,
202
+ depth,
203
+ argNames,
204
+ selectedFields: selectedSubFields,
205
+ rootTypeNames,
206
+ });
207
+ }
208
+ return null;
209
+ })
210
+ .filter((f) => {
211
+ var _a, _b;
212
+ if (f == null) {
213
+ return false;
214
+ }
215
+ else if ('selectionSet' in f) {
216
+ return !!((_b = (_a = f.selectionSet) === null || _a === void 0 ? void 0 : _a.selections) === null || _b === void 0 ? void 0 : _b.length);
217
+ }
218
+ return true;
219
+ }),
220
+ };
221
+ }
222
+ }
223
+ function resolveVariable(arg, name) {
224
+ function resolveVariableType(type) {
225
+ if ((0, graphql_1.isListType)(type)) {
226
+ return {
227
+ kind: graphql_1.Kind.LIST_TYPE,
228
+ type: resolveVariableType(type.ofType),
229
+ };
230
+ }
231
+ if ((0, graphql_1.isNonNullType)(type)) {
232
+ return {
233
+ kind: graphql_1.Kind.NON_NULL_TYPE,
234
+ // for v16 compatibility
235
+ type: resolveVariableType(type.ofType),
236
+ };
237
+ }
238
+ return {
239
+ kind: graphql_1.Kind.NAMED_TYPE,
240
+ name: {
241
+ kind: graphql_1.Kind.NAME,
242
+ value: type.name,
243
+ },
244
+ };
245
+ }
246
+ return {
247
+ kind: graphql_1.Kind.VARIABLE_DEFINITION,
248
+ variable: {
249
+ kind: graphql_1.Kind.VARIABLE,
250
+ name: {
251
+ kind: graphql_1.Kind.NAME,
252
+ value: name || arg.name,
253
+ },
254
+ },
255
+ type: resolveVariableType(arg.type),
256
+ };
257
+ }
258
+ function getArgumentName(name, path) {
259
+ return [...path, name].join('_');
260
+ }
261
+ function resolveField({ type, field, models, firstCall, path, ancestors, ignore, depthLimit, circularReferenceDepth, schema, depth, argNames, selectedFields, rootTypeNames, }) {
262
+ const namedType = (0, graphql_1.getNamedType)(field.type);
263
+ let args = [];
264
+ let removeField = false;
265
+ if (field.args && field.args.length) {
266
+ args = field.args
267
+ .map(arg => {
268
+ const argumentName = getArgumentName(arg.name, path);
269
+ if (argNames && !argNames.includes(argumentName)) {
270
+ if ((0, graphql_1.isNonNullType)(arg.type)) {
271
+ removeField = true;
272
+ }
273
+ return null;
274
+ }
275
+ if (!firstCall) {
276
+ addOperationVariable(resolveVariable(arg, argumentName));
277
+ }
278
+ return {
279
+ kind: graphql_1.Kind.ARGUMENT,
280
+ name: {
281
+ kind: graphql_1.Kind.NAME,
282
+ value: arg.name,
283
+ },
284
+ value: {
285
+ kind: graphql_1.Kind.VARIABLE,
286
+ name: {
287
+ kind: graphql_1.Kind.NAME,
288
+ value: getArgumentName(arg.name, path),
289
+ },
290
+ },
291
+ };
292
+ })
293
+ .filter(Boolean);
294
+ }
295
+ if (removeField) {
296
+ return null;
297
+ }
298
+ const fieldPath = [...path, field.name];
299
+ const fieldPathStr = fieldPath.join('.');
300
+ let fieldName = field.name;
301
+ if (fieldTypeMap.has(fieldPathStr) && fieldTypeMap.get(fieldPathStr) !== field.type.toString()) {
302
+ fieldName += field.type.toString().replace('!', 'NonNull');
303
+ }
304
+ fieldTypeMap.set(fieldPathStr, field.type.toString());
305
+ if (!(0, graphql_1.isScalarType)(namedType) && !(0, graphql_1.isEnumType)(namedType)) {
306
+ return {
307
+ kind: graphql_1.Kind.FIELD,
308
+ name: {
309
+ kind: graphql_1.Kind.NAME,
310
+ value: field.name,
311
+ },
312
+ ...(fieldName !== field.name && { alias: { kind: graphql_1.Kind.NAME, value: fieldName } }),
313
+ selectionSet: resolveSelectionSet({
314
+ parent: type,
315
+ type: namedType,
316
+ models,
317
+ firstCall,
318
+ path: fieldPath,
319
+ ancestors: [...ancestors, type],
320
+ ignore,
321
+ depthLimit,
322
+ circularReferenceDepth,
323
+ schema,
324
+ depth: depth + 1,
325
+ argNames,
326
+ selectedFields,
327
+ rootTypeNames,
328
+ }) || undefined,
329
+ arguments: args,
330
+ };
331
+ }
332
+ return {
333
+ kind: graphql_1.Kind.FIELD,
334
+ name: {
335
+ kind: graphql_1.Kind.NAME,
336
+ value: field.name,
337
+ },
338
+ ...(fieldName !== field.name && { alias: { kind: graphql_1.Kind.NAME, value: fieldName } }),
339
+ arguments: args,
340
+ };
341
+ }
342
+ function hasCircularRef(types, config = {
343
+ depth: 1,
344
+ }) {
345
+ const type = types[types.length - 1];
346
+ if ((0, graphql_1.isScalarType)(type)) {
347
+ return false;
348
+ }
349
+ const size = types.filter(t => t.name === type.name).length;
350
+ return size > config.depth;
351
+ }
@@ -0,0 +1,98 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.collectSubFields = exports.collectFields = void 0;
4
+ const memoize_js_1 = require("./memoize.js");
5
+ const graphql_1 = require("graphql");
6
+ // Taken from GraphQL-JS v16 for backwards compat
7
+ function collectFields(schema, fragments, variableValues, runtimeType, selectionSet, fields, visitedFragmentNames) {
8
+ for (const selection of selectionSet.selections) {
9
+ switch (selection.kind) {
10
+ case graphql_1.Kind.FIELD: {
11
+ if (!shouldIncludeNode(variableValues, selection)) {
12
+ continue;
13
+ }
14
+ const name = getFieldEntryKey(selection);
15
+ const fieldList = fields.get(name);
16
+ if (fieldList !== undefined) {
17
+ fieldList.push(selection);
18
+ }
19
+ else {
20
+ fields.set(name, [selection]);
21
+ }
22
+ break;
23
+ }
24
+ case graphql_1.Kind.INLINE_FRAGMENT: {
25
+ if (!shouldIncludeNode(variableValues, selection) ||
26
+ !doesFragmentConditionMatch(schema, selection, runtimeType)) {
27
+ continue;
28
+ }
29
+ collectFields(schema, fragments, variableValues, runtimeType, selection.selectionSet, fields, visitedFragmentNames);
30
+ break;
31
+ }
32
+ case graphql_1.Kind.FRAGMENT_SPREAD: {
33
+ const fragName = selection.name.value;
34
+ if (visitedFragmentNames.has(fragName) || !shouldIncludeNode(variableValues, selection)) {
35
+ continue;
36
+ }
37
+ visitedFragmentNames.add(fragName);
38
+ const fragment = fragments[fragName];
39
+ if (!fragment || !doesFragmentConditionMatch(schema, fragment, runtimeType)) {
40
+ continue;
41
+ }
42
+ collectFields(schema, fragments, variableValues, runtimeType, fragment.selectionSet, fields, visitedFragmentNames);
43
+ break;
44
+ }
45
+ }
46
+ }
47
+ return fields;
48
+ }
49
+ exports.collectFields = collectFields;
50
+ /**
51
+ * Determines if a field should be included based on the `@include` and `@skip`
52
+ * directives, where `@skip` has higher precedence than `@include`.
53
+ */
54
+ function shouldIncludeNode(variableValues, node) {
55
+ const skip = (0, graphql_1.getDirectiveValues)(graphql_1.GraphQLSkipDirective, node, variableValues);
56
+ if ((skip === null || skip === void 0 ? void 0 : skip['if']) === true) {
57
+ return false;
58
+ }
59
+ const include = (0, graphql_1.getDirectiveValues)(graphql_1.GraphQLIncludeDirective, node, variableValues);
60
+ if ((include === null || include === void 0 ? void 0 : include['if']) === false) {
61
+ return false;
62
+ }
63
+ return true;
64
+ }
65
+ /**
66
+ * Determines if a fragment is applicable to the given type.
67
+ */
68
+ function doesFragmentConditionMatch(schema, fragment, type) {
69
+ const typeConditionNode = fragment.typeCondition;
70
+ if (!typeConditionNode) {
71
+ return true;
72
+ }
73
+ const conditionalType = (0, graphql_1.typeFromAST)(schema, typeConditionNode);
74
+ if (conditionalType === type) {
75
+ return true;
76
+ }
77
+ if ((0, graphql_1.isAbstractType)(conditionalType)) {
78
+ const possibleTypes = schema.getPossibleTypes(conditionalType);
79
+ return possibleTypes.includes(type);
80
+ }
81
+ return false;
82
+ }
83
+ /**
84
+ * Implements the logic to compute the key of a given field's entry
85
+ */
86
+ function getFieldEntryKey(node) {
87
+ return node.alias ? node.alias.value : node.name.value;
88
+ }
89
+ exports.collectSubFields = (0, memoize_js_1.memoize5)(function collectSubFields(schema, fragments, variableValues, type, fieldNodes) {
90
+ const subFieldNodes = new Map();
91
+ const visitedFragmentNames = new Set();
92
+ for (const fieldNode of fieldNodes) {
93
+ if (fieldNode.selectionSet) {
94
+ collectFields(schema, fragments, variableValues, type, fieldNode.selectionSet, subFieldNodes, visitedFragmentNames);
95
+ }
96
+ }
97
+ return subFieldNodes;
98
+ });