@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,367 @@
1
+ import { visit, TokenKind, } from 'graphql';
2
+ const MAX_LINE_LENGTH = 80;
3
+ let commentsRegistry = {};
4
+ export function resetComments() {
5
+ commentsRegistry = {};
6
+ }
7
+ export function collectComment(node) {
8
+ var _a;
9
+ const entityName = (_a = node.name) === null || _a === void 0 ? void 0 : _a.value;
10
+ if (entityName == null) {
11
+ return;
12
+ }
13
+ pushComment(node, entityName);
14
+ switch (node.kind) {
15
+ case 'EnumTypeDefinition':
16
+ if (node.values) {
17
+ for (const value of node.values) {
18
+ pushComment(value, entityName, value.name.value);
19
+ }
20
+ }
21
+ break;
22
+ case 'ObjectTypeDefinition':
23
+ case 'InputObjectTypeDefinition':
24
+ case 'InterfaceTypeDefinition':
25
+ if (node.fields) {
26
+ for (const field of node.fields) {
27
+ pushComment(field, entityName, field.name.value);
28
+ if (isFieldDefinitionNode(field) && field.arguments) {
29
+ for (const arg of field.arguments) {
30
+ pushComment(arg, entityName, field.name.value, arg.name.value);
31
+ }
32
+ }
33
+ }
34
+ }
35
+ break;
36
+ }
37
+ }
38
+ export function pushComment(node, entity, field, argument) {
39
+ const comment = getComment(node);
40
+ if (typeof comment !== 'string' || comment.length === 0) {
41
+ return;
42
+ }
43
+ const keys = [entity];
44
+ if (field) {
45
+ keys.push(field);
46
+ if (argument) {
47
+ keys.push(argument);
48
+ }
49
+ }
50
+ const path = keys.join('.');
51
+ if (!commentsRegistry[path]) {
52
+ commentsRegistry[path] = [];
53
+ }
54
+ commentsRegistry[path].push(comment);
55
+ }
56
+ export function printComment(comment) {
57
+ return '\n# ' + comment.replace(/\n/g, '\n# ');
58
+ }
59
+ /**
60
+ * Copyright (c) 2015-present, Facebook, Inc.
61
+ *
62
+ * This source code is licensed under the MIT license found in the
63
+ * LICENSE file in the root directory of this source tree.
64
+ */
65
+ /**
66
+ * NOTE: ==> This file has been modified just to add comments to the printed AST
67
+ * This is a temp measure, we will move to using the original non modified printer.js ASAP.
68
+ */
69
+ /**
70
+ * Given maybeArray, print an empty string if it is null or empty, otherwise
71
+ * print all items together separated by separator if provided
72
+ */
73
+ function join(maybeArray, separator) {
74
+ return maybeArray ? maybeArray.filter(x => x).join(separator || '') : '';
75
+ }
76
+ function hasMultilineItems(maybeArray) {
77
+ var _a;
78
+ return (_a = maybeArray === null || maybeArray === void 0 ? void 0 : maybeArray.some(str => str.includes('\n'))) !== null && _a !== void 0 ? _a : false;
79
+ }
80
+ function addDescription(cb) {
81
+ return (node, _key, _parent, path, ancestors) => {
82
+ var _a;
83
+ const keys = [];
84
+ const parent = path.reduce((prev, key) => {
85
+ if (['fields', 'arguments', 'values'].includes(key) && prev.name) {
86
+ keys.push(prev.name.value);
87
+ }
88
+ return prev[key];
89
+ }, ancestors[0]);
90
+ const key = [...keys, (_a = parent === null || parent === void 0 ? void 0 : parent.name) === null || _a === void 0 ? void 0 : _a.value].filter(Boolean).join('.');
91
+ const items = [];
92
+ if (node.kind.includes('Definition') && commentsRegistry[key]) {
93
+ items.push(...commentsRegistry[key]);
94
+ }
95
+ return join([...items.map(printComment), node.description, cb(node, _key, _parent, path, ancestors)], '\n');
96
+ };
97
+ }
98
+ function indent(maybeString) {
99
+ return maybeString && ` ${maybeString.replace(/\n/g, '\n ')}`;
100
+ }
101
+ /**
102
+ * Given array, print each item on its own line, wrapped in an
103
+ * indented "{ }" block.
104
+ */
105
+ function block(array) {
106
+ return array && array.length !== 0 ? `{\n${indent(join(array, '\n'))}\n}` : '';
107
+ }
108
+ /**
109
+ * If maybeString is not null or empty, then wrap with start and end, otherwise
110
+ * print an empty string.
111
+ */
112
+ function wrap(start, maybeString, end) {
113
+ return maybeString ? start + maybeString + (end || '') : '';
114
+ }
115
+ /**
116
+ * Print a block string in the indented block form by adding a leading and
117
+ * trailing blank line. However, if a block string starts with whitespace and is
118
+ * a single-line, adding a leading blank line would strip that whitespace.
119
+ */
120
+ function printBlockString(value, isDescription = false) {
121
+ const escaped = value.replace(/"""/g, '\\"""');
122
+ return (value[0] === ' ' || value[0] === '\t') && value.indexOf('\n') === -1
123
+ ? `"""${escaped.replace(/"$/, '"\n')}"""`
124
+ : `"""\n${isDescription ? escaped : indent(escaped)}\n"""`;
125
+ }
126
+ const printDocASTReducer = {
127
+ Name: { leave: node => node.value },
128
+ Variable: { leave: node => '$' + node.name },
129
+ // Document
130
+ Document: {
131
+ leave: node => join(node.definitions, '\n\n'),
132
+ },
133
+ OperationDefinition: {
134
+ leave: node => {
135
+ const varDefs = wrap('(', join(node.variableDefinitions, ', '), ')');
136
+ const prefix = join([node.operation, join([node.name, varDefs]), join(node.directives, ' ')], ' ');
137
+ // the query short form.
138
+ return prefix + ' ' + node.selectionSet;
139
+ },
140
+ },
141
+ VariableDefinition: {
142
+ leave: ({ variable, type, defaultValue, directives }) => variable + ': ' + type + wrap(' = ', defaultValue) + wrap(' ', join(directives, ' ')),
143
+ },
144
+ SelectionSet: { leave: ({ selections }) => block(selections) },
145
+ Field: {
146
+ leave({ alias, name, arguments: args, directives, selectionSet }) {
147
+ const prefix = wrap('', alias, ': ') + name;
148
+ let argsLine = prefix + wrap('(', join(args, ', '), ')');
149
+ if (argsLine.length > MAX_LINE_LENGTH) {
150
+ argsLine = prefix + wrap('(\n', indent(join(args, '\n')), '\n)');
151
+ }
152
+ return join([argsLine, join(directives, ' '), selectionSet], ' ');
153
+ },
154
+ },
155
+ Argument: { leave: ({ name, value }) => name + ': ' + value },
156
+ // Fragments
157
+ FragmentSpread: {
158
+ leave: ({ name, directives }) => '...' + name + wrap(' ', join(directives, ' ')),
159
+ },
160
+ InlineFragment: {
161
+ leave: ({ typeCondition, directives, selectionSet }) => join(['...', wrap('on ', typeCondition), join(directives, ' '), selectionSet], ' '),
162
+ },
163
+ FragmentDefinition: {
164
+ leave: ({ name, typeCondition, variableDefinitions, directives, selectionSet }) =>
165
+ // Note: fragment variable definitions are experimental and may be changed
166
+ // or removed in the future.
167
+ `fragment ${name}${wrap('(', join(variableDefinitions, ', '), ')')} ` +
168
+ `on ${typeCondition} ${wrap('', join(directives, ' '), ' ')}` +
169
+ selectionSet,
170
+ },
171
+ // Value
172
+ IntValue: { leave: ({ value }) => value },
173
+ FloatValue: { leave: ({ value }) => value },
174
+ StringValue: {
175
+ leave: ({ value, block: isBlockString }) => {
176
+ if (isBlockString) {
177
+ return printBlockString(value);
178
+ }
179
+ return JSON.stringify(value);
180
+ },
181
+ },
182
+ BooleanValue: { leave: ({ value }) => (value ? 'true' : 'false') },
183
+ NullValue: { leave: () => 'null' },
184
+ EnumValue: { leave: ({ value }) => value },
185
+ ListValue: { leave: ({ values }) => '[' + join(values, ', ') + ']' },
186
+ ObjectValue: { leave: ({ fields }) => '{' + join(fields, ', ') + '}' },
187
+ ObjectField: { leave: ({ name, value }) => name + ': ' + value },
188
+ // Directive
189
+ Directive: {
190
+ leave: ({ name, arguments: args }) => '@' + name + wrap('(', join(args, ', '), ')'),
191
+ },
192
+ // Type
193
+ NamedType: { leave: ({ name }) => name },
194
+ ListType: { leave: ({ type }) => '[' + type + ']' },
195
+ NonNullType: { leave: ({ type }) => type + '!' },
196
+ // Type System Definitions
197
+ SchemaDefinition: {
198
+ leave: ({ directives, operationTypes }) => join(['schema', join(directives, ' '), block(operationTypes)], ' '),
199
+ },
200
+ OperationTypeDefinition: {
201
+ leave: ({ operation, type }) => operation + ': ' + type,
202
+ },
203
+ ScalarTypeDefinition: {
204
+ leave: ({ name, directives }) => join(['scalar', name, join(directives, ' ')], ' '),
205
+ },
206
+ ObjectTypeDefinition: {
207
+ leave: ({ name, interfaces, directives, fields }) => join(['type', name, wrap('implements ', join(interfaces, ' & ')), join(directives, ' '), block(fields)], ' '),
208
+ },
209
+ FieldDefinition: {
210
+ leave: ({ name, arguments: args, type, directives }) => name +
211
+ (hasMultilineItems(args)
212
+ ? wrap('(\n', indent(join(args, '\n')), '\n)')
213
+ : wrap('(', join(args, ', '), ')')) +
214
+ ': ' +
215
+ type +
216
+ wrap(' ', join(directives, ' ')),
217
+ },
218
+ InputValueDefinition: {
219
+ leave: ({ name, type, defaultValue, directives }) => join([name + ': ' + type, wrap('= ', defaultValue), join(directives, ' ')], ' '),
220
+ },
221
+ InterfaceTypeDefinition: {
222
+ leave: ({ name, interfaces, directives, fields }) => join(['interface', name, wrap('implements ', join(interfaces, ' & ')), join(directives, ' '), block(fields)], ' '),
223
+ },
224
+ UnionTypeDefinition: {
225
+ leave: ({ name, directives, types }) => join(['union', name, join(directives, ' '), wrap('= ', join(types, ' | '))], ' '),
226
+ },
227
+ EnumTypeDefinition: {
228
+ leave: ({ name, directives, values }) => join(['enum', name, join(directives, ' '), block(values)], ' '),
229
+ },
230
+ EnumValueDefinition: {
231
+ leave: ({ name, directives }) => join([name, join(directives, ' ')], ' '),
232
+ },
233
+ InputObjectTypeDefinition: {
234
+ leave: ({ name, directives, fields }) => join(['input', name, join(directives, ' '), block(fields)], ' '),
235
+ },
236
+ DirectiveDefinition: {
237
+ leave: ({ name, arguments: args, repeatable, locations }) => 'directive @' +
238
+ name +
239
+ (hasMultilineItems(args)
240
+ ? wrap('(\n', indent(join(args, '\n')), '\n)')
241
+ : wrap('(', join(args, ', '), ')')) +
242
+ (repeatable ? ' repeatable' : '') +
243
+ ' on ' +
244
+ join(locations, ' | '),
245
+ },
246
+ SchemaExtension: {
247
+ leave: ({ directives, operationTypes }) => join(['extend schema', join(directives, ' '), block(operationTypes)], ' '),
248
+ },
249
+ ScalarTypeExtension: {
250
+ leave: ({ name, directives }) => join(['extend scalar', name, join(directives, ' ')], ' '),
251
+ },
252
+ ObjectTypeExtension: {
253
+ leave: ({ name, interfaces, directives, fields }) => join(['extend type', name, wrap('implements ', join(interfaces, ' & ')), join(directives, ' '), block(fields)], ' '),
254
+ },
255
+ InterfaceTypeExtension: {
256
+ leave: ({ name, interfaces, directives, fields }) => join(['extend interface', name, wrap('implements ', join(interfaces, ' & ')), join(directives, ' '), block(fields)], ' '),
257
+ },
258
+ UnionTypeExtension: {
259
+ leave: ({ name, directives, types }) => join(['extend union', name, join(directives, ' '), wrap('= ', join(types, ' | '))], ' '),
260
+ },
261
+ EnumTypeExtension: {
262
+ leave: ({ name, directives, values }) => join(['extend enum', name, join(directives, ' '), block(values)], ' '),
263
+ },
264
+ InputObjectTypeExtension: {
265
+ leave: ({ name, directives, fields }) => join(['extend input', name, join(directives, ' '), block(fields)], ' '),
266
+ },
267
+ };
268
+ const printDocASTReducerWithComments = Object.keys(printDocASTReducer).reduce((prev, key) => ({
269
+ ...prev,
270
+ [key]: {
271
+ leave: addDescription(printDocASTReducer[key].leave),
272
+ },
273
+ }), {});
274
+ /**
275
+ * Converts an AST into a string, using one set of reasonable
276
+ * formatting rules.
277
+ */
278
+ export function printWithComments(ast) {
279
+ return visit(ast, printDocASTReducerWithComments);
280
+ }
281
+ function isFieldDefinitionNode(node) {
282
+ return node.kind === 'FieldDefinition';
283
+ }
284
+ // graphql < v13 and > v15 does not export getDescription
285
+ export function getDescription(node, options) {
286
+ if (node.description != null) {
287
+ return node.description.value;
288
+ }
289
+ if (options === null || options === void 0 ? void 0 : options.commentDescriptions) {
290
+ return getComment(node);
291
+ }
292
+ }
293
+ export function getComment(node) {
294
+ const rawValue = getLeadingCommentBlock(node);
295
+ if (rawValue !== undefined) {
296
+ return dedentBlockStringValue(`\n${rawValue}`);
297
+ }
298
+ }
299
+ export function getLeadingCommentBlock(node) {
300
+ const loc = node.loc;
301
+ if (!loc) {
302
+ return;
303
+ }
304
+ const comments = [];
305
+ let token = loc.startToken.prev;
306
+ while (token != null &&
307
+ token.kind === TokenKind.COMMENT &&
308
+ token.next != null &&
309
+ token.prev != null &&
310
+ token.line + 1 === token.next.line &&
311
+ token.line !== token.prev.line) {
312
+ const value = String(token.value);
313
+ comments.push(value);
314
+ token = token.prev;
315
+ }
316
+ return comments.length > 0 ? comments.reverse().join('\n') : undefined;
317
+ }
318
+ export function dedentBlockStringValue(rawString) {
319
+ // Expand a block string's raw value into independent lines.
320
+ const lines = rawString.split(/\r\n|[\n\r]/g);
321
+ // Remove common indentation from all lines but first.
322
+ const commonIndent = getBlockStringIndentation(lines);
323
+ if (commonIndent !== 0) {
324
+ for (let i = 1; i < lines.length; i++) {
325
+ lines[i] = lines[i].slice(commonIndent);
326
+ }
327
+ }
328
+ // Remove leading and trailing blank lines.
329
+ while (lines.length > 0 && isBlank(lines[0])) {
330
+ lines.shift();
331
+ }
332
+ while (lines.length > 0 && isBlank(lines[lines.length - 1])) {
333
+ lines.pop();
334
+ }
335
+ // Return a string of the lines joined with U+000A.
336
+ return lines.join('\n');
337
+ }
338
+ /**
339
+ * @internal
340
+ */
341
+ export function getBlockStringIndentation(lines) {
342
+ let commonIndent = null;
343
+ for (let i = 1; i < lines.length; i++) {
344
+ const line = lines[i];
345
+ const indent = leadingWhitespace(line);
346
+ if (indent === line.length) {
347
+ continue; // skip empty lines
348
+ }
349
+ if (commonIndent === null || indent < commonIndent) {
350
+ commonIndent = indent;
351
+ if (commonIndent === 0) {
352
+ break;
353
+ }
354
+ }
355
+ }
356
+ return commonIndent === null ? 0 : commonIndent;
357
+ }
358
+ function leadingWhitespace(str) {
359
+ let i = 0;
360
+ while (i < str.length && (str[i] === ' ' || str[i] === '\t')) {
361
+ i++;
362
+ }
363
+ return i;
364
+ }
365
+ function isBlank(str) {
366
+ return leadingWhitespace(str) === str.length;
367
+ }
package/esm/errors.js ADDED
@@ -0,0 +1,17 @@
1
+ import { GraphQLError, versionInfo } from 'graphql';
2
+ export function createGraphQLError(message, options) {
3
+ if (versionInfo.major >= 17) {
4
+ return new GraphQLError(message, options);
5
+ }
6
+ return new GraphQLError(message, options === null || options === void 0 ? void 0 : options.nodes, options === null || options === void 0 ? void 0 : options.source, options === null || options === void 0 ? void 0 : options.positions, options === null || options === void 0 ? void 0 : options.path, options === null || options === void 0 ? void 0 : options.originalError, options === null || options === void 0 ? void 0 : options.extensions);
7
+ }
8
+ export function relocatedError(originalError, path) {
9
+ return createGraphQLError(originalError.message, {
10
+ nodes: originalError.nodes,
11
+ source: originalError.source,
12
+ positions: originalError.positions,
13
+ path: path == null ? originalError.path : path,
14
+ originalError,
15
+ extensions: originalError.extensions,
16
+ });
17
+ }
@@ -0,0 +1 @@
1
+ export {};
package/esm/fields.js ADDED
@@ -0,0 +1,108 @@
1
+ import { GraphQLObjectType } from 'graphql';
2
+ import { MapperKind } from './Interfaces.js';
3
+ import { mapSchema, correctASTNodes } from './mapSchema.js';
4
+ import { addTypes } from './addTypes.js';
5
+ export function appendObjectFields(schema, typeName, additionalFields) {
6
+ if (schema.getType(typeName) == null) {
7
+ return addTypes(schema, [
8
+ new GraphQLObjectType({
9
+ name: typeName,
10
+ fields: additionalFields,
11
+ }),
12
+ ]);
13
+ }
14
+ return mapSchema(schema, {
15
+ [MapperKind.OBJECT_TYPE]: type => {
16
+ if (type.name === typeName) {
17
+ const config = type.toConfig();
18
+ const originalFieldConfigMap = config.fields;
19
+ const newFieldConfigMap = {};
20
+ for (const fieldName in originalFieldConfigMap) {
21
+ newFieldConfigMap[fieldName] = originalFieldConfigMap[fieldName];
22
+ }
23
+ for (const fieldName in additionalFields) {
24
+ newFieldConfigMap[fieldName] = additionalFields[fieldName];
25
+ }
26
+ return correctASTNodes(new GraphQLObjectType({
27
+ ...config,
28
+ fields: newFieldConfigMap,
29
+ }));
30
+ }
31
+ },
32
+ });
33
+ }
34
+ export function removeObjectFields(schema, typeName, testFn) {
35
+ const removedFields = {};
36
+ const newSchema = mapSchema(schema, {
37
+ [MapperKind.OBJECT_TYPE]: type => {
38
+ if (type.name === typeName) {
39
+ const config = type.toConfig();
40
+ const originalFieldConfigMap = config.fields;
41
+ const newFieldConfigMap = {};
42
+ for (const fieldName in originalFieldConfigMap) {
43
+ const originalFieldConfig = originalFieldConfigMap[fieldName];
44
+ if (testFn(fieldName, originalFieldConfig)) {
45
+ removedFields[fieldName] = originalFieldConfig;
46
+ }
47
+ else {
48
+ newFieldConfigMap[fieldName] = originalFieldConfig;
49
+ }
50
+ }
51
+ return correctASTNodes(new GraphQLObjectType({
52
+ ...config,
53
+ fields: newFieldConfigMap,
54
+ }));
55
+ }
56
+ },
57
+ });
58
+ return [newSchema, removedFields];
59
+ }
60
+ export function selectObjectFields(schema, typeName, testFn) {
61
+ const selectedFields = {};
62
+ mapSchema(schema, {
63
+ [MapperKind.OBJECT_TYPE]: type => {
64
+ if (type.name === typeName) {
65
+ const config = type.toConfig();
66
+ const originalFieldConfigMap = config.fields;
67
+ for (const fieldName in originalFieldConfigMap) {
68
+ const originalFieldConfig = originalFieldConfigMap[fieldName];
69
+ if (testFn(fieldName, originalFieldConfig)) {
70
+ selectedFields[fieldName] = originalFieldConfig;
71
+ }
72
+ }
73
+ }
74
+ return undefined;
75
+ },
76
+ });
77
+ return selectedFields;
78
+ }
79
+ export function modifyObjectFields(schema, typeName, testFn, newFields) {
80
+ const removedFields = {};
81
+ const newSchema = mapSchema(schema, {
82
+ [MapperKind.OBJECT_TYPE]: type => {
83
+ if (type.name === typeName) {
84
+ const config = type.toConfig();
85
+ const originalFieldConfigMap = config.fields;
86
+ const newFieldConfigMap = {};
87
+ for (const fieldName in originalFieldConfigMap) {
88
+ const originalFieldConfig = originalFieldConfigMap[fieldName];
89
+ if (testFn(fieldName, originalFieldConfig)) {
90
+ removedFields[fieldName] = originalFieldConfig;
91
+ }
92
+ else {
93
+ newFieldConfigMap[fieldName] = originalFieldConfig;
94
+ }
95
+ }
96
+ for (const fieldName in newFields) {
97
+ const fieldConfig = newFields[fieldName];
98
+ newFieldConfigMap[fieldName] = fieldConfig;
99
+ }
100
+ return correctASTNodes(new GraphQLObjectType({
101
+ ...config,
102
+ fields: newFieldConfigMap,
103
+ }));
104
+ }
105
+ },
106
+ });
107
+ return [newSchema, removedFields];
108
+ }
@@ -0,0 +1,62 @@
1
+ import { GraphQLInputObjectType, GraphQLInterfaceType, GraphQLObjectType, } from 'graphql';
2
+ import { MapperKind } from './Interfaces.js';
3
+ import { mapSchema } from './mapSchema.js';
4
+ export function filterSchema({ schema, typeFilter = () => true, fieldFilter = undefined, rootFieldFilter = undefined, objectFieldFilter = undefined, interfaceFieldFilter = undefined, inputObjectFieldFilter = undefined, argumentFilter = undefined, }) {
5
+ const filteredSchema = mapSchema(schema, {
6
+ [MapperKind.QUERY]: (type) => filterRootFields(type, 'Query', rootFieldFilter, argumentFilter),
7
+ [MapperKind.MUTATION]: (type) => filterRootFields(type, 'Mutation', rootFieldFilter, argumentFilter),
8
+ [MapperKind.SUBSCRIPTION]: (type) => filterRootFields(type, 'Subscription', rootFieldFilter, argumentFilter),
9
+ [MapperKind.OBJECT_TYPE]: (type) => typeFilter(type.name, type)
10
+ ? filterElementFields(GraphQLObjectType, type, objectFieldFilter || fieldFilter, argumentFilter)
11
+ : null,
12
+ [MapperKind.INTERFACE_TYPE]: (type) => typeFilter(type.name, type)
13
+ ? filterElementFields(GraphQLInterfaceType, type, interfaceFieldFilter || fieldFilter, argumentFilter)
14
+ : null,
15
+ [MapperKind.INPUT_OBJECT_TYPE]: (type) => typeFilter(type.name, type)
16
+ ? filterElementFields(GraphQLInputObjectType, type, inputObjectFieldFilter || fieldFilter)
17
+ : null,
18
+ [MapperKind.UNION_TYPE]: (type) => (typeFilter(type.name, type) ? undefined : null),
19
+ [MapperKind.ENUM_TYPE]: (type) => (typeFilter(type.name, type) ? undefined : null),
20
+ [MapperKind.SCALAR_TYPE]: (type) => (typeFilter(type.name, type) ? undefined : null),
21
+ });
22
+ return filteredSchema;
23
+ }
24
+ function filterRootFields(type, operation, rootFieldFilter, argumentFilter) {
25
+ if (rootFieldFilter || argumentFilter) {
26
+ const config = type.toConfig();
27
+ for (const fieldName in config.fields) {
28
+ const field = config.fields[fieldName];
29
+ if (rootFieldFilter && !rootFieldFilter(operation, fieldName, config.fields[fieldName])) {
30
+ delete config.fields[fieldName];
31
+ }
32
+ else if (argumentFilter && field.args) {
33
+ for (const argName in field.args) {
34
+ if (!argumentFilter(operation, fieldName, argName, field.args[argName])) {
35
+ delete field.args[argName];
36
+ }
37
+ }
38
+ }
39
+ }
40
+ return new GraphQLObjectType(config);
41
+ }
42
+ return type;
43
+ }
44
+ function filterElementFields(ElementConstructor, type, fieldFilter, argumentFilter) {
45
+ if (fieldFilter || argumentFilter) {
46
+ const config = type.toConfig();
47
+ for (const fieldName in config.fields) {
48
+ const field = config.fields[fieldName];
49
+ if (fieldFilter && !fieldFilter(type.name, fieldName, config.fields[fieldName])) {
50
+ delete config.fields[fieldName];
51
+ }
52
+ else if (argumentFilter && 'args' in field) {
53
+ for (const argName in field.args) {
54
+ if (!argumentFilter(type.name, fieldName, argName, field.args[argName])) {
55
+ delete field.args[argName];
56
+ }
57
+ }
58
+ }
59
+ }
60
+ return new ElementConstructor(config);
61
+ }
62
+ }
@@ -0,0 +1,22 @@
1
+ import { buildASTSchema } from 'graphql';
2
+ import { getDocumentNodeFromSchema } from './print-schema-with-directives.js';
3
+ function buildFixedSchema(schema, options) {
4
+ const document = getDocumentNodeFromSchema(schema);
5
+ return buildASTSchema(document, {
6
+ ...(options || {}),
7
+ });
8
+ }
9
+ export function fixSchemaAst(schema, options) {
10
+ // eslint-disable-next-line no-undef-init
11
+ let schemaWithValidAst = undefined;
12
+ if (!schema.astNode || !schema.extensionASTNodes) {
13
+ schemaWithValidAst = buildFixedSchema(schema, options);
14
+ }
15
+ if (!schema.astNode && (schemaWithValidAst === null || schemaWithValidAst === void 0 ? void 0 : schemaWithValidAst.astNode)) {
16
+ schema.astNode = schemaWithValidAst.astNode;
17
+ }
18
+ if (!schema.extensionASTNodes && (schemaWithValidAst === null || schemaWithValidAst === void 0 ? void 0 : schemaWithValidAst.astNode)) {
19
+ schema.extensionASTNodes = schemaWithValidAst.extensionASTNodes;
20
+ }
21
+ return schema;
22
+ }
@@ -0,0 +1,25 @@
1
+ import { getNamedType, isObjectType, isInputObjectType } from 'graphql';
2
+ export function forEachDefaultValue(schema, fn) {
3
+ const typeMap = schema.getTypeMap();
4
+ for (const typeName in typeMap) {
5
+ const type = typeMap[typeName];
6
+ if (!getNamedType(type).name.startsWith('__')) {
7
+ if (isObjectType(type)) {
8
+ const fields = type.getFields();
9
+ for (const fieldName in fields) {
10
+ const field = fields[fieldName];
11
+ for (const arg of field.args) {
12
+ arg.defaultValue = fn(arg.type, arg.defaultValue);
13
+ }
14
+ }
15
+ }
16
+ else if (isInputObjectType(type)) {
17
+ const fields = type.getFields();
18
+ for (const fieldName in fields) {
19
+ const field = fields[fieldName];
20
+ field.defaultValue = fn(field.type, field.defaultValue);
21
+ }
22
+ }
23
+ }
24
+ }
25
+ }
@@ -0,0 +1,15 @@
1
+ import { getNamedType, isObjectType } from 'graphql';
2
+ export function forEachField(schema, fn) {
3
+ const typeMap = schema.getTypeMap();
4
+ for (const typeName in typeMap) {
5
+ const type = typeMap[typeName];
6
+ // TODO: maybe have an option to include these?
7
+ if (!getNamedType(type).name.startsWith('__') && isObjectType(type)) {
8
+ const fields = type.getFields();
9
+ for (const fieldName in fields) {
10
+ const field = fields[fieldName];
11
+ fn(field, typeName, fieldName);
12
+ }
13
+ }
14
+ }
15
+ }