@envelop/testing 3.0.0-alpha-15b486f.0 → 3.0.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 (3) hide show
  1. package/index.js +6 -942
  2. package/index.mjs +3 -939
  3. package/package.json +9 -5
package/index.mjs CHANGED
@@ -1,942 +1,6 @@
1
- import { GraphQLSchema, Kind, isObjectType, isEnumType, GraphQLEnumType, isListType, GraphQLList, isNonNullType, GraphQLNonNull, isNamedType, isInterfaceType, isInputObjectType, GraphQLObjectType, GraphQLInterfaceType, GraphQLInputObjectType, isSpecifiedDirective, GraphQLDirective, isUnionType, GraphQLUnionType, isScalarType, isSpecifiedScalarType, GraphQLScalarType, isLeafType, GraphQLID, GraphQLBoolean, GraphQLString, GraphQLFloat, GraphQLInt, getNullableType, getOperationAST, GraphQLError, print } from 'graphql';
2
- import { envelop, useSchema } from '@envelop/core';
3
- import { isAsyncIterable } from '@envelop/types';
4
-
5
- const MAX_LINE_LENGTH = 80;
6
- let commentsRegistry = {};
7
- function printComment(comment) {
8
- return '\n# ' + comment.replace(/\n/g, '\n# ');
9
- }
10
- /**
11
- * Copyright (c) 2015-present, Facebook, Inc.
12
- *
13
- * This source code is licensed under the MIT license found in the
14
- * LICENSE file in the root directory of this source tree.
15
- */
16
- /**
17
- * NOTE: ==> This file has been modified just to add comments to the printed AST
18
- * This is a temp measure, we will move to using the original non modified printer.js ASAP.
19
- */
20
- /**
21
- * Given maybeArray, print an empty string if it is null or empty, otherwise
22
- * print all items together separated by separator if provided
23
- */
24
- function join(maybeArray, separator) {
25
- return maybeArray ? maybeArray.filter(x => x).join(separator || '') : '';
26
- }
27
- function hasMultilineItems(maybeArray) {
28
- var _a;
29
- return (_a = maybeArray === null || maybeArray === void 0 ? void 0 : maybeArray.some(str => str.includes('\n'))) !== null && _a !== void 0 ? _a : false;
30
- }
31
- function addDescription(cb) {
32
- return (node, _key, _parent, path, ancestors) => {
33
- var _a;
34
- const keys = [];
35
- const parent = path.reduce((prev, key) => {
36
- if (['fields', 'arguments', 'values'].includes(key) && prev.name) {
37
- keys.push(prev.name.value);
38
- }
39
- return prev[key];
40
- }, ancestors[0]);
41
- const key = [...keys, (_a = parent === null || parent === void 0 ? void 0 : parent.name) === null || _a === void 0 ? void 0 : _a.value].filter(Boolean).join('.');
42
- const items = [];
43
- if (node.kind.includes('Definition') && commentsRegistry[key]) {
44
- items.push(...commentsRegistry[key]);
45
- }
46
- return join([...items.map(printComment), node.description, cb(node, _key, _parent, path, ancestors)], '\n');
47
- };
48
- }
49
- function indent(maybeString) {
50
- return maybeString && ` ${maybeString.replace(/\n/g, '\n ')}`;
51
- }
52
- /**
53
- * Given array, print each item on its own line, wrapped in an
54
- * indented "{ }" block.
55
- */
56
- function block(array) {
57
- return array && array.length !== 0 ? `{\n${indent(join(array, '\n'))}\n}` : '';
58
- }
59
- /**
60
- * If maybeString is not null or empty, then wrap with start and end, otherwise
61
- * print an empty string.
62
- */
63
- function wrap(start, maybeString, end) {
64
- return maybeString ? start + maybeString + (end || '') : '';
65
- }
66
- /**
67
- * Print a block string in the indented block form by adding a leading and
68
- * trailing blank line. However, if a block string starts with whitespace and is
69
- * a single-line, adding a leading blank line would strip that whitespace.
70
- */
71
- function printBlockString(value, isDescription = false) {
72
- const escaped = value.replace(/"""/g, '\\"""');
73
- return (value[0] === ' ' || value[0] === '\t') && value.indexOf('\n') === -1
74
- ? `"""${escaped.replace(/"$/, '"\n')}"""`
75
- : `"""\n${isDescription ? escaped : indent(escaped)}\n"""`;
76
- }
77
- const printDocASTReducer = {
78
- Name: { leave: node => node.value },
79
- Variable: { leave: node => '$' + node.name },
80
- // Document
81
- Document: {
82
- leave: node => join(node.definitions, '\n\n'),
83
- },
84
- OperationDefinition: {
85
- leave: node => {
86
- const varDefs = wrap('(', join(node.variableDefinitions, ', '), ')');
87
- const prefix = join([node.operation, join([node.name, varDefs]), join(node.directives, ' ')], ' ');
88
- // the query short form.
89
- return prefix + ' ' + node.selectionSet;
90
- },
91
- },
92
- VariableDefinition: {
93
- leave: ({ variable, type, defaultValue, directives }) => variable + ': ' + type + wrap(' = ', defaultValue) + wrap(' ', join(directives, ' ')),
94
- },
95
- SelectionSet: { leave: ({ selections }) => block(selections) },
96
- Field: {
97
- leave({ alias, name, arguments: args, directives, selectionSet }) {
98
- const prefix = wrap('', alias, ': ') + name;
99
- let argsLine = prefix + wrap('(', join(args, ', '), ')');
100
- if (argsLine.length > MAX_LINE_LENGTH) {
101
- argsLine = prefix + wrap('(\n', indent(join(args, '\n')), '\n)');
102
- }
103
- return join([argsLine, join(directives, ' '), selectionSet], ' ');
104
- },
105
- },
106
- Argument: { leave: ({ name, value }) => name + ': ' + value },
107
- // Fragments
108
- FragmentSpread: {
109
- leave: ({ name, directives }) => '...' + name + wrap(' ', join(directives, ' ')),
110
- },
111
- InlineFragment: {
112
- leave: ({ typeCondition, directives, selectionSet }) => join(['...', wrap('on ', typeCondition), join(directives, ' '), selectionSet], ' '),
113
- },
114
- FragmentDefinition: {
115
- leave: ({ name, typeCondition, variableDefinitions, directives, selectionSet }) =>
116
- // Note: fragment variable definitions are experimental and may be changed
117
- // or removed in the future.
118
- `fragment ${name}${wrap('(', join(variableDefinitions, ', '), ')')} ` +
119
- `on ${typeCondition} ${wrap('', join(directives, ' '), ' ')}` +
120
- selectionSet,
121
- },
122
- // Value
123
- IntValue: { leave: ({ value }) => value },
124
- FloatValue: { leave: ({ value }) => value },
125
- StringValue: {
126
- leave: ({ value, block: isBlockString }) => {
127
- if (isBlockString) {
128
- return printBlockString(value);
129
- }
130
- return JSON.stringify(value);
131
- },
132
- },
133
- BooleanValue: { leave: ({ value }) => (value ? 'true' : 'false') },
134
- NullValue: { leave: () => 'null' },
135
- EnumValue: { leave: ({ value }) => value },
136
- ListValue: { leave: ({ values }) => '[' + join(values, ', ') + ']' },
137
- ObjectValue: { leave: ({ fields }) => '{' + join(fields, ', ') + '}' },
138
- ObjectField: { leave: ({ name, value }) => name + ': ' + value },
139
- // Directive
140
- Directive: {
141
- leave: ({ name, arguments: args }) => '@' + name + wrap('(', join(args, ', '), ')'),
142
- },
143
- // Type
144
- NamedType: { leave: ({ name }) => name },
145
- ListType: { leave: ({ type }) => '[' + type + ']' },
146
- NonNullType: { leave: ({ type }) => type + '!' },
147
- // Type System Definitions
148
- SchemaDefinition: {
149
- leave: ({ directives, operationTypes }) => join(['schema', join(directives, ' '), block(operationTypes)], ' '),
150
- },
151
- OperationTypeDefinition: {
152
- leave: ({ operation, type }) => operation + ': ' + type,
153
- },
154
- ScalarTypeDefinition: {
155
- leave: ({ name, directives }) => join(['scalar', name, join(directives, ' ')], ' '),
156
- },
157
- ObjectTypeDefinition: {
158
- leave: ({ name, interfaces, directives, fields }) => join(['type', name, wrap('implements ', join(interfaces, ' & ')), join(directives, ' '), block(fields)], ' '),
159
- },
160
- FieldDefinition: {
161
- leave: ({ name, arguments: args, type, directives }) => name +
162
- (hasMultilineItems(args)
163
- ? wrap('(\n', indent(join(args, '\n')), '\n)')
164
- : wrap('(', join(args, ', '), ')')) +
165
- ': ' +
166
- type +
167
- wrap(' ', join(directives, ' ')),
168
- },
169
- InputValueDefinition: {
170
- leave: ({ name, type, defaultValue, directives }) => join([name + ': ' + type, wrap('= ', defaultValue), join(directives, ' ')], ' '),
171
- },
172
- InterfaceTypeDefinition: {
173
- leave: ({ name, interfaces, directives, fields }) => join(['interface', name, wrap('implements ', join(interfaces, ' & ')), join(directives, ' '), block(fields)], ' '),
174
- },
175
- UnionTypeDefinition: {
176
- leave: ({ name, directives, types }) => join(['union', name, join(directives, ' '), wrap('= ', join(types, ' | '))], ' '),
177
- },
178
- EnumTypeDefinition: {
179
- leave: ({ name, directives, values }) => join(['enum', name, join(directives, ' '), block(values)], ' '),
180
- },
181
- EnumValueDefinition: {
182
- leave: ({ name, directives }) => join([name, join(directives, ' ')], ' '),
183
- },
184
- InputObjectTypeDefinition: {
185
- leave: ({ name, directives, fields }) => join(['input', name, join(directives, ' '), block(fields)], ' '),
186
- },
187
- DirectiveDefinition: {
188
- leave: ({ name, arguments: args, repeatable, locations }) => 'directive @' +
189
- name +
190
- (hasMultilineItems(args)
191
- ? wrap('(\n', indent(join(args, '\n')), '\n)')
192
- : wrap('(', join(args, ', '), ')')) +
193
- (repeatable ? ' repeatable' : '') +
194
- ' on ' +
195
- join(locations, ' | '),
196
- },
197
- SchemaExtension: {
198
- leave: ({ directives, operationTypes }) => join(['extend schema', join(directives, ' '), block(operationTypes)], ' '),
199
- },
200
- ScalarTypeExtension: {
201
- leave: ({ name, directives }) => join(['extend scalar', name, join(directives, ' ')], ' '),
202
- },
203
- ObjectTypeExtension: {
204
- leave: ({ name, interfaces, directives, fields }) => join(['extend type', name, wrap('implements ', join(interfaces, ' & ')), join(directives, ' '), block(fields)], ' '),
205
- },
206
- InterfaceTypeExtension: {
207
- leave: ({ name, interfaces, directives, fields }) => join(['extend interface', name, wrap('implements ', join(interfaces, ' & ')), join(directives, ' '), block(fields)], ' '),
208
- },
209
- UnionTypeExtension: {
210
- leave: ({ name, directives, types }) => join(['extend union', name, join(directives, ' '), wrap('= ', join(types, ' | '))], ' '),
211
- },
212
- EnumTypeExtension: {
213
- leave: ({ name, directives, values }) => join(['extend enum', name, join(directives, ' '), block(values)], ' '),
214
- },
215
- InputObjectTypeExtension: {
216
- leave: ({ name, directives, fields }) => join(['extend input', name, join(directives, ' '), block(fields)], ' '),
217
- },
218
- };
219
- const printDocASTReducerWithComments = Object.keys(printDocASTReducer).reduce((prev, key) => ({
220
- ...prev,
221
- [key]: {
222
- leave: addDescription(printDocASTReducer[key].leave),
223
- },
224
- }), {});
225
-
226
- var MapperKind;
227
- (function (MapperKind) {
228
- MapperKind["TYPE"] = "MapperKind.TYPE";
229
- MapperKind["SCALAR_TYPE"] = "MapperKind.SCALAR_TYPE";
230
- MapperKind["ENUM_TYPE"] = "MapperKind.ENUM_TYPE";
231
- MapperKind["COMPOSITE_TYPE"] = "MapperKind.COMPOSITE_TYPE";
232
- MapperKind["OBJECT_TYPE"] = "MapperKind.OBJECT_TYPE";
233
- MapperKind["INPUT_OBJECT_TYPE"] = "MapperKind.INPUT_OBJECT_TYPE";
234
- MapperKind["ABSTRACT_TYPE"] = "MapperKind.ABSTRACT_TYPE";
235
- MapperKind["UNION_TYPE"] = "MapperKind.UNION_TYPE";
236
- MapperKind["INTERFACE_TYPE"] = "MapperKind.INTERFACE_TYPE";
237
- MapperKind["ROOT_OBJECT"] = "MapperKind.ROOT_OBJECT";
238
- MapperKind["QUERY"] = "MapperKind.QUERY";
239
- MapperKind["MUTATION"] = "MapperKind.MUTATION";
240
- MapperKind["SUBSCRIPTION"] = "MapperKind.SUBSCRIPTION";
241
- MapperKind["DIRECTIVE"] = "MapperKind.DIRECTIVE";
242
- MapperKind["FIELD"] = "MapperKind.FIELD";
243
- MapperKind["COMPOSITE_FIELD"] = "MapperKind.COMPOSITE_FIELD";
244
- MapperKind["OBJECT_FIELD"] = "MapperKind.OBJECT_FIELD";
245
- MapperKind["ROOT_FIELD"] = "MapperKind.ROOT_FIELD";
246
- MapperKind["QUERY_ROOT_FIELD"] = "MapperKind.QUERY_ROOT_FIELD";
247
- MapperKind["MUTATION_ROOT_FIELD"] = "MapperKind.MUTATION_ROOT_FIELD";
248
- MapperKind["SUBSCRIPTION_ROOT_FIELD"] = "MapperKind.SUBSCRIPTION_ROOT_FIELD";
249
- MapperKind["INTERFACE_FIELD"] = "MapperKind.INTERFACE_FIELD";
250
- MapperKind["INPUT_OBJECT_FIELD"] = "MapperKind.INPUT_OBJECT_FIELD";
251
- MapperKind["ARGUMENT"] = "MapperKind.ARGUMENT";
252
- MapperKind["ENUM_VALUE"] = "MapperKind.ENUM_VALUE";
253
- })(MapperKind || (MapperKind = {}));
254
-
255
- function getObjectTypeFromTypeMap(typeMap, type) {
256
- if (type) {
257
- const maybeObjectType = typeMap[type.name];
258
- if (isObjectType(maybeObjectType)) {
259
- return maybeObjectType;
260
- }
261
- }
262
- }
263
- function isNamedStub(type) {
264
- if ('getFields' in type) {
265
- const fields = type.getFields();
266
- // eslint-disable-next-line no-unreachable-loop
267
- for (const fieldName in fields) {
268
- const field = fields[fieldName];
269
- return field.name === '_fake';
270
- }
271
- }
272
- return false;
273
- }
274
- function getBuiltInForStub(type) {
275
- switch (type.name) {
276
- case GraphQLInt.name:
277
- return GraphQLInt;
278
- case GraphQLFloat.name:
279
- return GraphQLFloat;
280
- case GraphQLString.name:
281
- return GraphQLString;
282
- case GraphQLBoolean.name:
283
- return GraphQLBoolean;
284
- case GraphQLID.name:
285
- return GraphQLID;
286
- default:
287
- return type;
288
- }
289
- }
290
-
291
- function rewireTypes(originalTypeMap, directives) {
292
- const referenceTypeMap = Object.create(null);
293
- for (const typeName in originalTypeMap) {
294
- referenceTypeMap[typeName] = originalTypeMap[typeName];
295
- }
296
- const newTypeMap = Object.create(null);
297
- for (const typeName in referenceTypeMap) {
298
- const namedType = referenceTypeMap[typeName];
299
- if (namedType == null || typeName.startsWith('__')) {
300
- continue;
301
- }
302
- const newName = namedType.name;
303
- if (newName.startsWith('__')) {
304
- continue;
305
- }
306
- if (newTypeMap[newName] != null) {
307
- throw new Error(`Duplicate schema type name ${newName}`);
308
- }
309
- newTypeMap[newName] = namedType;
310
- }
311
- for (const typeName in newTypeMap) {
312
- newTypeMap[typeName] = rewireNamedType(newTypeMap[typeName]);
313
- }
314
- const newDirectives = directives.map(directive => rewireDirective(directive));
315
- return {
316
- typeMap: newTypeMap,
317
- directives: newDirectives,
318
- };
319
- function rewireDirective(directive) {
320
- if (isSpecifiedDirective(directive)) {
321
- return directive;
322
- }
323
- const directiveConfig = directive.toConfig();
324
- directiveConfig.args = rewireArgs(directiveConfig.args);
325
- return new GraphQLDirective(directiveConfig);
326
- }
327
- function rewireArgs(args) {
328
- const rewiredArgs = {};
329
- for (const argName in args) {
330
- const arg = args[argName];
331
- const rewiredArgType = rewireType(arg.type);
332
- if (rewiredArgType != null) {
333
- arg.type = rewiredArgType;
334
- rewiredArgs[argName] = arg;
335
- }
336
- }
337
- return rewiredArgs;
338
- }
339
- function rewireNamedType(type) {
340
- if (isObjectType(type)) {
341
- const config = type.toConfig();
342
- const newConfig = {
343
- ...config,
344
- fields: () => rewireFields(config.fields),
345
- interfaces: () => rewireNamedTypes(config.interfaces),
346
- };
347
- return new GraphQLObjectType(newConfig);
348
- }
349
- else if (isInterfaceType(type)) {
350
- const config = type.toConfig();
351
- const newConfig = {
352
- ...config,
353
- fields: () => rewireFields(config.fields),
354
- };
355
- if ('interfaces' in newConfig) {
356
- newConfig.interfaces = () => rewireNamedTypes(config.interfaces);
357
- }
358
- return new GraphQLInterfaceType(newConfig);
359
- }
360
- else if (isUnionType(type)) {
361
- const config = type.toConfig();
362
- const newConfig = {
363
- ...config,
364
- types: () => rewireNamedTypes(config.types),
365
- };
366
- return new GraphQLUnionType(newConfig);
367
- }
368
- else if (isInputObjectType(type)) {
369
- const config = type.toConfig();
370
- const newConfig = {
371
- ...config,
372
- fields: () => rewireInputFields(config.fields),
373
- };
374
- return new GraphQLInputObjectType(newConfig);
375
- }
376
- else if (isEnumType(type)) {
377
- const enumConfig = type.toConfig();
378
- return new GraphQLEnumType(enumConfig);
379
- }
380
- else if (isScalarType(type)) {
381
- if (isSpecifiedScalarType(type)) {
382
- return type;
383
- }
384
- const scalarConfig = type.toConfig();
385
- return new GraphQLScalarType(scalarConfig);
386
- }
387
- throw new Error(`Unexpected schema type: ${type}`);
388
- }
389
- function rewireFields(fields) {
390
- const rewiredFields = {};
391
- for (const fieldName in fields) {
392
- const field = fields[fieldName];
393
- const rewiredFieldType = rewireType(field.type);
394
- if (rewiredFieldType != null && field.args) {
395
- field.type = rewiredFieldType;
396
- field.args = rewireArgs(field.args);
397
- rewiredFields[fieldName] = field;
398
- }
399
- }
400
- return rewiredFields;
401
- }
402
- function rewireInputFields(fields) {
403
- const rewiredFields = {};
404
- for (const fieldName in fields) {
405
- const field = fields[fieldName];
406
- const rewiredFieldType = rewireType(field.type);
407
- if (rewiredFieldType != null) {
408
- field.type = rewiredFieldType;
409
- rewiredFields[fieldName] = field;
410
- }
411
- }
412
- return rewiredFields;
413
- }
414
- function rewireNamedTypes(namedTypes) {
415
- const rewiredTypes = [];
416
- for (const namedType of namedTypes) {
417
- const rewiredType = rewireType(namedType);
418
- if (rewiredType != null) {
419
- rewiredTypes.push(rewiredType);
420
- }
421
- }
422
- return rewiredTypes;
423
- }
424
- function rewireType(type) {
425
- if (isListType(type)) {
426
- const rewiredType = rewireType(type.ofType);
427
- return rewiredType != null ? new GraphQLList(rewiredType) : null;
428
- }
429
- else if (isNonNullType(type)) {
430
- const rewiredType = rewireType(type.ofType);
431
- return rewiredType != null ? new GraphQLNonNull(rewiredType) : null;
432
- }
433
- else if (isNamedType(type)) {
434
- let rewiredType = referenceTypeMap[type.name];
435
- if (rewiredType === undefined) {
436
- rewiredType = isNamedStub(type) ? getBuiltInForStub(type) : rewireNamedType(type);
437
- newTypeMap[rewiredType.name] = referenceTypeMap[type.name] = rewiredType;
438
- }
439
- return rewiredType != null ? newTypeMap[rewiredType.name] : null;
440
- }
441
- return null;
442
- }
443
- }
444
-
445
- function transformInputValue(type, value, inputLeafValueTransformer = null, inputObjectValueTransformer = null) {
446
- if (value == null) {
447
- return value;
448
- }
449
- const nullableType = getNullableType(type);
450
- if (isLeafType(nullableType)) {
451
- return inputLeafValueTransformer != null ? inputLeafValueTransformer(nullableType, value) : value;
452
- }
453
- else if (isListType(nullableType)) {
454
- return value.map((listMember) => transformInputValue(nullableType.ofType, listMember, inputLeafValueTransformer, inputObjectValueTransformer));
455
- }
456
- else if (isInputObjectType(nullableType)) {
457
- const fields = nullableType.getFields();
458
- const newValue = {};
459
- for (const key in value) {
460
- const field = fields[key];
461
- if (field != null) {
462
- newValue[key] = transformInputValue(field.type, value[key], inputLeafValueTransformer, inputObjectValueTransformer);
463
- }
464
- }
465
- return inputObjectValueTransformer != null ? inputObjectValueTransformer(nullableType, newValue) : newValue;
466
- }
467
- // unreachable, no other possible return value
468
- }
469
- function serializeInputValue(type, value) {
470
- return transformInputValue(type, value, (t, v) => t.serialize(v));
471
- }
472
- function parseInputValue(type, value) {
473
- return transformInputValue(type, value, (t, v) => t.parseValue(v));
474
- }
475
-
476
- function mapSchema(schema, schemaMapper = {}) {
477
- const newTypeMap = mapArguments(mapFields(mapTypes(mapDefaultValues(mapEnumValues(mapTypes(mapDefaultValues(schema.getTypeMap(), schema, serializeInputValue), schema, schemaMapper, type => isLeafType(type)), schema, schemaMapper), schema, parseInputValue), schema, schemaMapper, type => !isLeafType(type)), schema, schemaMapper), schema, schemaMapper);
478
- const originalDirectives = schema.getDirectives();
479
- const newDirectives = mapDirectives(originalDirectives, schema, schemaMapper);
480
- const { typeMap, directives } = rewireTypes(newTypeMap, newDirectives);
481
- return new GraphQLSchema({
482
- ...schema.toConfig(),
483
- query: getObjectTypeFromTypeMap(typeMap, getObjectTypeFromTypeMap(newTypeMap, schema.getQueryType())),
484
- mutation: getObjectTypeFromTypeMap(typeMap, getObjectTypeFromTypeMap(newTypeMap, schema.getMutationType())),
485
- subscription: getObjectTypeFromTypeMap(typeMap, getObjectTypeFromTypeMap(newTypeMap, schema.getSubscriptionType())),
486
- types: Object.values(typeMap),
487
- directives,
488
- });
489
- }
490
- function mapTypes(originalTypeMap, schema, schemaMapper, testFn = () => true) {
491
- const newTypeMap = {};
492
- for (const typeName in originalTypeMap) {
493
- if (!typeName.startsWith('__')) {
494
- const originalType = originalTypeMap[typeName];
495
- if (originalType == null || !testFn(originalType)) {
496
- newTypeMap[typeName] = originalType;
497
- continue;
498
- }
499
- const typeMapper = getTypeMapper(schema, schemaMapper, typeName);
500
- if (typeMapper == null) {
501
- newTypeMap[typeName] = originalType;
502
- continue;
503
- }
504
- const maybeNewType = typeMapper(originalType, schema);
505
- if (maybeNewType === undefined) {
506
- newTypeMap[typeName] = originalType;
507
- continue;
508
- }
509
- newTypeMap[typeName] = maybeNewType;
510
- }
511
- }
512
- return newTypeMap;
513
- }
514
- function mapEnumValues(originalTypeMap, schema, schemaMapper) {
515
- const enumValueMapper = getEnumValueMapper(schemaMapper);
516
- if (!enumValueMapper) {
517
- return originalTypeMap;
518
- }
519
- return mapTypes(originalTypeMap, schema, {
520
- [MapperKind.ENUM_TYPE]: type => {
521
- const config = type.toConfig();
522
- const originalEnumValueConfigMap = config.values;
523
- const newEnumValueConfigMap = {};
524
- for (const externalValue in originalEnumValueConfigMap) {
525
- const originalEnumValueConfig = originalEnumValueConfigMap[externalValue];
526
- const mappedEnumValue = enumValueMapper(originalEnumValueConfig, type.name, schema, externalValue);
527
- if (mappedEnumValue === undefined) {
528
- newEnumValueConfigMap[externalValue] = originalEnumValueConfig;
529
- }
530
- else if (Array.isArray(mappedEnumValue)) {
531
- const [newExternalValue, newEnumValueConfig] = mappedEnumValue;
532
- newEnumValueConfigMap[newExternalValue] =
533
- newEnumValueConfig === undefined ? originalEnumValueConfig : newEnumValueConfig;
534
- }
535
- else if (mappedEnumValue !== null) {
536
- newEnumValueConfigMap[externalValue] = mappedEnumValue;
537
- }
538
- }
539
- return correctASTNodes(new GraphQLEnumType({
540
- ...config,
541
- values: newEnumValueConfigMap,
542
- }));
543
- },
544
- }, type => isEnumType(type));
545
- }
546
- function mapDefaultValues(originalTypeMap, schema, fn) {
547
- const newTypeMap = mapArguments(originalTypeMap, schema, {
548
- [MapperKind.ARGUMENT]: argumentConfig => {
549
- if (argumentConfig.defaultValue === undefined) {
550
- return argumentConfig;
551
- }
552
- const maybeNewType = getNewType(originalTypeMap, argumentConfig.type);
553
- if (maybeNewType != null) {
554
- return {
555
- ...argumentConfig,
556
- defaultValue: fn(maybeNewType, argumentConfig.defaultValue),
557
- };
558
- }
559
- },
560
- });
561
- return mapFields(newTypeMap, schema, {
562
- [MapperKind.INPUT_OBJECT_FIELD]: inputFieldConfig => {
563
- if (inputFieldConfig.defaultValue === undefined) {
564
- return inputFieldConfig;
565
- }
566
- const maybeNewType = getNewType(newTypeMap, inputFieldConfig.type);
567
- if (maybeNewType != null) {
568
- return {
569
- ...inputFieldConfig,
570
- defaultValue: fn(maybeNewType, inputFieldConfig.defaultValue),
571
- };
572
- }
573
- },
574
- });
575
- }
576
- function getNewType(newTypeMap, type) {
577
- if (isListType(type)) {
578
- const newType = getNewType(newTypeMap, type.ofType);
579
- return newType != null ? new GraphQLList(newType) : null;
580
- }
581
- else if (isNonNullType(type)) {
582
- const newType = getNewType(newTypeMap, type.ofType);
583
- return newType != null ? new GraphQLNonNull(newType) : null;
584
- }
585
- else if (isNamedType(type)) {
586
- const newType = newTypeMap[type.name];
587
- return newType != null ? newType : null;
588
- }
589
- return null;
590
- }
591
- function mapFields(originalTypeMap, schema, schemaMapper) {
592
- const newTypeMap = {};
593
- for (const typeName in originalTypeMap) {
594
- if (!typeName.startsWith('__')) {
595
- const originalType = originalTypeMap[typeName];
596
- if (!isObjectType(originalType) && !isInterfaceType(originalType) && !isInputObjectType(originalType)) {
597
- newTypeMap[typeName] = originalType;
598
- continue;
599
- }
600
- const fieldMapper = getFieldMapper(schema, schemaMapper, typeName);
601
- if (fieldMapper == null) {
602
- newTypeMap[typeName] = originalType;
603
- continue;
604
- }
605
- const config = originalType.toConfig();
606
- const originalFieldConfigMap = config.fields;
607
- const newFieldConfigMap = {};
608
- for (const fieldName in originalFieldConfigMap) {
609
- const originalFieldConfig = originalFieldConfigMap[fieldName];
610
- const mappedField = fieldMapper(originalFieldConfig, fieldName, typeName, schema);
611
- if (mappedField === undefined) {
612
- newFieldConfigMap[fieldName] = originalFieldConfig;
613
- }
614
- else if (Array.isArray(mappedField)) {
615
- const [newFieldName, newFieldConfig] = mappedField;
616
- if (newFieldConfig.astNode != null) {
617
- newFieldConfig.astNode = {
618
- ...newFieldConfig.astNode,
619
- name: {
620
- ...newFieldConfig.astNode.name,
621
- value: newFieldName,
622
- },
623
- };
624
- }
625
- newFieldConfigMap[newFieldName] = newFieldConfig === undefined ? originalFieldConfig : newFieldConfig;
626
- }
627
- else if (mappedField !== null) {
628
- newFieldConfigMap[fieldName] = mappedField;
629
- }
630
- }
631
- if (isObjectType(originalType)) {
632
- newTypeMap[typeName] = correctASTNodes(new GraphQLObjectType({
633
- ...config,
634
- fields: newFieldConfigMap,
635
- }));
636
- }
637
- else if (isInterfaceType(originalType)) {
638
- newTypeMap[typeName] = correctASTNodes(new GraphQLInterfaceType({
639
- ...config,
640
- fields: newFieldConfigMap,
641
- }));
642
- }
643
- else {
644
- newTypeMap[typeName] = correctASTNodes(new GraphQLInputObjectType({
645
- ...config,
646
- fields: newFieldConfigMap,
647
- }));
648
- }
649
- }
650
- }
651
- return newTypeMap;
652
- }
653
- function mapArguments(originalTypeMap, schema, schemaMapper) {
654
- const newTypeMap = {};
655
- for (const typeName in originalTypeMap) {
656
- if (!typeName.startsWith('__')) {
657
- const originalType = originalTypeMap[typeName];
658
- if (!isObjectType(originalType) && !isInterfaceType(originalType)) {
659
- newTypeMap[typeName] = originalType;
660
- continue;
661
- }
662
- const argumentMapper = getArgumentMapper(schemaMapper);
663
- if (argumentMapper == null) {
664
- newTypeMap[typeName] = originalType;
665
- continue;
666
- }
667
- const config = originalType.toConfig();
668
- const originalFieldConfigMap = config.fields;
669
- const newFieldConfigMap = {};
670
- for (const fieldName in originalFieldConfigMap) {
671
- const originalFieldConfig = originalFieldConfigMap[fieldName];
672
- const originalArgumentConfigMap = originalFieldConfig.args;
673
- if (originalArgumentConfigMap == null) {
674
- newFieldConfigMap[fieldName] = originalFieldConfig;
675
- continue;
676
- }
677
- const argumentNames = Object.keys(originalArgumentConfigMap);
678
- if (!argumentNames.length) {
679
- newFieldConfigMap[fieldName] = originalFieldConfig;
680
- continue;
681
- }
682
- const newArgumentConfigMap = {};
683
- for (const argumentName of argumentNames) {
684
- const originalArgumentConfig = originalArgumentConfigMap[argumentName];
685
- const mappedArgument = argumentMapper(originalArgumentConfig, fieldName, typeName, schema);
686
- if (mappedArgument === undefined) {
687
- newArgumentConfigMap[argumentName] = originalArgumentConfig;
688
- }
689
- else if (Array.isArray(mappedArgument)) {
690
- const [newArgumentName, newArgumentConfig] = mappedArgument;
691
- newArgumentConfigMap[newArgumentName] = newArgumentConfig;
692
- }
693
- else if (mappedArgument !== null) {
694
- newArgumentConfigMap[argumentName] = mappedArgument;
695
- }
696
- }
697
- newFieldConfigMap[fieldName] = {
698
- ...originalFieldConfig,
699
- args: newArgumentConfigMap,
700
- };
701
- }
702
- if (isObjectType(originalType)) {
703
- newTypeMap[typeName] = new GraphQLObjectType({
704
- ...config,
705
- fields: newFieldConfigMap,
706
- });
707
- }
708
- else if (isInterfaceType(originalType)) {
709
- newTypeMap[typeName] = new GraphQLInterfaceType({
710
- ...config,
711
- fields: newFieldConfigMap,
712
- });
713
- }
714
- else {
715
- newTypeMap[typeName] = new GraphQLInputObjectType({
716
- ...config,
717
- fields: newFieldConfigMap,
718
- });
719
- }
720
- }
721
- }
722
- return newTypeMap;
723
- }
724
- function mapDirectives(originalDirectives, schema, schemaMapper) {
725
- const directiveMapper = getDirectiveMapper(schemaMapper);
726
- if (directiveMapper == null) {
727
- return originalDirectives.slice();
728
- }
729
- const newDirectives = [];
730
- for (const directive of originalDirectives) {
731
- const mappedDirective = directiveMapper(directive, schema);
732
- if (mappedDirective === undefined) {
733
- newDirectives.push(directive);
734
- }
735
- else if (mappedDirective !== null) {
736
- newDirectives.push(mappedDirective);
737
- }
738
- }
739
- return newDirectives;
740
- }
741
- function getTypeSpecifiers(schema, typeName) {
742
- var _a, _b, _c;
743
- const type = schema.getType(typeName);
744
- const specifiers = [MapperKind.TYPE];
745
- if (isObjectType(type)) {
746
- specifiers.push(MapperKind.COMPOSITE_TYPE, MapperKind.OBJECT_TYPE);
747
- if (typeName === ((_a = schema.getQueryType()) === null || _a === void 0 ? void 0 : _a.name)) {
748
- specifiers.push(MapperKind.ROOT_OBJECT, MapperKind.QUERY);
749
- }
750
- else if (typeName === ((_b = schema.getMutationType()) === null || _b === void 0 ? void 0 : _b.name)) {
751
- specifiers.push(MapperKind.ROOT_OBJECT, MapperKind.MUTATION);
752
- }
753
- else if (typeName === ((_c = schema.getSubscriptionType()) === null || _c === void 0 ? void 0 : _c.name)) {
754
- specifiers.push(MapperKind.ROOT_OBJECT, MapperKind.SUBSCRIPTION);
755
- }
756
- }
757
- else if (isInputObjectType(type)) {
758
- specifiers.push(MapperKind.INPUT_OBJECT_TYPE);
759
- }
760
- else if (isInterfaceType(type)) {
761
- specifiers.push(MapperKind.COMPOSITE_TYPE, MapperKind.ABSTRACT_TYPE, MapperKind.INTERFACE_TYPE);
762
- }
763
- else if (isUnionType(type)) {
764
- specifiers.push(MapperKind.COMPOSITE_TYPE, MapperKind.ABSTRACT_TYPE, MapperKind.UNION_TYPE);
765
- }
766
- else if (isEnumType(type)) {
767
- specifiers.push(MapperKind.ENUM_TYPE);
768
- }
769
- else if (isScalarType(type)) {
770
- specifiers.push(MapperKind.SCALAR_TYPE);
771
- }
772
- return specifiers;
773
- }
774
- function getTypeMapper(schema, schemaMapper, typeName) {
775
- const specifiers = getTypeSpecifiers(schema, typeName);
776
- let typeMapper;
777
- const stack = [...specifiers];
778
- while (!typeMapper && stack.length > 0) {
779
- // It is safe to use the ! operator here as we check the length.
780
- const next = stack.pop();
781
- typeMapper = schemaMapper[next];
782
- }
783
- return typeMapper != null ? typeMapper : null;
784
- }
785
- function getFieldSpecifiers(schema, typeName) {
786
- var _a, _b, _c;
787
- const type = schema.getType(typeName);
788
- const specifiers = [MapperKind.FIELD];
789
- if (isObjectType(type)) {
790
- specifiers.push(MapperKind.COMPOSITE_FIELD, MapperKind.OBJECT_FIELD);
791
- if (typeName === ((_a = schema.getQueryType()) === null || _a === void 0 ? void 0 : _a.name)) {
792
- specifiers.push(MapperKind.ROOT_FIELD, MapperKind.QUERY_ROOT_FIELD);
793
- }
794
- else if (typeName === ((_b = schema.getMutationType()) === null || _b === void 0 ? void 0 : _b.name)) {
795
- specifiers.push(MapperKind.ROOT_FIELD, MapperKind.MUTATION_ROOT_FIELD);
796
- }
797
- else if (typeName === ((_c = schema.getSubscriptionType()) === null || _c === void 0 ? void 0 : _c.name)) {
798
- specifiers.push(MapperKind.ROOT_FIELD, MapperKind.SUBSCRIPTION_ROOT_FIELD);
799
- }
800
- }
801
- else if (isInterfaceType(type)) {
802
- specifiers.push(MapperKind.COMPOSITE_FIELD, MapperKind.INTERFACE_FIELD);
803
- }
804
- else if (isInputObjectType(type)) {
805
- specifiers.push(MapperKind.INPUT_OBJECT_FIELD);
806
- }
807
- return specifiers;
808
- }
809
- function getFieldMapper(schema, schemaMapper, typeName) {
810
- const specifiers = getFieldSpecifiers(schema, typeName);
811
- let fieldMapper;
812
- const stack = [...specifiers];
813
- while (!fieldMapper && stack.length > 0) {
814
- // It is safe to use the ! operator here as we check the length.
815
- const next = stack.pop();
816
- // TODO: fix this as unknown cast
817
- fieldMapper = schemaMapper[next];
818
- }
819
- return fieldMapper !== null && fieldMapper !== void 0 ? fieldMapper : null;
820
- }
821
- function getArgumentMapper(schemaMapper) {
822
- const argumentMapper = schemaMapper[MapperKind.ARGUMENT];
823
- return argumentMapper != null ? argumentMapper : null;
824
- }
825
- function getDirectiveMapper(schemaMapper) {
826
- const directiveMapper = schemaMapper[MapperKind.DIRECTIVE];
827
- return directiveMapper != null ? directiveMapper : null;
828
- }
829
- function getEnumValueMapper(schemaMapper) {
830
- const enumValueMapper = schemaMapper[MapperKind.ENUM_VALUE];
831
- return enumValueMapper != null ? enumValueMapper : null;
832
- }
833
- function correctASTNodes(type) {
834
- if (isObjectType(type)) {
835
- const config = type.toConfig();
836
- if (config.astNode != null) {
837
- const fields = [];
838
- for (const fieldName in config.fields) {
839
- const fieldConfig = config.fields[fieldName];
840
- if (fieldConfig.astNode != null) {
841
- fields.push(fieldConfig.astNode);
842
- }
843
- }
844
- config.astNode = {
845
- ...config.astNode,
846
- kind: Kind.OBJECT_TYPE_DEFINITION,
847
- fields,
848
- };
849
- }
850
- if (config.extensionASTNodes != null) {
851
- config.extensionASTNodes = config.extensionASTNodes.map(node => ({
852
- ...node,
853
- kind: Kind.OBJECT_TYPE_EXTENSION,
854
- fields: undefined,
855
- }));
856
- }
857
- return new GraphQLObjectType(config);
858
- }
859
- else if (isInterfaceType(type)) {
860
- const config = type.toConfig();
861
- if (config.astNode != null) {
862
- const fields = [];
863
- for (const fieldName in config.fields) {
864
- const fieldConfig = config.fields[fieldName];
865
- if (fieldConfig.astNode != null) {
866
- fields.push(fieldConfig.astNode);
867
- }
868
- }
869
- config.astNode = {
870
- ...config.astNode,
871
- kind: Kind.INTERFACE_TYPE_DEFINITION,
872
- fields,
873
- };
874
- }
875
- if (config.extensionASTNodes != null) {
876
- config.extensionASTNodes = config.extensionASTNodes.map(node => ({
877
- ...node,
878
- kind: Kind.INTERFACE_TYPE_EXTENSION,
879
- fields: undefined,
880
- }));
881
- }
882
- return new GraphQLInterfaceType(config);
883
- }
884
- else if (isInputObjectType(type)) {
885
- const config = type.toConfig();
886
- if (config.astNode != null) {
887
- const fields = [];
888
- for (const fieldName in config.fields) {
889
- const fieldConfig = config.fields[fieldName];
890
- if (fieldConfig.astNode != null) {
891
- fields.push(fieldConfig.astNode);
892
- }
893
- }
894
- config.astNode = {
895
- ...config.astNode,
896
- kind: Kind.INPUT_OBJECT_TYPE_DEFINITION,
897
- fields,
898
- };
899
- }
900
- if (config.extensionASTNodes != null) {
901
- config.extensionASTNodes = config.extensionASTNodes.map(node => ({
902
- ...node,
903
- kind: Kind.INPUT_OBJECT_TYPE_EXTENSION,
904
- fields: undefined,
905
- }));
906
- }
907
- return new GraphQLInputObjectType(config);
908
- }
909
- else if (isEnumType(type)) {
910
- const config = type.toConfig();
911
- if (config.astNode != null) {
912
- const values = [];
913
- for (const enumKey in config.values) {
914
- const enumValueConfig = config.values[enumKey];
915
- if (enumValueConfig.astNode != null) {
916
- values.push(enumValueConfig.astNode);
917
- }
918
- }
919
- config.astNode = {
920
- ...config.astNode,
921
- values,
922
- };
923
- }
924
- if (config.extensionASTNodes != null) {
925
- config.extensionASTNodes = config.extensionASTNodes.map(node => ({
926
- ...node,
927
- values: undefined,
928
- }));
929
- }
930
- return new GraphQLEnumType(config);
931
- }
932
- else {
933
- return type;
934
- }
935
- }
936
-
937
- function isDocumentNode(object) {
938
- return object && typeof object === 'object' && 'kind' in object && object.kind === Kind.DOCUMENT;
939
- }
1
+ import { getOperationAST, GraphQLError, print } from 'graphql';
2
+ import { envelop, useSchema, isAsyncIterable } from '@envelop/core';
3
+ import { mapSchema, isDocumentNode } from '@graphql-tools/utils';
940
4
 
941
5
  // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
942
6
  function createSpiedPlugin() {