@graphql-tools/graphql-file-loader 6.2.7-alpha-523f2cce.0 → 6.2.7

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/es5/index.esm.js CHANGED
@@ -1,735 +1,10 @@
1
- import { __awaiter, __generator, __assign, __spread } from 'tslib';
2
- import { printSchemaWithDirectives, createSchemaDefinition, compareNodes, isNotEqual, isValidPath, parseGraphQLSDL } from '@graphql-tools/utils/es5';
1
+ import { __awaiter, __generator } from 'tslib';
2
+ import { isValidPath, parseGraphQLSDL } from '@graphql-tools/utils/es5';
3
3
  import { isAbsolute, resolve } from 'path';
4
4
  import { promises, accessSync, readFileSync } from 'fs';
5
5
  import { cwd } from 'process';
6
- import { Kind, visit, isSchema, parse, Source, getDescription, print, isExecutableDefinitionNode } from 'graphql';
7
6
  import { processImport } from '@graphql-tools/import/es5';
8
7
 
9
- function mergeArguments(args1, args2, config) {
10
- const result = deduplicateArguments([].concat(args2, args1).filter(a => a));
11
- if (config && config.sort) {
12
- result.sort(compareNodes);
13
- }
14
- return result;
15
- }
16
- function deduplicateArguments(args) {
17
- return args.reduce((acc, current) => {
18
- const dup = acc.find(arg => arg.name.value === current.name.value);
19
- if (!dup) {
20
- return acc.concat([current]);
21
- }
22
- return acc;
23
- }, []);
24
- }
25
-
26
- let commentsRegistry = {};
27
- function resetComments() {
28
- commentsRegistry = {};
29
- }
30
- function collectComment(node) {
31
- const entityName = node.name.value;
32
- pushComment(node, entityName);
33
- switch (node.kind) {
34
- case 'EnumTypeDefinition':
35
- node.values.forEach(value => {
36
- pushComment(value, entityName, value.name.value);
37
- });
38
- break;
39
- case 'ObjectTypeDefinition':
40
- case 'InputObjectTypeDefinition':
41
- case 'InterfaceTypeDefinition':
42
- if (node.fields) {
43
- node.fields.forEach((field) => {
44
- pushComment(field, entityName, field.name.value);
45
- if (isFieldDefinitionNode(field) && field.arguments) {
46
- field.arguments.forEach(arg => {
47
- pushComment(arg, entityName, field.name.value, arg.name.value);
48
- });
49
- }
50
- });
51
- }
52
- break;
53
- }
54
- }
55
- function pushComment(node, entity, field, argument) {
56
- const comment = getDescription(node, { commentDescriptions: true });
57
- if (typeof comment !== 'string' || comment.length === 0) {
58
- return;
59
- }
60
- const keys = [entity];
61
- if (field) {
62
- keys.push(field);
63
- if (argument) {
64
- keys.push(argument);
65
- }
66
- }
67
- const path = keys.join('.');
68
- if (!commentsRegistry[path]) {
69
- commentsRegistry[path] = [];
70
- }
71
- commentsRegistry[path].push(comment);
72
- }
73
- function printComment(comment) {
74
- return '\n# ' + comment.replace(/\n/g, '\n# ');
75
- }
76
- /**
77
- * Copyright (c) 2015-present, Facebook, Inc.
78
- *
79
- * This source code is licensed under the MIT license found in the
80
- * LICENSE file in the root directory of this source tree.
81
- */
82
- /**
83
- * NOTE: ==> This file has been modified just to add comments to the printed AST
84
- * This is a temp measure, we will move to using the original non modified printer.js ASAP.
85
- */
86
- // import { visit, VisitFn } from 'graphql/language/visitor';
87
- /**
88
- * Given maybeArray, print an empty string if it is null or empty, otherwise
89
- * print all items together separated by separator if provided
90
- */
91
- function join(maybeArray, separator) {
92
- return maybeArray ? maybeArray.filter(x => x).join(separator || '') : '';
93
- }
94
- function addDescription(cb) {
95
- return (node, _key, _parent, path, ancestors) => {
96
- const keys = [];
97
- const parent = path.reduce((prev, key) => {
98
- if (['fields', 'arguments', 'values'].includes(key)) {
99
- keys.push(prev.name.value);
100
- }
101
- return prev[key];
102
- }, ancestors[0]);
103
- const key = [...keys, parent.name.value].join('.');
104
- const items = [];
105
- if (commentsRegistry[key]) {
106
- items.push(...commentsRegistry[key]);
107
- }
108
- return join([...items.map(printComment), node.description, cb(node)], '\n');
109
- };
110
- }
111
- function indent(maybeString) {
112
- return maybeString && ` ${maybeString.replace(/\n/g, '\n ')}`;
113
- }
114
- /**
115
- * Given array, print each item on its own line, wrapped in an
116
- * indented "{ }" block.
117
- */
118
- function block(array) {
119
- return array && array.length !== 0 ? `{\n${indent(join(array, '\n'))}\n}` : '';
120
- }
121
- /**
122
- * If maybeString is not null or empty, then wrap with start and end, otherwise
123
- * print an empty string.
124
- */
125
- function wrap(start, maybeString, end) {
126
- return maybeString ? start + maybeString + (end || '') : '';
127
- }
128
- /**
129
- * Print a block string in the indented block form by adding a leading and
130
- * trailing blank line. However, if a block string starts with whitespace and is
131
- * a single-line, adding a leading blank line would strip that whitespace.
132
- */
133
- function printBlockString(value, isDescription) {
134
- const escaped = value.replace(/"""/g, '\\"""');
135
- return (value[0] === ' ' || value[0] === '\t') && value.indexOf('\n') === -1
136
- ? `"""${escaped.replace(/"$/, '"\n')}"""`
137
- : `"""\n${isDescription ? escaped : indent(escaped)}\n"""`;
138
- }
139
- /**
140
- * Converts an AST into a string, using one set of reasonable
141
- * formatting rules.
142
- */
143
- function printWithComments(ast) {
144
- return visit(ast, {
145
- leave: {
146
- Name: node => node.value,
147
- Variable: node => `$${node.name}`,
148
- // Document
149
- Document: node => `${node.definitions
150
- .map(defNode => `${defNode}\n${defNode[0] === '#' ? '' : '\n'}`)
151
- .join('')
152
- .trim()}\n`,
153
- OperationTypeDefinition: node => `${node.operation}: ${node.type}`,
154
- VariableDefinition: ({ variable, type, defaultValue }) => `${variable}: ${type}${wrap(' = ', defaultValue)}`,
155
- SelectionSet: ({ selections }) => block(selections),
156
- Field: ({ alias, name, arguments: args, directives, selectionSet }) => join([wrap('', alias, ': ') + name + wrap('(', join(args, ', '), ')'), join(directives, ' '), selectionSet], ' '),
157
- Argument: addDescription(({ name, value }) => `${name}: ${value}`),
158
- // Value
159
- IntValue: ({ value }) => value,
160
- FloatValue: ({ value }) => value,
161
- StringValue: ({ value, block: isBlockString }, key) => isBlockString ? printBlockString(value, key === 'description') : JSON.stringify(value),
162
- BooleanValue: ({ value }) => (value ? 'true' : 'false'),
163
- NullValue: () => 'null',
164
- EnumValue: ({ value }) => value,
165
- ListValue: ({ values }) => `[${join(values, ', ')}]`,
166
- ObjectValue: ({ fields }) => `{${join(fields, ', ')}}`,
167
- ObjectField: ({ name, value }) => `${name}: ${value}`,
168
- // Directive
169
- Directive: ({ name, arguments: args }) => `@${name}${wrap('(', join(args, ', '), ')')}`,
170
- // Type
171
- NamedType: ({ name }) => name,
172
- ListType: ({ type }) => `[${type}]`,
173
- NonNullType: ({ type }) => `${type}!`,
174
- // Type System Definitions
175
- SchemaDefinition: ({ directives, operationTypes }) => join(['schema', join(directives, ' '), block(operationTypes)], ' '),
176
- ScalarTypeDefinition: addDescription(({ name, directives }) => join(['scalar', name, join(directives, ' ')], ' ')),
177
- ObjectTypeDefinition: addDescription(({ name, interfaces, directives, fields }) => join(['type', name, wrap('implements ', join(interfaces, ' & ')), join(directives, ' '), block(fields)], ' ')),
178
- FieldDefinition: addDescription(({ name, arguments: args, type, directives }) => `${name + wrap('(', join(args, ', '), ')')}: ${type}${wrap(' ', join(directives, ' '))}`),
179
- InputValueDefinition: addDescription(({ name, type, defaultValue, directives }) => join([`${name}: ${type}`, wrap('= ', defaultValue), join(directives, ' ')], ' ')),
180
- InterfaceTypeDefinition: addDescription(({ name, directives, fields }) => join(['interface', name, join(directives, ' '), block(fields)], ' ')),
181
- UnionTypeDefinition: addDescription(({ name, directives, types }) => join(['union', name, join(directives, ' '), types && types.length !== 0 ? `= ${join(types, ' | ')}` : ''], ' ')),
182
- EnumTypeDefinition: addDescription(({ name, directives, values }) => join(['enum', name, join(directives, ' '), block(values)], ' ')),
183
- EnumValueDefinition: addDescription(({ name, directives }) => join([name, join(directives, ' ')], ' ')),
184
- InputObjectTypeDefinition: addDescription(({ name, directives, fields }) => join(['input', name, join(directives, ' '), block(fields)], ' ')),
185
- ScalarTypeExtension: ({ name, directives }) => join(['extend scalar', name, join(directives, ' ')], ' '),
186
- ObjectTypeExtension: ({ name, interfaces, directives, fields }) => join(['extend type', name, wrap('implements ', join(interfaces, ' & ')), join(directives, ' '), block(fields)], ' '),
187
- InterfaceTypeExtension: ({ name, directives, fields }) => join(['extend interface', name, join(directives, ' '), block(fields)], ' '),
188
- UnionTypeExtension: ({ name, directives, types }) => join(['extend union', name, join(directives, ' '), types && types.length !== 0 ? `= ${join(types, ' | ')}` : ''], ' '),
189
- EnumTypeExtension: ({ name, directives, values }) => join(['extend enum', name, join(directives, ' '), block(values)], ' '),
190
- InputObjectTypeExtension: ({ name, directives, fields }) => join(['extend input', name, join(directives, ' '), block(fields)], ' '),
191
- DirectiveDefinition: addDescription(({ name, arguments: args, locations }) => `directive @${name}${wrap('(', join(args, ', '), ')')} on ${join(locations, ' | ')}`),
192
- },
193
- });
194
- }
195
- function isFieldDefinitionNode(node) {
196
- return node.kind === 'FieldDefinition';
197
- }
198
-
199
- function directiveAlreadyExists(directivesArr, otherDirective) {
200
- return !!directivesArr.find(directive => directive.name.value === otherDirective.name.value);
201
- }
202
- function nameAlreadyExists(name, namesArr) {
203
- return namesArr.some(({ value }) => value === name.value);
204
- }
205
- function mergeArguments$1(a1, a2) {
206
- const result = [...a2];
207
- for (const argument of a1) {
208
- const existingIndex = result.findIndex(a => a.name.value === argument.name.value);
209
- if (existingIndex > -1) {
210
- const existingArg = result[existingIndex];
211
- if (existingArg.value.kind === 'ListValue') {
212
- const source = existingArg.value.values;
213
- const target = argument.value.values;
214
- // merge values of two lists
215
- existingArg.value.values = deduplicateLists(source, target, (targetVal, source) => {
216
- const value = targetVal.value;
217
- return !value || !source.some((sourceVal) => sourceVal.value === value);
218
- });
219
- }
220
- else {
221
- existingArg.value = argument.value;
222
- }
223
- }
224
- else {
225
- result.push(argument);
226
- }
227
- }
228
- return result;
229
- }
230
- function deduplicateDirectives(directives) {
231
- return directives
232
- .map((directive, i, all) => {
233
- const firstAt = all.findIndex(d => d.name.value === directive.name.value);
234
- if (firstAt !== i) {
235
- const dup = all[firstAt];
236
- directive.arguments = mergeArguments$1(directive.arguments, dup.arguments);
237
- return null;
238
- }
239
- return directive;
240
- })
241
- .filter(d => d);
242
- }
243
- function mergeDirectives(d1 = [], d2 = [], config) {
244
- const reverseOrder = config && config.reverseDirectives;
245
- const asNext = reverseOrder ? d1 : d2;
246
- const asFirst = reverseOrder ? d2 : d1;
247
- const result = deduplicateDirectives([...asNext]);
248
- for (const directive of asFirst) {
249
- if (directiveAlreadyExists(result, directive)) {
250
- const existingDirectiveIndex = result.findIndex(d => d.name.value === directive.name.value);
251
- const existingDirective = result[existingDirectiveIndex];
252
- result[existingDirectiveIndex].arguments = mergeArguments$1(directive.arguments || [], existingDirective.arguments || []);
253
- }
254
- else {
255
- result.push(directive);
256
- }
257
- }
258
- return result;
259
- }
260
- function validateInputs(node, existingNode) {
261
- const printedNode = print(node);
262
- const printedExistingNode = print(existingNode);
263
- // eslint-disable-next-line
264
- const leaveInputs = new RegExp('(directive @w*d*)|( on .*$)', 'g');
265
- const sameArguments = printedNode.replace(leaveInputs, '') === printedExistingNode.replace(leaveInputs, '');
266
- if (!sameArguments) {
267
- throw new Error(`Unable to merge GraphQL directive "${node.name.value}". \nExisting directive: \n\t${printedExistingNode} \nReceived directive: \n\t${printedNode}`);
268
- }
269
- }
270
- function mergeDirective(node, existingNode) {
271
- if (existingNode) {
272
- validateInputs(node, existingNode);
273
- return {
274
- ...node,
275
- locations: [
276
- ...existingNode.locations,
277
- ...node.locations.filter(name => !nameAlreadyExists(name, existingNode.locations)),
278
- ],
279
- };
280
- }
281
- return node;
282
- }
283
- function deduplicateLists(source, target, filterFn) {
284
- return source.concat(target.filter(val => filterFn(val, source)));
285
- }
286
-
287
- function mergeEnumValues(first, second, config) {
288
- const enumValueMap = new Map();
289
- for (const firstValue of first) {
290
- enumValueMap.set(firstValue.name.value, firstValue);
291
- }
292
- for (const secondValue of second) {
293
- const enumValue = secondValue.name.value;
294
- if (enumValueMap.has(enumValue)) {
295
- const firstValue = enumValueMap.get(enumValue);
296
- firstValue.description = secondValue.description || firstValue.description;
297
- firstValue.directives = mergeDirectives(secondValue.directives, firstValue.directives);
298
- }
299
- else {
300
- enumValueMap.set(enumValue, secondValue);
301
- }
302
- }
303
- const result = [...enumValueMap.values()];
304
- if (config && config.sort) {
305
- result.sort(compareNodes);
306
- }
307
- return result;
308
- }
309
-
310
- function mergeEnum(e1, e2, config) {
311
- if (e2) {
312
- return {
313
- name: e1.name,
314
- description: e1['description'] || e2['description'],
315
- kind: (config && config.convertExtensions) || e1.kind === 'EnumTypeDefinition' || e2.kind === 'EnumTypeDefinition'
316
- ? 'EnumTypeDefinition'
317
- : 'EnumTypeExtension',
318
- loc: e1.loc,
319
- directives: mergeDirectives(e1.directives, e2.directives, config),
320
- values: mergeEnumValues(e1.values, e2.values, config),
321
- };
322
- }
323
- return config && config.convertExtensions
324
- ? {
325
- ...e1,
326
- kind: 'EnumTypeDefinition',
327
- }
328
- : e1;
329
- }
330
-
331
- function isStringTypes(types) {
332
- return typeof types === 'string';
333
- }
334
- function isSourceTypes(types) {
335
- return types instanceof Source;
336
- }
337
- function isGraphQLType(definition) {
338
- return definition.kind === 'ObjectTypeDefinition';
339
- }
340
- function isGraphQLTypeExtension(definition) {
341
- return definition.kind === 'ObjectTypeExtension';
342
- }
343
- function isGraphQLEnum(definition) {
344
- return definition.kind === 'EnumTypeDefinition';
345
- }
346
- function isGraphQLEnumExtension(definition) {
347
- return definition.kind === 'EnumTypeExtension';
348
- }
349
- function isGraphQLUnion(definition) {
350
- return definition.kind === 'UnionTypeDefinition';
351
- }
352
- function isGraphQLUnionExtension(definition) {
353
- return definition.kind === 'UnionTypeExtension';
354
- }
355
- function isGraphQLScalar(definition) {
356
- return definition.kind === 'ScalarTypeDefinition';
357
- }
358
- function isGraphQLScalarExtension(definition) {
359
- return definition.kind === 'ScalarTypeExtension';
360
- }
361
- function isGraphQLInputType(definition) {
362
- return definition.kind === 'InputObjectTypeDefinition';
363
- }
364
- function isGraphQLInputTypeExtension(definition) {
365
- return definition.kind === 'InputObjectTypeExtension';
366
- }
367
- function isGraphQLInterface(definition) {
368
- return definition.kind === 'InterfaceTypeDefinition';
369
- }
370
- function isGraphQLInterfaceExtension(definition) {
371
- return definition.kind === 'InterfaceTypeExtension';
372
- }
373
- function isGraphQLDirective(definition) {
374
- return definition.kind === 'DirectiveDefinition';
375
- }
376
- function extractType(type) {
377
- let visitedType = type;
378
- while (visitedType.kind === 'ListType' || visitedType.kind === 'NonNullType') {
379
- visitedType = visitedType.type;
380
- }
381
- return visitedType;
382
- }
383
- function isSchemaDefinition(node) {
384
- return node.kind === 'SchemaDefinition';
385
- }
386
- function isWrappingTypeNode(type) {
387
- return type.kind !== Kind.NAMED_TYPE;
388
- }
389
- function isListTypeNode(type) {
390
- return type.kind === Kind.LIST_TYPE;
391
- }
392
- function isNonNullTypeNode(type) {
393
- return type.kind === Kind.NON_NULL_TYPE;
394
- }
395
- function printTypeNode(type) {
396
- if (isListTypeNode(type)) {
397
- return `[${printTypeNode(type.type)}]`;
398
- }
399
- if (isNonNullTypeNode(type)) {
400
- return `${printTypeNode(type.type)}!`;
401
- }
402
- return type.name.value;
403
- }
404
-
405
- function fieldAlreadyExists(fieldsArr, otherField) {
406
- const result = fieldsArr.find(field => field.name.value === otherField.name.value);
407
- if (result) {
408
- const t1 = extractType(result.type);
409
- const t2 = extractType(otherField.type);
410
- if (t1.name.value !== t2.name.value) {
411
- throw new Error(`Field "${otherField.name.value}" already defined with a different type. Declared as "${t1.name.value}", but you tried to override with "${t2.name.value}"`);
412
- }
413
- }
414
- return !!result;
415
- }
416
- function mergeFields(type, f1, f2, config) {
417
- const result = [...f2];
418
- for (const field of f1) {
419
- if (fieldAlreadyExists(result, field)) {
420
- const existing = result.find((f) => f.name.value === field.name.value);
421
- if (config && config.throwOnConflict) {
422
- preventConflicts(type, existing, field, false);
423
- }
424
- else {
425
- preventConflicts(type, existing, field, true);
426
- }
427
- if (isNonNullTypeNode(field.type) && !isNonNullTypeNode(existing.type)) {
428
- existing.type = field.type;
429
- }
430
- existing.arguments = mergeArguments(field['arguments'] || [], existing.arguments || [], config);
431
- existing.directives = mergeDirectives(field.directives, existing.directives, config);
432
- existing.description = field.description || existing.description;
433
- }
434
- else {
435
- result.push(field);
436
- }
437
- }
438
- if (config && config.sort) {
439
- result.sort(compareNodes);
440
- }
441
- if (config && config.exclusions) {
442
- return result.filter(field => !config.exclusions.includes(`${type.name.value}.${field.name.value}`));
443
- }
444
- return result;
445
- }
446
- function preventConflicts(type, a, b, ignoreNullability = false) {
447
- const aType = printTypeNode(a.type);
448
- const bType = printTypeNode(b.type);
449
- if (isNotEqual(aType, bType)) {
450
- if (safeChangeForFieldType(a.type, b.type, ignoreNullability) === false) {
451
- throw new Error(`Field '${type.name.value}.${a.name.value}' changed type from '${aType}' to '${bType}'`);
452
- }
453
- }
454
- }
455
- function safeChangeForFieldType(oldType, newType, ignoreNullability = false) {
456
- // both are named
457
- if (!isWrappingTypeNode(oldType) && !isWrappingTypeNode(newType)) {
458
- return oldType.toString() === newType.toString();
459
- }
460
- // new is non-null
461
- if (isNonNullTypeNode(newType)) {
462
- const ofType = isNonNullTypeNode(oldType) ? oldType.type : oldType;
463
- return safeChangeForFieldType(ofType, newType.type);
464
- }
465
- // old is non-null
466
- if (isNonNullTypeNode(oldType)) {
467
- return safeChangeForFieldType(newType, oldType, ignoreNullability);
468
- }
469
- // old is list
470
- if (isListTypeNode(oldType)) {
471
- return ((isListTypeNode(newType) && safeChangeForFieldType(oldType.type, newType.type)) ||
472
- (isNonNullTypeNode(newType) && safeChangeForFieldType(oldType, newType['type'])));
473
- }
474
- return false;
475
- }
476
-
477
- function mergeInputType(node, existingNode, config) {
478
- if (existingNode) {
479
- try {
480
- return {
481
- name: node.name,
482
- description: node['description'] || existingNode['description'],
483
- kind: (config && config.convertExtensions) ||
484
- node.kind === 'InputObjectTypeDefinition' ||
485
- existingNode.kind === 'InputObjectTypeDefinition'
486
- ? 'InputObjectTypeDefinition'
487
- : 'InputObjectTypeExtension',
488
- loc: node.loc,
489
- fields: mergeFields(node, node.fields, existingNode.fields, config),
490
- directives: mergeDirectives(node.directives, existingNode.directives, config),
491
- };
492
- }
493
- catch (e) {
494
- throw new Error(`Unable to merge GraphQL input type "${node.name.value}": ${e.message}`);
495
- }
496
- }
497
- return config && config.convertExtensions
498
- ? {
499
- ...node,
500
- kind: 'InputObjectTypeDefinition',
501
- }
502
- : node;
503
- }
504
-
505
- function mergeInterface(node, existingNode, config) {
506
- if (existingNode) {
507
- try {
508
- return {
509
- name: node.name,
510
- description: node['description'] || existingNode['description'],
511
- kind: (config && config.convertExtensions) ||
512
- node.kind === 'InterfaceTypeDefinition' ||
513
- existingNode.kind === 'InterfaceTypeDefinition'
514
- ? 'InterfaceTypeDefinition'
515
- : 'InterfaceTypeExtension',
516
- loc: node.loc,
517
- fields: mergeFields(node, node.fields, existingNode.fields, config),
518
- directives: mergeDirectives(node.directives, existingNode.directives, config),
519
- };
520
- }
521
- catch (e) {
522
- throw new Error(`Unable to merge GraphQL interface "${node.name.value}": ${e.message}`);
523
- }
524
- }
525
- return config && config.convertExtensions
526
- ? {
527
- ...node,
528
- kind: 'InterfaceTypeDefinition',
529
- }
530
- : node;
531
- }
532
-
533
- function alreadyExists(arr, other) {
534
- return !!arr.find(i => i.name.value === other.name.value);
535
- }
536
- function mergeNamedTypeArray(first, second, config) {
537
- const result = [...second, ...first.filter(d => !alreadyExists(second, d))];
538
- if (config && config.sort) {
539
- result.sort(compareNodes);
540
- }
541
- return result;
542
- }
543
-
544
- function mergeType(node, existingNode, config) {
545
- if (existingNode) {
546
- try {
547
- return {
548
- name: node.name,
549
- description: node['description'] || existingNode['description'],
550
- kind: (config && config.convertExtensions) ||
551
- node.kind === 'ObjectTypeDefinition' ||
552
- existingNode.kind === 'ObjectTypeDefinition'
553
- ? 'ObjectTypeDefinition'
554
- : 'ObjectTypeExtension',
555
- loc: node.loc,
556
- fields: mergeFields(node, node.fields, existingNode.fields, config),
557
- directives: mergeDirectives(node.directives, existingNode.directives, config),
558
- interfaces: mergeNamedTypeArray(node.interfaces, existingNode.interfaces, config),
559
- };
560
- }
561
- catch (e) {
562
- throw new Error(`Unable to merge GraphQL type "${node.name.value}": ${e.message}`);
563
- }
564
- }
565
- return config && config.convertExtensions
566
- ? {
567
- ...node,
568
- kind: 'ObjectTypeDefinition',
569
- }
570
- : node;
571
- }
572
-
573
- function mergeScalar(node, existingNode, config) {
574
- if (existingNode) {
575
- return {
576
- name: node.name,
577
- description: node['description'] || existingNode['description'],
578
- kind: (config && config.convertExtensions) ||
579
- node.kind === 'ScalarTypeDefinition' ||
580
- existingNode.kind === 'ScalarTypeDefinition'
581
- ? 'ScalarTypeDefinition'
582
- : 'ScalarTypeExtension',
583
- loc: node.loc,
584
- directives: mergeDirectives(node.directives, existingNode.directives, config),
585
- };
586
- }
587
- return config && config.convertExtensions
588
- ? {
589
- ...node,
590
- kind: 'ScalarTypeDefinition',
591
- }
592
- : node;
593
- }
594
-
595
- function mergeUnion(first, second, config) {
596
- if (second) {
597
- return {
598
- name: first.name,
599
- description: first['description'] || second['description'],
600
- directives: mergeDirectives(first.directives, second.directives, config),
601
- kind: (config && config.convertExtensions) ||
602
- first.kind === 'UnionTypeDefinition' ||
603
- second.kind === 'UnionTypeDefinition'
604
- ? 'UnionTypeDefinition'
605
- : 'UnionTypeExtension',
606
- loc: first.loc,
607
- types: mergeNamedTypeArray(first.types, second.types, config),
608
- };
609
- }
610
- return config && config.convertExtensions
611
- ? {
612
- ...first,
613
- kind: 'UnionTypeDefinition',
614
- }
615
- : first;
616
- }
617
-
618
- function mergeGraphQLNodes(nodes, config) {
619
- return nodes.reduce((prev, nodeDefinition) => {
620
- const node = nodeDefinition;
621
- if (node && node.name && node.name.value) {
622
- const name = node.name.value;
623
- if (config && config.commentDescriptions) {
624
- collectComment(node);
625
- }
626
- if (config &&
627
- config.exclusions &&
628
- (config.exclusions.includes(name + '.*') || config.exclusions.includes(name))) {
629
- delete prev[name];
630
- }
631
- else if (isGraphQLType(nodeDefinition) || isGraphQLTypeExtension(nodeDefinition)) {
632
- prev[name] = mergeType(nodeDefinition, prev[name], config);
633
- }
634
- else if (isGraphQLEnum(nodeDefinition) || isGraphQLEnumExtension(nodeDefinition)) {
635
- prev[name] = mergeEnum(nodeDefinition, prev[name], config);
636
- }
637
- else if (isGraphQLUnion(nodeDefinition) || isGraphQLUnionExtension(nodeDefinition)) {
638
- prev[name] = mergeUnion(nodeDefinition, prev[name], config);
639
- }
640
- else if (isGraphQLScalar(nodeDefinition) || isGraphQLScalarExtension(nodeDefinition)) {
641
- prev[name] = mergeScalar(nodeDefinition, prev[name], config);
642
- }
643
- else if (isGraphQLInputType(nodeDefinition) || isGraphQLInputTypeExtension(nodeDefinition)) {
644
- prev[name] = mergeInputType(nodeDefinition, prev[name], config);
645
- }
646
- else if (isGraphQLInterface(nodeDefinition) || isGraphQLInterfaceExtension(nodeDefinition)) {
647
- prev[name] = mergeInterface(nodeDefinition, prev[name], config);
648
- }
649
- else if (isGraphQLDirective(nodeDefinition)) {
650
- prev[name] = mergeDirective(nodeDefinition, prev[name]);
651
- }
652
- }
653
- return prev;
654
- }, {});
655
- }
656
-
657
- function mergeTypeDefs(types, config) {
658
- resetComments();
659
- const doc = {
660
- kind: Kind.DOCUMENT,
661
- definitions: mergeGraphQLTypes(types, {
662
- useSchemaDefinition: true,
663
- forceSchemaDefinition: false,
664
- throwOnConflict: false,
665
- commentDescriptions: false,
666
- ...config,
667
- }),
668
- };
669
- let result;
670
- if (config && config.commentDescriptions) {
671
- result = printWithComments(doc);
672
- }
673
- else {
674
- result = doc;
675
- }
676
- resetComments();
677
- return result;
678
- }
679
- function mergeGraphQLTypes(types, config) {
680
- resetComments();
681
- const allNodes = types
682
- .map(type => {
683
- if (Array.isArray(type)) {
684
- type = mergeTypeDefs(type);
685
- }
686
- if (isSchema(type)) {
687
- return parse(printSchemaWithDirectives(type));
688
- }
689
- else if (isStringTypes(type) || isSourceTypes(type)) {
690
- return parse(type);
691
- }
692
- return type;
693
- })
694
- .map(ast => ast.definitions)
695
- .reduce((defs, newDef = []) => [...defs, ...newDef], []);
696
- // XXX: right now we don't handle multiple schema definitions
697
- let schemaDef = allNodes.filter(isSchemaDefinition).reduce((def, node) => {
698
- node.operationTypes
699
- .filter(op => op.type.name.value)
700
- .forEach(op => {
701
- def[op.operation] = op.type.name.value;
702
- });
703
- return def;
704
- }, {
705
- query: null,
706
- mutation: null,
707
- subscription: null,
708
- });
709
- const mergedNodes = mergeGraphQLNodes(allNodes, config);
710
- const allTypes = Object.keys(mergedNodes);
711
- if (config && config.sort) {
712
- allTypes.sort(typeof config.sort === 'function' ? config.sort : undefined);
713
- }
714
- if (config && config.useSchemaDefinition) {
715
- const queryType = schemaDef.query ? schemaDef.query : allTypes.find(t => t === 'Query');
716
- const mutationType = schemaDef.mutation ? schemaDef.mutation : allTypes.find(t => t === 'Mutation');
717
- const subscriptionType = schemaDef.subscription ? schemaDef.subscription : allTypes.find(t => t === 'Subscription');
718
- schemaDef = {
719
- query: queryType,
720
- mutation: mutationType,
721
- subscription: subscriptionType,
722
- };
723
- }
724
- const schemaDefinition = createSchemaDefinition(schemaDef, {
725
- force: config.forceSchemaDefinition,
726
- });
727
- if (!schemaDefinition) {
728
- return Object.values(mergedNodes);
729
- }
730
- return [...Object.values(mergedNodes), parse(schemaDefinition).definitions[0]];
731
- }
732
-
733
8
  var readFile = promises.readFile, access = promises.access;
734
9
  var FILE_EXTENSIONS = ['.gql', '.gqls', '.graphql', '.graphqls'];
735
10
  function isGraphQLImportFile(rawSDL) {
@@ -827,17 +102,9 @@ var GraphQLFileLoader = /** @class */ (function () {
827
102
  GraphQLFileLoader.prototype.handleFileContent = function (rawSDL, pointer, options) {
828
103
  if (!options.skipGraphQLImport && isGraphQLImportFile(rawSDL)) {
829
104
  var document_1 = processImport(pointer, options.cwd);
830
- var typeSystemDefinitions = document_1.definitions
831
- .filter(function (d) { return !isExecutableDefinitionNode(d); })
832
- .map(function (definition) { return ({
833
- kind: Kind.DOCUMENT,
834
- definitions: [definition],
835
- }); });
836
- var mergedTypeDefs = mergeTypeDefs(typeSystemDefinitions, { useSchemaDefinition: false });
837
- var executableDefinitions = document_1.definitions.filter(isExecutableDefinitionNode);
838
105
  return {
839
106
  location: pointer,
840
- document: __assign(__assign({}, mergedTypeDefs), { definitions: __spread(mergedTypeDefs.definitions, executableDefinitions) }),
107
+ document: document_1,
841
108
  };
842
109
  }
843
110
  return parseGraphQLSDL(pointer, rawSDL, options);