@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,219 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.visitResult = exports.visitErrors = exports.visitData = void 0;
4
+ const getOperationASTFromRequest_js_1 = require("./getOperationASTFromRequest.js");
5
+ const graphql_1 = require("graphql");
6
+ const collectFields_js_1 = require("./collectFields.js");
7
+ function visitData(data, enter, leave) {
8
+ if (Array.isArray(data)) {
9
+ return data.map(value => visitData(value, enter, leave));
10
+ }
11
+ else if (typeof data === 'object') {
12
+ const newData = enter != null ? enter(data) : data;
13
+ if (newData != null) {
14
+ for (const key in newData) {
15
+ const value = newData[key];
16
+ Object.defineProperty(newData, key, {
17
+ value: visitData(value, enter, leave),
18
+ });
19
+ }
20
+ }
21
+ return leave != null ? leave(newData) : newData;
22
+ }
23
+ return data;
24
+ }
25
+ exports.visitData = visitData;
26
+ function visitErrors(errors, visitor) {
27
+ return errors.map(error => visitor(error));
28
+ }
29
+ exports.visitErrors = visitErrors;
30
+ function visitResult(result, request, schema, resultVisitorMap, errorVisitorMap) {
31
+ const fragments = request.document.definitions.reduce((acc, def) => {
32
+ if (def.kind === graphql_1.Kind.FRAGMENT_DEFINITION) {
33
+ acc[def.name.value] = def;
34
+ }
35
+ return acc;
36
+ }, {});
37
+ const variableValues = request.variables || {};
38
+ const errorInfo = {
39
+ segmentInfoMap: new Map(),
40
+ unpathedErrors: new Set(),
41
+ };
42
+ const data = result.data;
43
+ const errors = result.errors;
44
+ const visitingErrors = errors != null && errorVisitorMap != null;
45
+ const operationDocumentNode = (0, getOperationASTFromRequest_js_1.getOperationASTFromRequest)(request);
46
+ if (data != null && operationDocumentNode != null) {
47
+ result.data = visitRoot(data, operationDocumentNode, schema, fragments, variableValues, resultVisitorMap, visitingErrors ? errors : undefined, errorInfo);
48
+ }
49
+ if (errors != null && errorVisitorMap) {
50
+ result.errors = visitErrorsByType(errors, errorVisitorMap, errorInfo);
51
+ }
52
+ return result;
53
+ }
54
+ exports.visitResult = visitResult;
55
+ function visitErrorsByType(errors, errorVisitorMap, errorInfo) {
56
+ const segmentInfoMap = errorInfo.segmentInfoMap;
57
+ const unpathedErrors = errorInfo.unpathedErrors;
58
+ const unpathedErrorVisitor = errorVisitorMap['__unpathed'];
59
+ return errors.map(originalError => {
60
+ const pathSegmentsInfo = segmentInfoMap.get(originalError);
61
+ const newError = pathSegmentsInfo == null
62
+ ? originalError
63
+ : pathSegmentsInfo.reduceRight((acc, segmentInfo) => {
64
+ const typeName = segmentInfo.type.name;
65
+ const typeVisitorMap = errorVisitorMap[typeName];
66
+ if (typeVisitorMap == null) {
67
+ return acc;
68
+ }
69
+ const errorVisitor = typeVisitorMap[segmentInfo.fieldName];
70
+ return errorVisitor == null ? acc : errorVisitor(acc, segmentInfo.pathIndex);
71
+ }, originalError);
72
+ if (unpathedErrorVisitor && unpathedErrors.has(originalError)) {
73
+ return unpathedErrorVisitor(newError);
74
+ }
75
+ return newError;
76
+ });
77
+ }
78
+ function getOperationRootType(schema, operationDef) {
79
+ switch (operationDef.operation) {
80
+ case 'query':
81
+ return schema.getQueryType();
82
+ case 'mutation':
83
+ return schema.getMutationType();
84
+ case 'subscription':
85
+ return schema.getSubscriptionType();
86
+ }
87
+ }
88
+ function visitRoot(root, operation, schema, fragments, variableValues, resultVisitorMap, errors, errorInfo) {
89
+ const operationRootType = getOperationRootType(schema, operation);
90
+ const collectedFields = (0, collectFields_js_1.collectFields)(schema, fragments, variableValues, operationRootType, operation.selectionSet, new Map(), new Set());
91
+ return visitObjectValue(root, operationRootType, collectedFields, schema, fragments, variableValues, resultVisitorMap, 0, errors, errorInfo);
92
+ }
93
+ function visitObjectValue(object, type, fieldNodeMap, schema, fragments, variableValues, resultVisitorMap, pathIndex, errors, errorInfo) {
94
+ var _a;
95
+ const fieldMap = type.getFields();
96
+ const typeVisitorMap = resultVisitorMap === null || resultVisitorMap === void 0 ? void 0 : resultVisitorMap[type.name];
97
+ const enterObject = typeVisitorMap === null || typeVisitorMap === void 0 ? void 0 : typeVisitorMap.__enter;
98
+ const newObject = enterObject != null ? enterObject(object) : object;
99
+ let sortedErrors;
100
+ let errorMap = null;
101
+ if (errors != null) {
102
+ sortedErrors = sortErrorsByPathSegment(errors, pathIndex);
103
+ errorMap = sortedErrors.errorMap;
104
+ for (const error of sortedErrors.unpathedErrors) {
105
+ errorInfo.unpathedErrors.add(error);
106
+ }
107
+ }
108
+ for (const [responseKey, subFieldNodes] of fieldNodeMap) {
109
+ const fieldName = subFieldNodes[0].name.value;
110
+ const fieldType = fieldName === '__typename' ? graphql_1.TypeNameMetaFieldDef.type : (_a = fieldMap[fieldName]) === null || _a === void 0 ? void 0 : _a.type;
111
+ const newPathIndex = pathIndex + 1;
112
+ let fieldErrors;
113
+ if (errorMap) {
114
+ fieldErrors = errorMap[responseKey];
115
+ if (fieldErrors != null) {
116
+ delete errorMap[responseKey];
117
+ }
118
+ addPathSegmentInfo(type, fieldName, newPathIndex, fieldErrors, errorInfo);
119
+ }
120
+ const newValue = visitFieldValue(object[responseKey], fieldType, subFieldNodes, schema, fragments, variableValues, resultVisitorMap, newPathIndex, fieldErrors, errorInfo);
121
+ updateObject(newObject, responseKey, newValue, typeVisitorMap, fieldName);
122
+ }
123
+ const oldTypename = newObject.__typename;
124
+ if (oldTypename != null) {
125
+ updateObject(newObject, '__typename', oldTypename, typeVisitorMap, '__typename');
126
+ }
127
+ if (errorMap) {
128
+ for (const errorsKey in errorMap) {
129
+ const errors = errorMap[errorsKey];
130
+ for (const error of errors) {
131
+ errorInfo.unpathedErrors.add(error);
132
+ }
133
+ }
134
+ }
135
+ const leaveObject = typeVisitorMap === null || typeVisitorMap === void 0 ? void 0 : typeVisitorMap.__leave;
136
+ return leaveObject != null ? leaveObject(newObject) : newObject;
137
+ }
138
+ function updateObject(object, responseKey, newValue, typeVisitorMap, fieldName) {
139
+ if (typeVisitorMap == null) {
140
+ object[responseKey] = newValue;
141
+ return;
142
+ }
143
+ const fieldVisitor = typeVisitorMap[fieldName];
144
+ if (fieldVisitor == null) {
145
+ object[responseKey] = newValue;
146
+ return;
147
+ }
148
+ const visitedValue = fieldVisitor(newValue);
149
+ if (visitedValue === undefined) {
150
+ delete object[responseKey];
151
+ return;
152
+ }
153
+ object[responseKey] = visitedValue;
154
+ }
155
+ function visitListValue(list, returnType, fieldNodes, schema, fragments, variableValues, resultVisitorMap, pathIndex, errors, errorInfo) {
156
+ return list.map(listMember => visitFieldValue(listMember, returnType, fieldNodes, schema, fragments, variableValues, resultVisitorMap, pathIndex + 1, errors, errorInfo));
157
+ }
158
+ function visitFieldValue(value, returnType, fieldNodes, schema, fragments, variableValues, resultVisitorMap, pathIndex, errors = [], errorInfo) {
159
+ if (value == null) {
160
+ return value;
161
+ }
162
+ const nullableType = (0, graphql_1.getNullableType)(returnType);
163
+ if ((0, graphql_1.isListType)(nullableType)) {
164
+ return visitListValue(value, nullableType.ofType, fieldNodes, schema, fragments, variableValues, resultVisitorMap, pathIndex, errors, errorInfo);
165
+ }
166
+ else if ((0, graphql_1.isAbstractType)(nullableType)) {
167
+ const finalType = schema.getType(value.__typename);
168
+ const collectedFields = (0, collectFields_js_1.collectSubFields)(schema, fragments, variableValues, finalType, fieldNodes);
169
+ return visitObjectValue(value, finalType, collectedFields, schema, fragments, variableValues, resultVisitorMap, pathIndex, errors, errorInfo);
170
+ }
171
+ else if ((0, graphql_1.isObjectType)(nullableType)) {
172
+ const collectedFields = (0, collectFields_js_1.collectSubFields)(schema, fragments, variableValues, nullableType, fieldNodes);
173
+ return visitObjectValue(value, nullableType, collectedFields, schema, fragments, variableValues, resultVisitorMap, pathIndex, errors, errorInfo);
174
+ }
175
+ const typeVisitorMap = resultVisitorMap === null || resultVisitorMap === void 0 ? void 0 : resultVisitorMap[nullableType.name];
176
+ if (typeVisitorMap == null) {
177
+ return value;
178
+ }
179
+ const visitedValue = typeVisitorMap(value);
180
+ return visitedValue === undefined ? value : visitedValue;
181
+ }
182
+ function sortErrorsByPathSegment(errors, pathIndex) {
183
+ var _a;
184
+ const errorMap = Object.create(null);
185
+ const unpathedErrors = new Set();
186
+ for (const error of errors) {
187
+ const pathSegment = (_a = error.path) === null || _a === void 0 ? void 0 : _a[pathIndex];
188
+ if (pathSegment == null) {
189
+ unpathedErrors.add(error);
190
+ continue;
191
+ }
192
+ if (pathSegment in errorMap) {
193
+ errorMap[pathSegment].push(error);
194
+ }
195
+ else {
196
+ errorMap[pathSegment] = [error];
197
+ }
198
+ }
199
+ return {
200
+ errorMap,
201
+ unpathedErrors,
202
+ };
203
+ }
204
+ function addPathSegmentInfo(type, fieldName, pathIndex, errors = [], errorInfo) {
205
+ for (const error of errors) {
206
+ const segmentInfo = {
207
+ type,
208
+ fieldName,
209
+ pathIndex,
210
+ };
211
+ const pathSegmentsInfo = errorInfo.segmentInfoMap.get(error);
212
+ if (pathSegmentsInfo == null) {
213
+ errorInfo.segmentInfoMap.set(error, [segmentInfo]);
214
+ }
215
+ else {
216
+ pathSegmentsInfo.push(segmentInfo);
217
+ }
218
+ }
219
+ }
@@ -0,0 +1,56 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.withCancel = exports.getAsyncIterableWithCancel = exports.getAsyncIteratorWithCancel = void 0;
4
+ const memoize_js_1 = require("./memoize.js");
5
+ async function defaultAsyncIteratorReturn(value) {
6
+ return { value, done: true };
7
+ }
8
+ const proxyMethodFactory = (0, memoize_js_1.memoize2)(function proxyMethodFactory(target, targetMethod) {
9
+ return function proxyMethod(...args) {
10
+ return Reflect.apply(targetMethod, target, args);
11
+ };
12
+ });
13
+ function getAsyncIteratorWithCancel(asyncIterator, onCancel) {
14
+ return new Proxy(asyncIterator, {
15
+ has(asyncIterator, prop) {
16
+ if (prop === 'return') {
17
+ return true;
18
+ }
19
+ return Reflect.has(asyncIterator, prop);
20
+ },
21
+ get(asyncIterator, prop, receiver) {
22
+ const existingPropValue = Reflect.get(asyncIterator, prop, receiver);
23
+ if (prop === 'return') {
24
+ const existingReturn = existingPropValue || defaultAsyncIteratorReturn;
25
+ return async function returnWithCancel(value) {
26
+ const returnValue = await onCancel(value);
27
+ return Reflect.apply(existingReturn, asyncIterator, [returnValue]);
28
+ };
29
+ }
30
+ else if (typeof existingPropValue === 'function') {
31
+ return proxyMethodFactory(asyncIterator, existingPropValue);
32
+ }
33
+ return existingPropValue;
34
+ },
35
+ });
36
+ }
37
+ exports.getAsyncIteratorWithCancel = getAsyncIteratorWithCancel;
38
+ function getAsyncIterableWithCancel(asyncIterable, onCancel) {
39
+ return new Proxy(asyncIterable, {
40
+ get(asyncIterable, prop, receiver) {
41
+ const existingPropValue = Reflect.get(asyncIterable, prop, receiver);
42
+ if (Symbol.asyncIterator === prop) {
43
+ return function asyncIteratorFactory() {
44
+ const asyncIterator = Reflect.apply(existingPropValue, asyncIterable, []);
45
+ return getAsyncIteratorWithCancel(asyncIterator, onCancel);
46
+ };
47
+ }
48
+ else if (typeof existingPropValue === 'function') {
49
+ return proxyMethodFactory(asyncIterable, existingPropValue);
50
+ }
51
+ return existingPropValue;
52
+ },
53
+ });
54
+ }
55
+ exports.getAsyncIterableWithCancel = getAsyncIterableWithCancel;
56
+ exports.withCancel = getAsyncIterableWithCancel;
@@ -0,0 +1,21 @@
1
+ let AggregateErrorImpl;
2
+ if (typeof AggregateError === 'undefined') {
3
+ class AggregateErrorClass extends Error {
4
+ constructor(errors, message = '') {
5
+ super(message);
6
+ this.errors = errors;
7
+ this.name = 'AggregateError';
8
+ Error.captureStackTrace(this, AggregateErrorClass);
9
+ }
10
+ }
11
+ AggregateErrorImpl = function (errors, message) {
12
+ return new AggregateErrorClass(errors, message);
13
+ };
14
+ }
15
+ else {
16
+ AggregateErrorImpl = AggregateError;
17
+ }
18
+ export { AggregateErrorImpl as AggregateError };
19
+ export function isAggregateError(error) {
20
+ return 'errors' in error && Array.isArray(error['errors']);
21
+ }
@@ -0,0 +1,28 @@
1
+ export var MapperKind;
2
+ (function (MapperKind) {
3
+ MapperKind["TYPE"] = "MapperKind.TYPE";
4
+ MapperKind["SCALAR_TYPE"] = "MapperKind.SCALAR_TYPE";
5
+ MapperKind["ENUM_TYPE"] = "MapperKind.ENUM_TYPE";
6
+ MapperKind["COMPOSITE_TYPE"] = "MapperKind.COMPOSITE_TYPE";
7
+ MapperKind["OBJECT_TYPE"] = "MapperKind.OBJECT_TYPE";
8
+ MapperKind["INPUT_OBJECT_TYPE"] = "MapperKind.INPUT_OBJECT_TYPE";
9
+ MapperKind["ABSTRACT_TYPE"] = "MapperKind.ABSTRACT_TYPE";
10
+ MapperKind["UNION_TYPE"] = "MapperKind.UNION_TYPE";
11
+ MapperKind["INTERFACE_TYPE"] = "MapperKind.INTERFACE_TYPE";
12
+ MapperKind["ROOT_OBJECT"] = "MapperKind.ROOT_OBJECT";
13
+ MapperKind["QUERY"] = "MapperKind.QUERY";
14
+ MapperKind["MUTATION"] = "MapperKind.MUTATION";
15
+ MapperKind["SUBSCRIPTION"] = "MapperKind.SUBSCRIPTION";
16
+ MapperKind["DIRECTIVE"] = "MapperKind.DIRECTIVE";
17
+ MapperKind["FIELD"] = "MapperKind.FIELD";
18
+ MapperKind["COMPOSITE_FIELD"] = "MapperKind.COMPOSITE_FIELD";
19
+ MapperKind["OBJECT_FIELD"] = "MapperKind.OBJECT_FIELD";
20
+ MapperKind["ROOT_FIELD"] = "MapperKind.ROOT_FIELD";
21
+ MapperKind["QUERY_ROOT_FIELD"] = "MapperKind.QUERY_ROOT_FIELD";
22
+ MapperKind["MUTATION_ROOT_FIELD"] = "MapperKind.MUTATION_ROOT_FIELD";
23
+ MapperKind["SUBSCRIPTION_ROOT_FIELD"] = "MapperKind.SUBSCRIPTION_ROOT_FIELD";
24
+ MapperKind["INTERFACE_FIELD"] = "MapperKind.INTERFACE_FIELD";
25
+ MapperKind["INPUT_OBJECT_FIELD"] = "MapperKind.INPUT_OBJECT_FIELD";
26
+ MapperKind["ARGUMENT"] = "MapperKind.ARGUMENT";
27
+ MapperKind["ENUM_VALUE"] = "MapperKind.ENUM_VALUE";
28
+ })(MapperKind || (MapperKind = {}));
@@ -0,0 +1,58 @@
1
+ // addTypes uses toConfig to create a new schema with a new or replaced
2
+ // type or directive. Rewiring is employed so that the replaced type can be
3
+ // reconnected with the existing types.
4
+ //
5
+ // Rewiring is employed even for new types or directives as a convenience, so
6
+ // that type references within the new type or directive do not have to be to
7
+ // the identical objects within the original schema.
8
+ //
9
+ // In fact, the type references could even be stub types with entirely different
10
+ // fields, as long as the type references share the same name as the desired
11
+ // type within the original schema's type map.
12
+ //
13
+ // This makes it easy to perform simple schema operations (e.g. adding a new
14
+ // type with a fiew fields removed from an existing type) that could normally be
15
+ // performed by using toConfig directly, but is blocked if any intervening
16
+ // more advanced schema operations have caused the types to be recreated via
17
+ // rewiring.
18
+ //
19
+ // Type recreation happens, for example, with every use of mapSchema, as the
20
+ // types are always rewired. If fields are selected and removed using
21
+ // mapSchema, adding those fields to a new type can no longer be simply done
22
+ // by toConfig, as the types are not the identical JavaScript objects, and
23
+ // schema creation will fail with errors referencing multiple types with the
24
+ // same names.
25
+ //
26
+ // enhanceSchema can fill this gap by adding an additional round of rewiring.
27
+ //
28
+ import { GraphQLSchema, isNamedType, isDirective } from 'graphql';
29
+ import { getObjectTypeFromTypeMap } from './getObjectTypeFromTypeMap.js';
30
+ import { rewireTypes } from './rewire.js';
31
+ export function addTypes(schema, newTypesOrDirectives) {
32
+ const config = schema.toConfig();
33
+ const originalTypeMap = {};
34
+ for (const type of config.types) {
35
+ originalTypeMap[type.name] = type;
36
+ }
37
+ const originalDirectiveMap = {};
38
+ for (const directive of config.directives) {
39
+ originalDirectiveMap[directive.name] = directive;
40
+ }
41
+ for (const newTypeOrDirective of newTypesOrDirectives) {
42
+ if (isNamedType(newTypeOrDirective)) {
43
+ originalTypeMap[newTypeOrDirective.name] = newTypeOrDirective;
44
+ }
45
+ else if (isDirective(newTypeOrDirective)) {
46
+ originalDirectiveMap[newTypeOrDirective.name] = newTypeOrDirective;
47
+ }
48
+ }
49
+ const { typeMap, directives } = rewireTypes(originalTypeMap, Object.values(originalDirectiveMap));
50
+ return new GraphQLSchema({
51
+ ...config,
52
+ query: getObjectTypeFromTypeMap(typeMap, schema.getQueryType()),
53
+ mutation: getObjectTypeFromTypeMap(typeMap, schema.getMutationType()),
54
+ subscription: getObjectTypeFromTypeMap(typeMap, schema.getSubscriptionType()),
55
+ types: Object.values(typeMap),
56
+ directives,
57
+ });
58
+ }
@@ -0,0 +1,27 @@
1
+ import { isNonNullType, Kind, isListType } from 'graphql';
2
+ import { inspect } from './inspect.js';
3
+ export function astFromType(type) {
4
+ if (isNonNullType(type)) {
5
+ const innerType = astFromType(type.ofType);
6
+ if (innerType.kind === Kind.NON_NULL_TYPE) {
7
+ throw new Error(`Invalid type node ${inspect(type)}. Inner type of non-null type cannot be a non-null type.`);
8
+ }
9
+ return {
10
+ kind: Kind.NON_NULL_TYPE,
11
+ type: innerType,
12
+ };
13
+ }
14
+ else if (isListType(type)) {
15
+ return {
16
+ kind: Kind.LIST_TYPE,
17
+ type: astFromType(type.ofType),
18
+ };
19
+ }
20
+ return {
21
+ kind: Kind.NAMED_TYPE,
22
+ name: {
23
+ kind: Kind.NAME,
24
+ value: type.name,
25
+ },
26
+ };
27
+ }
@@ -0,0 +1,74 @@
1
+ import { Kind } from 'graphql';
2
+ /**
3
+ * Produces a GraphQL Value AST given a JavaScript object.
4
+ * Function will match JavaScript/JSON values to GraphQL AST schema format
5
+ * by using the following mapping.
6
+ *
7
+ * | JSON Value | GraphQL Value |
8
+ * | ------------- | -------------------- |
9
+ * | Object | Input Object |
10
+ * | Array | List |
11
+ * | Boolean | Boolean |
12
+ * | String | String |
13
+ * | Number | Int / Float |
14
+ * | null | NullValue |
15
+ *
16
+ */
17
+ export function astFromValueUntyped(value) {
18
+ // only explicit null, not undefined, NaN
19
+ if (value === null) {
20
+ return { kind: Kind.NULL };
21
+ }
22
+ // undefined
23
+ if (value === undefined) {
24
+ return null;
25
+ }
26
+ // Convert JavaScript array to GraphQL list. If the GraphQLType is a list, but
27
+ // the value is not an array, convert the value using the list's item type.
28
+ if (Array.isArray(value)) {
29
+ const valuesNodes = [];
30
+ for (const item of value) {
31
+ const itemNode = astFromValueUntyped(item);
32
+ if (itemNode != null) {
33
+ valuesNodes.push(itemNode);
34
+ }
35
+ }
36
+ return { kind: Kind.LIST, values: valuesNodes };
37
+ }
38
+ if (typeof value === 'object') {
39
+ const fieldNodes = [];
40
+ for (const fieldName in value) {
41
+ const fieldValue = value[fieldName];
42
+ const ast = astFromValueUntyped(fieldValue);
43
+ if (ast) {
44
+ fieldNodes.push({
45
+ kind: Kind.OBJECT_FIELD,
46
+ name: { kind: Kind.NAME, value: fieldName },
47
+ value: ast,
48
+ });
49
+ }
50
+ }
51
+ return { kind: Kind.OBJECT, fields: fieldNodes };
52
+ }
53
+ // Others serialize based on their corresponding JavaScript scalar types.
54
+ if (typeof value === 'boolean') {
55
+ return { kind: Kind.BOOLEAN, value };
56
+ }
57
+ // JavaScript numbers can be Int or Float values.
58
+ if (typeof value === 'number' && isFinite(value)) {
59
+ const stringNum = String(value);
60
+ return integerStringRegExp.test(stringNum)
61
+ ? { kind: Kind.INT, value: stringNum }
62
+ : { kind: Kind.FLOAT, value: stringNum };
63
+ }
64
+ if (typeof value === 'string') {
65
+ return { kind: Kind.STRING, value };
66
+ }
67
+ throw new TypeError(`Cannot convert value to AST: ${value}.`);
68
+ }
69
+ /**
70
+ * IntValue:
71
+ * - NegativeSign? 0
72
+ * - NegativeSign? NonZeroDigit ( Digit+ )?
73
+ */
74
+ const integerStringRegExp = /^-?(?:0|[1-9][0-9]*)$/;