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