@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,148 @@
1
+ import { GraphQLEnumType, GraphQLInputObjectType, GraphQLInterfaceType, GraphQLObjectType, GraphQLScalarType, GraphQLUnionType, isEnumType, isInterfaceType, isInputObjectType, isObjectType, isScalarType, isUnionType, } from 'graphql';
2
+ export function renameType(type, newTypeName) {
3
+ if (isObjectType(type)) {
4
+ return new GraphQLObjectType({
5
+ ...type.toConfig(),
6
+ name: newTypeName,
7
+ astNode: type.astNode == null
8
+ ? type.astNode
9
+ : {
10
+ ...type.astNode,
11
+ name: {
12
+ ...type.astNode.name,
13
+ value: newTypeName,
14
+ },
15
+ },
16
+ extensionASTNodes: type.extensionASTNodes == null
17
+ ? type.extensionASTNodes
18
+ : type.extensionASTNodes.map(node => ({
19
+ ...node,
20
+ name: {
21
+ ...node.name,
22
+ value: newTypeName,
23
+ },
24
+ })),
25
+ });
26
+ }
27
+ else if (isInterfaceType(type)) {
28
+ return new GraphQLInterfaceType({
29
+ ...type.toConfig(),
30
+ name: newTypeName,
31
+ astNode: type.astNode == null
32
+ ? type.astNode
33
+ : {
34
+ ...type.astNode,
35
+ name: {
36
+ ...type.astNode.name,
37
+ value: newTypeName,
38
+ },
39
+ },
40
+ extensionASTNodes: type.extensionASTNodes == null
41
+ ? type.extensionASTNodes
42
+ : type.extensionASTNodes.map(node => ({
43
+ ...node,
44
+ name: {
45
+ ...node.name,
46
+ value: newTypeName,
47
+ },
48
+ })),
49
+ });
50
+ }
51
+ else if (isUnionType(type)) {
52
+ return new GraphQLUnionType({
53
+ ...type.toConfig(),
54
+ name: newTypeName,
55
+ astNode: type.astNode == null
56
+ ? type.astNode
57
+ : {
58
+ ...type.astNode,
59
+ name: {
60
+ ...type.astNode.name,
61
+ value: newTypeName,
62
+ },
63
+ },
64
+ extensionASTNodes: type.extensionASTNodes == null
65
+ ? type.extensionASTNodes
66
+ : type.extensionASTNodes.map(node => ({
67
+ ...node,
68
+ name: {
69
+ ...node.name,
70
+ value: newTypeName,
71
+ },
72
+ })),
73
+ });
74
+ }
75
+ else if (isInputObjectType(type)) {
76
+ return new GraphQLInputObjectType({
77
+ ...type.toConfig(),
78
+ name: newTypeName,
79
+ astNode: type.astNode == null
80
+ ? type.astNode
81
+ : {
82
+ ...type.astNode,
83
+ name: {
84
+ ...type.astNode.name,
85
+ value: newTypeName,
86
+ },
87
+ },
88
+ extensionASTNodes: type.extensionASTNodes == null
89
+ ? type.extensionASTNodes
90
+ : type.extensionASTNodes.map(node => ({
91
+ ...node,
92
+ name: {
93
+ ...node.name,
94
+ value: newTypeName,
95
+ },
96
+ })),
97
+ });
98
+ }
99
+ else if (isEnumType(type)) {
100
+ return new GraphQLEnumType({
101
+ ...type.toConfig(),
102
+ name: newTypeName,
103
+ astNode: type.astNode == null
104
+ ? type.astNode
105
+ : {
106
+ ...type.astNode,
107
+ name: {
108
+ ...type.astNode.name,
109
+ value: newTypeName,
110
+ },
111
+ },
112
+ extensionASTNodes: type.extensionASTNodes == null
113
+ ? type.extensionASTNodes
114
+ : type.extensionASTNodes.map(node => ({
115
+ ...node,
116
+ name: {
117
+ ...node.name,
118
+ value: newTypeName,
119
+ },
120
+ })),
121
+ });
122
+ }
123
+ else if (isScalarType(type)) {
124
+ return new GraphQLScalarType({
125
+ ...type.toConfig(),
126
+ name: newTypeName,
127
+ astNode: type.astNode == null
128
+ ? type.astNode
129
+ : {
130
+ ...type.astNode,
131
+ name: {
132
+ ...type.astNode.name,
133
+ value: newTypeName,
134
+ },
135
+ },
136
+ extensionASTNodes: type.extensionASTNodes == null
137
+ ? type.extensionASTNodes
138
+ : type.extensionASTNodes.map(node => ({
139
+ ...node,
140
+ name: {
141
+ ...node.name,
142
+ value: newTypeName,
143
+ },
144
+ })),
145
+ });
146
+ }
147
+ throw new Error(`Unknown type ${type}.`);
148
+ }
package/esm/rewire.js ADDED
@@ -0,0 +1,155 @@
1
+ import { GraphQLDirective, GraphQLEnumType, GraphQLInputObjectType, GraphQLInterfaceType, GraphQLList, GraphQLObjectType, GraphQLNonNull, GraphQLScalarType, GraphQLUnionType, isInterfaceType, isEnumType, isInputObjectType, isListType, isNamedType, isNonNullType, isObjectType, isScalarType, isUnionType, isSpecifiedScalarType, isSpecifiedDirective, } from 'graphql';
2
+ import { getBuiltInForStub, isNamedStub } from './stub.js';
3
+ export function rewireTypes(originalTypeMap, directives) {
4
+ const referenceTypeMap = Object.create(null);
5
+ for (const typeName in originalTypeMap) {
6
+ referenceTypeMap[typeName] = originalTypeMap[typeName];
7
+ }
8
+ const newTypeMap = Object.create(null);
9
+ for (const typeName in referenceTypeMap) {
10
+ const namedType = referenceTypeMap[typeName];
11
+ if (namedType == null || typeName.startsWith('__')) {
12
+ continue;
13
+ }
14
+ const newName = namedType.name;
15
+ if (newName.startsWith('__')) {
16
+ continue;
17
+ }
18
+ if (newTypeMap[newName] != null) {
19
+ throw new Error(`Duplicate schema type name ${newName}`);
20
+ }
21
+ newTypeMap[newName] = namedType;
22
+ }
23
+ for (const typeName in newTypeMap) {
24
+ newTypeMap[typeName] = rewireNamedType(newTypeMap[typeName]);
25
+ }
26
+ const newDirectives = directives.map(directive => rewireDirective(directive));
27
+ return {
28
+ typeMap: newTypeMap,
29
+ directives: newDirectives,
30
+ };
31
+ function rewireDirective(directive) {
32
+ if (isSpecifiedDirective(directive)) {
33
+ return directive;
34
+ }
35
+ const directiveConfig = directive.toConfig();
36
+ directiveConfig.args = rewireArgs(directiveConfig.args);
37
+ return new GraphQLDirective(directiveConfig);
38
+ }
39
+ function rewireArgs(args) {
40
+ const rewiredArgs = {};
41
+ for (const argName in args) {
42
+ const arg = args[argName];
43
+ const rewiredArgType = rewireType(arg.type);
44
+ if (rewiredArgType != null) {
45
+ arg.type = rewiredArgType;
46
+ rewiredArgs[argName] = arg;
47
+ }
48
+ }
49
+ return rewiredArgs;
50
+ }
51
+ function rewireNamedType(type) {
52
+ if (isObjectType(type)) {
53
+ const config = type.toConfig();
54
+ const newConfig = {
55
+ ...config,
56
+ fields: () => rewireFields(config.fields),
57
+ interfaces: () => rewireNamedTypes(config.interfaces),
58
+ };
59
+ return new GraphQLObjectType(newConfig);
60
+ }
61
+ else if (isInterfaceType(type)) {
62
+ const config = type.toConfig();
63
+ const newConfig = {
64
+ ...config,
65
+ fields: () => rewireFields(config.fields),
66
+ };
67
+ if ('interfaces' in newConfig) {
68
+ newConfig.interfaces = () => rewireNamedTypes(config.interfaces);
69
+ }
70
+ return new GraphQLInterfaceType(newConfig);
71
+ }
72
+ else if (isUnionType(type)) {
73
+ const config = type.toConfig();
74
+ const newConfig = {
75
+ ...config,
76
+ types: () => rewireNamedTypes(config.types),
77
+ };
78
+ return new GraphQLUnionType(newConfig);
79
+ }
80
+ else if (isInputObjectType(type)) {
81
+ const config = type.toConfig();
82
+ const newConfig = {
83
+ ...config,
84
+ fields: () => rewireInputFields(config.fields),
85
+ };
86
+ return new GraphQLInputObjectType(newConfig);
87
+ }
88
+ else if (isEnumType(type)) {
89
+ const enumConfig = type.toConfig();
90
+ return new GraphQLEnumType(enumConfig);
91
+ }
92
+ else if (isScalarType(type)) {
93
+ if (isSpecifiedScalarType(type)) {
94
+ return type;
95
+ }
96
+ const scalarConfig = type.toConfig();
97
+ return new GraphQLScalarType(scalarConfig);
98
+ }
99
+ throw new Error(`Unexpected schema type: ${type}`);
100
+ }
101
+ function rewireFields(fields) {
102
+ const rewiredFields = {};
103
+ for (const fieldName in fields) {
104
+ const field = fields[fieldName];
105
+ const rewiredFieldType = rewireType(field.type);
106
+ if (rewiredFieldType != null && field.args) {
107
+ field.type = rewiredFieldType;
108
+ field.args = rewireArgs(field.args);
109
+ rewiredFields[fieldName] = field;
110
+ }
111
+ }
112
+ return rewiredFields;
113
+ }
114
+ function rewireInputFields(fields) {
115
+ const rewiredFields = {};
116
+ for (const fieldName in fields) {
117
+ const field = fields[fieldName];
118
+ const rewiredFieldType = rewireType(field.type);
119
+ if (rewiredFieldType != null) {
120
+ field.type = rewiredFieldType;
121
+ rewiredFields[fieldName] = field;
122
+ }
123
+ }
124
+ return rewiredFields;
125
+ }
126
+ function rewireNamedTypes(namedTypes) {
127
+ const rewiredTypes = [];
128
+ for (const namedType of namedTypes) {
129
+ const rewiredType = rewireType(namedType);
130
+ if (rewiredType != null) {
131
+ rewiredTypes.push(rewiredType);
132
+ }
133
+ }
134
+ return rewiredTypes;
135
+ }
136
+ function rewireType(type) {
137
+ if (isListType(type)) {
138
+ const rewiredType = rewireType(type.ofType);
139
+ return rewiredType != null ? new GraphQLList(rewiredType) : null;
140
+ }
141
+ else if (isNonNullType(type)) {
142
+ const rewiredType = rewireType(type.ofType);
143
+ return rewiredType != null ? new GraphQLNonNull(rewiredType) : null;
144
+ }
145
+ else if (isNamedType(type)) {
146
+ let rewiredType = referenceTypeMap[type.name];
147
+ if (rewiredType === undefined) {
148
+ rewiredType = isNamedStub(type) ? getBuiltInForStub(type) : rewireNamedType(type);
149
+ newTypeMap[rewiredType.name] = referenceTypeMap[type.name] = rewiredType;
150
+ }
151
+ return rewiredType != null ? newTypeMap[rewiredType.name] : null;
152
+ }
153
+ return null;
154
+ }
155
+ }
@@ -0,0 +1,33 @@
1
+ import { memoize1 } from './memoize.js';
2
+ export function getDefinedRootType(schema, operation) {
3
+ const rootTypeMap = getRootTypeMap(schema);
4
+ const rootType = rootTypeMap.get(operation);
5
+ if (rootType == null) {
6
+ throw new Error(`Root type for operation "${operation}" not defined by the given schema.`);
7
+ }
8
+ return rootType;
9
+ }
10
+ export const getRootTypeNames = memoize1(function getRootTypeNames(schema) {
11
+ const rootTypes = getRootTypes(schema);
12
+ return new Set([...rootTypes].map(type => type.name));
13
+ });
14
+ export const getRootTypes = memoize1(function getRootTypes(schema) {
15
+ const rootTypeMap = getRootTypeMap(schema);
16
+ return new Set(rootTypeMap.values());
17
+ });
18
+ export const getRootTypeMap = memoize1(function getRootTypeMap(schema) {
19
+ const rootTypeMap = new Map();
20
+ const queryType = schema.getQueryType();
21
+ if (queryType) {
22
+ rootTypeMap.set('query', queryType);
23
+ }
24
+ const mutationType = schema.getMutationType();
25
+ if (mutationType) {
26
+ rootTypeMap.set('mutation', mutationType);
27
+ }
28
+ const subscriptionType = schema.getSubscriptionType();
29
+ if (subscriptionType) {
30
+ rootTypeMap.set('subscription', subscriptionType);
31
+ }
32
+ return rootTypeMap;
33
+ });
@@ -0,0 +1,5 @@
1
+ import { parse } from 'graphql';
2
+ export function parseSelectionSet(selectionSet, options) {
3
+ const query = parse(selectionSet, options).definitions[0];
4
+ return query.selectionSet;
5
+ }
package/esm/stub.js ADDED
@@ -0,0 +1,61 @@
1
+ import { GraphQLObjectType, GraphQLInterfaceType, GraphQLInputObjectType, GraphQLString, GraphQLInt, GraphQLFloat, GraphQLBoolean, GraphQLID, Kind, GraphQLList, GraphQLNonNull, } from 'graphql';
2
+ export function createNamedStub(name, type) {
3
+ let constructor;
4
+ if (type === 'object') {
5
+ constructor = GraphQLObjectType;
6
+ }
7
+ else if (type === 'interface') {
8
+ constructor = GraphQLInterfaceType;
9
+ }
10
+ else {
11
+ constructor = GraphQLInputObjectType;
12
+ }
13
+ return new constructor({
14
+ name,
15
+ fields: {
16
+ _fake: {
17
+ type: GraphQLString,
18
+ },
19
+ },
20
+ });
21
+ }
22
+ export function createStub(node, type) {
23
+ switch (node.kind) {
24
+ case Kind.LIST_TYPE:
25
+ return new GraphQLList(createStub(node.type, type));
26
+ case Kind.NON_NULL_TYPE:
27
+ return new GraphQLNonNull(createStub(node.type, type));
28
+ default:
29
+ if (type === 'output') {
30
+ return createNamedStub(node.name.value, 'object');
31
+ }
32
+ return createNamedStub(node.name.value, 'input');
33
+ }
34
+ }
35
+ export function isNamedStub(type) {
36
+ if ('getFields' in type) {
37
+ const fields = type.getFields();
38
+ // eslint-disable-next-line no-unreachable-loop
39
+ for (const fieldName in fields) {
40
+ const field = fields[fieldName];
41
+ return field.name === '_fake';
42
+ }
43
+ }
44
+ return false;
45
+ }
46
+ export function getBuiltInForStub(type) {
47
+ switch (type.name) {
48
+ case GraphQLInt.name:
49
+ return GraphQLInt;
50
+ case GraphQLFloat.name:
51
+ return GraphQLFloat;
52
+ case GraphQLString.name:
53
+ return GraphQLString;
54
+ case GraphQLBoolean.name:
55
+ return GraphQLBoolean;
56
+ case GraphQLID.name:
57
+ return GraphQLID;
58
+ default:
59
+ return type;
60
+ }
61
+ }
@@ -0,0 +1,48 @@
1
+ import { getNullableType, isLeafType, isListType, isInputObjectType } from 'graphql';
2
+ export function transformInputValue(type, value, inputLeafValueTransformer = null, inputObjectValueTransformer = null) {
3
+ if (value == null) {
4
+ return value;
5
+ }
6
+ const nullableType = getNullableType(type);
7
+ if (isLeafType(nullableType)) {
8
+ return inputLeafValueTransformer != null ? inputLeafValueTransformer(nullableType, value) : value;
9
+ }
10
+ else if (isListType(nullableType)) {
11
+ return value.map((listMember) => transformInputValue(nullableType.ofType, listMember, inputLeafValueTransformer, inputObjectValueTransformer));
12
+ }
13
+ else if (isInputObjectType(nullableType)) {
14
+ const fields = nullableType.getFields();
15
+ const newValue = {};
16
+ for (const key in value) {
17
+ const field = fields[key];
18
+ if (field != null) {
19
+ newValue[key] = transformInputValue(field.type, value[key], inputLeafValueTransformer, inputObjectValueTransformer);
20
+ }
21
+ }
22
+ return inputObjectValueTransformer != null ? inputObjectValueTransformer(nullableType, newValue) : newValue;
23
+ }
24
+ // unreachable, no other possible return value
25
+ }
26
+ export function serializeInputValue(type, value) {
27
+ return transformInputValue(type, value, (t, v) => {
28
+ try {
29
+ return t.serialize(v);
30
+ }
31
+ catch (_a) {
32
+ return v;
33
+ }
34
+ });
35
+ }
36
+ export function parseInputValue(type, value) {
37
+ return transformInputValue(type, value, (t, v) => {
38
+ try {
39
+ return t.parseValue(v);
40
+ }
41
+ catch (_a) {
42
+ return v;
43
+ }
44
+ });
45
+ }
46
+ export function parseInputValueLiteral(type, value) {
47
+ return transformInputValue(type, value, (t, v) => t.parseLiteral(v, {}));
48
+ }
package/esm/types.js ADDED
@@ -0,0 +1,24 @@
1
+ export var DirectiveLocation;
2
+ (function (DirectiveLocation) {
3
+ /** Request Definitions */
4
+ DirectiveLocation["QUERY"] = "QUERY";
5
+ DirectiveLocation["MUTATION"] = "MUTATION";
6
+ DirectiveLocation["SUBSCRIPTION"] = "SUBSCRIPTION";
7
+ DirectiveLocation["FIELD"] = "FIELD";
8
+ DirectiveLocation["FRAGMENT_DEFINITION"] = "FRAGMENT_DEFINITION";
9
+ DirectiveLocation["FRAGMENT_SPREAD"] = "FRAGMENT_SPREAD";
10
+ DirectiveLocation["INLINE_FRAGMENT"] = "INLINE_FRAGMENT";
11
+ DirectiveLocation["VARIABLE_DEFINITION"] = "VARIABLE_DEFINITION";
12
+ /** Type System Definitions */
13
+ DirectiveLocation["SCHEMA"] = "SCHEMA";
14
+ DirectiveLocation["SCALAR"] = "SCALAR";
15
+ DirectiveLocation["OBJECT"] = "OBJECT";
16
+ DirectiveLocation["FIELD_DEFINITION"] = "FIELD_DEFINITION";
17
+ DirectiveLocation["ARGUMENT_DEFINITION"] = "ARGUMENT_DEFINITION";
18
+ DirectiveLocation["INTERFACE"] = "INTERFACE";
19
+ DirectiveLocation["UNION"] = "UNION";
20
+ DirectiveLocation["ENUM"] = "ENUM";
21
+ DirectiveLocation["ENUM_VALUE"] = "ENUM_VALUE";
22
+ DirectiveLocation["INPUT_OBJECT"] = "INPUT_OBJECT";
23
+ DirectiveLocation["INPUT_FIELD_DEFINITION"] = "INPUT_FIELD_DEFINITION";
24
+ })(DirectiveLocation || (DirectiveLocation = {}));
@@ -0,0 +1,49 @@
1
+ import { Kind } from 'graphql';
2
+ import { astFromType } from './astFromType.js';
3
+ export function updateArgument(argumentNodes, variableDefinitionsMap, variableValues, argName, varName, type, value) {
4
+ argumentNodes[argName] = {
5
+ kind: Kind.ARGUMENT,
6
+ name: {
7
+ kind: Kind.NAME,
8
+ value: argName,
9
+ },
10
+ value: {
11
+ kind: Kind.VARIABLE,
12
+ name: {
13
+ kind: Kind.NAME,
14
+ value: varName,
15
+ },
16
+ },
17
+ };
18
+ variableDefinitionsMap[varName] = {
19
+ kind: Kind.VARIABLE_DEFINITION,
20
+ variable: {
21
+ kind: Kind.VARIABLE,
22
+ name: {
23
+ kind: Kind.NAME,
24
+ value: varName,
25
+ },
26
+ },
27
+ type: astFromType(type),
28
+ };
29
+ if (value !== undefined) {
30
+ variableValues[varName] = value;
31
+ return;
32
+ }
33
+ // including the variable in the map with value of `undefined`
34
+ // will actually be translated by graphql-js into `null`
35
+ // see https://github.com/graphql/graphql-js/issues/2533
36
+ if (varName in variableValues) {
37
+ delete variableValues[varName];
38
+ }
39
+ }
40
+ export function createVariableNameGenerator(variableDefinitionMap) {
41
+ let varCounter = 0;
42
+ return (argName) => {
43
+ let varName;
44
+ do {
45
+ varName = `_v${(varCounter++).toString()}_${argName}`;
46
+ } while (varName in variableDefinitionMap);
47
+ return varName;
48
+ };
49
+ }
@@ -0,0 +1,70 @@
1
+ import { Kind, validate, specifiedRules, concatAST, versionInfo, } from 'graphql';
2
+ import { AggregateError } from './AggregateError.js';
3
+ export async function validateGraphQlDocuments(schema, documentFiles, effectiveRules = createDefaultRules()) {
4
+ const allFragmentMap = new Map();
5
+ const documentFileObjectsToValidate = [];
6
+ for (const documentFile of documentFiles) {
7
+ if (documentFile.document) {
8
+ const definitionsToValidate = [];
9
+ for (const definitionNode of documentFile.document.definitions) {
10
+ if (definitionNode.kind === Kind.FRAGMENT_DEFINITION) {
11
+ allFragmentMap.set(definitionNode.name.value, definitionNode);
12
+ }
13
+ else {
14
+ definitionsToValidate.push(definitionNode);
15
+ }
16
+ }
17
+ documentFileObjectsToValidate.push({
18
+ location: documentFile.location,
19
+ document: {
20
+ kind: Kind.DOCUMENT,
21
+ definitions: definitionsToValidate,
22
+ },
23
+ });
24
+ }
25
+ }
26
+ const allErrors = [];
27
+ const allFragmentsDocument = {
28
+ kind: Kind.DOCUMENT,
29
+ definitions: [...allFragmentMap.values()],
30
+ };
31
+ await Promise.all(documentFileObjectsToValidate.map(async (documentFile) => {
32
+ const documentToValidate = concatAST([allFragmentsDocument, documentFile.document]);
33
+ const errors = validate(schema, documentToValidate, effectiveRules);
34
+ if (errors.length > 0) {
35
+ allErrors.push({
36
+ filePath: documentFile.location,
37
+ errors,
38
+ });
39
+ }
40
+ }));
41
+ return allErrors;
42
+ }
43
+ export function checkValidationErrors(loadDocumentErrors) {
44
+ if (loadDocumentErrors.length > 0) {
45
+ const errors = [];
46
+ for (const loadDocumentError of loadDocumentErrors) {
47
+ for (const graphQLError of loadDocumentError.errors) {
48
+ const error = new Error();
49
+ error.name = 'GraphQLDocumentError';
50
+ error.message = `${error.name}: ${graphQLError.message}`;
51
+ error.stack = error.message;
52
+ if (graphQLError.locations) {
53
+ for (const location of graphQLError.locations) {
54
+ error.stack += `\n at ${loadDocumentError.filePath}:${location.line}:${location.column}`;
55
+ }
56
+ }
57
+ errors.push(error);
58
+ }
59
+ }
60
+ throw new AggregateError(errors, `GraphQL Document Validation failed with ${errors.length} errors;
61
+ ${errors.map((error, index) => `Error ${index}: ${error.stack}`).join('\n\n')}`);
62
+ }
63
+ }
64
+ export function createDefaultRules() {
65
+ let ignored = ['NoUnusedFragmentsRule', 'NoUnusedVariablesRule', 'KnownDirectivesRule'];
66
+ if (versionInfo.major < 15) {
67
+ ignored = ignored.map(rule => rule.replace(/Rule$/, ''));
68
+ }
69
+ return specifiedRules.filter((f) => !ignored.includes(f.name));
70
+ }
@@ -0,0 +1,17 @@
1
+ export function valueMatchesCriteria(value, criteria) {
2
+ if (value == null) {
3
+ return value === criteria;
4
+ }
5
+ else if (Array.isArray(value)) {
6
+ return Array.isArray(criteria) && value.every((val, index) => valueMatchesCriteria(val, criteria[index]));
7
+ }
8
+ else if (typeof value === 'object') {
9
+ return (typeof criteria === 'object' &&
10
+ criteria &&
11
+ Object.keys(criteria).every(propertyName => valueMatchesCriteria(value[propertyName], criteria[propertyName])));
12
+ }
13
+ else if (criteria instanceof RegExp) {
14
+ return criteria.test(value);
15
+ }
16
+ return value === criteria;
17
+ }