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