@graphql-tools/load 7.3.0 → 7.3.1

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.mjs DELETED
@@ -1,859 +0,0 @@
1
- import { __values, __read, __awaiter, __generator, __spreadArray, __assign } from 'tslib';
2
- import { asArray, AggregateError, isDocumentString, parseGraphQLSDL, getDocumentNodeFromSchema, printSchemaWithDirectives, printWithComments, resetComments, compareStrings } from '@graphql-tools/utils/es5';
3
- import { cwd, env } from 'process';
4
- import { isSchema, Kind, lexicographicSortSchema, Source, print } from 'graphql';
5
- import pLimit from 'p-limit';
6
- import { createRequire } from 'module';
7
- import { join } from 'path';
8
- import { mergeSchemas } from '@graphql-tools/schema/es5';
9
-
10
- function normalizePointers(unnormalizedPointerOrPointers) {
11
- var e_1, _a, e_2, _b;
12
- var ignore = [];
13
- var pointerOptionMap = {};
14
- var handlePointer = function (rawPointer, options) {
15
- if (options === void 0) { options = {}; }
16
- if (rawPointer.startsWith('!')) {
17
- ignore.push(rawPointer.replace('!', ''));
18
- }
19
- else {
20
- pointerOptionMap[rawPointer] = options;
21
- }
22
- };
23
- try {
24
- for (var _c = __values(asArray(unnormalizedPointerOrPointers)), _d = _c.next(); !_d.done; _d = _c.next()) {
25
- var rawPointer = _d.value;
26
- if (typeof rawPointer === 'string') {
27
- handlePointer(rawPointer);
28
- }
29
- else if (typeof rawPointer === 'object') {
30
- try {
31
- for (var _e = (e_2 = void 0, __values(Object.entries(rawPointer))), _f = _e.next(); !_f.done; _f = _e.next()) {
32
- var _g = __read(_f.value, 2), path = _g[0], options = _g[1];
33
- handlePointer(path, options);
34
- }
35
- }
36
- catch (e_2_1) { e_2 = { error: e_2_1 }; }
37
- finally {
38
- try {
39
- if (_f && !_f.done && (_b = _e.return)) _b.call(_e);
40
- }
41
- finally { if (e_2) throw e_2.error; }
42
- }
43
- }
44
- else {
45
- throw new Error("Invalid pointer '" + rawPointer + "'.");
46
- }
47
- }
48
- }
49
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
50
- finally {
51
- try {
52
- if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
53
- }
54
- finally { if (e_1) throw e_1.error; }
55
- }
56
- return { ignore: ignore, pointerOptionMap: pointerOptionMap };
57
- }
58
-
59
- function applyDefaultOptions(options) {
60
- options.cache = options.cache || {};
61
- options.cwd = options.cwd || cwd();
62
- options.sort = 'sort' in options ? options.sort : true;
63
- }
64
-
65
- function loadFile(pointer, options) {
66
- var _a;
67
- return __awaiter(this, void 0, void 0, function () {
68
- var results, errors_1;
69
- var _this = this;
70
- return __generator(this, function (_b) {
71
- switch (_b.label) {
72
- case 0:
73
- results = (_a = options.cache) === null || _a === void 0 ? void 0 : _a[pointer];
74
- if (!!results) return [3 /*break*/, 2];
75
- results = [];
76
- errors_1 = [];
77
- return [4 /*yield*/, Promise.all(options.loaders.map(function (loader) { return __awaiter(_this, void 0, void 0, function () {
78
- var loaderResults, error_1, _a, _b, errorElement;
79
- var e_1, _c;
80
- return __generator(this, function (_d) {
81
- switch (_d.label) {
82
- case 0:
83
- _d.trys.push([0, 2, , 3]);
84
- return [4 /*yield*/, loader.load(pointer, options)];
85
- case 1:
86
- loaderResults = _d.sent();
87
- loaderResults === null || loaderResults === void 0 ? void 0 : loaderResults.forEach(function (result) { return results.push(result); });
88
- return [3 /*break*/, 3];
89
- case 2:
90
- error_1 = _d.sent();
91
- if (env['DEBUG']) {
92
- console.error(error_1);
93
- }
94
- if (error_1 instanceof AggregateError) {
95
- try {
96
- for (_a = __values(error_1.errors), _b = _a.next(); !_b.done; _b = _a.next()) {
97
- errorElement = _b.value;
98
- errors_1.push(errorElement);
99
- }
100
- }
101
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
102
- finally {
103
- try {
104
- if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
105
- }
106
- finally { if (e_1) throw e_1.error; }
107
- }
108
- }
109
- else {
110
- errors_1.push(error_1);
111
- }
112
- return [3 /*break*/, 3];
113
- case 3: return [2 /*return*/];
114
- }
115
- });
116
- }); }))];
117
- case 1:
118
- _b.sent();
119
- if (results.length === 0 && errors_1.length > 0) {
120
- if (errors_1.length === 1) {
121
- throw errors_1[0];
122
- }
123
- throw new AggregateError(errors_1, "Failed to find any GraphQL type definitions in: " + pointer + ";\n - " + errors_1
124
- .map(function (error) { return error.message; })
125
- .join('\n - '));
126
- }
127
- if (options.cache) {
128
- options.cache[pointer] = results;
129
- }
130
- _b.label = 2;
131
- case 2: return [2 /*return*/, results];
132
- }
133
- });
134
- });
135
- }
136
- function loadFileSync(pointer, options) {
137
- var e_2, _a, e_3, _b;
138
- var _c;
139
- var results = (_c = options.cache) === null || _c === void 0 ? void 0 : _c[pointer];
140
- if (!results) {
141
- results = [];
142
- var errors = [];
143
- try {
144
- for (var _d = __values(options.loaders), _e = _d.next(); !_e.done; _e = _d.next()) {
145
- var loader = _e.value;
146
- try {
147
- // We check for the existence so it is okay to force non null
148
- var loaderResults = loader.loadSync(pointer, options);
149
- loaderResults === null || loaderResults === void 0 ? void 0 : loaderResults.forEach(function (result) { return results.push(result); });
150
- }
151
- catch (error) {
152
- if (env['DEBUG']) {
153
- console.error(error);
154
- }
155
- if (error instanceof AggregateError) {
156
- try {
157
- for (var _f = (e_3 = void 0, __values(error.errors)), _g = _f.next(); !_g.done; _g = _f.next()) {
158
- var errorElement = _g.value;
159
- errors.push(errorElement);
160
- }
161
- }
162
- catch (e_3_1) { e_3 = { error: e_3_1 }; }
163
- finally {
164
- try {
165
- if (_g && !_g.done && (_b = _f.return)) _b.call(_f);
166
- }
167
- finally { if (e_3) throw e_3.error; }
168
- }
169
- }
170
- else {
171
- errors.push(error);
172
- }
173
- }
174
- }
175
- }
176
- catch (e_2_1) { e_2 = { error: e_2_1 }; }
177
- finally {
178
- try {
179
- if (_e && !_e.done && (_a = _d.return)) _a.call(_d);
180
- }
181
- finally { if (e_2) throw e_2.error; }
182
- }
183
- if (results.length === 0 && errors.length > 0) {
184
- if (errors.length === 1) {
185
- throw errors[0];
186
- }
187
- throw new AggregateError(errors, "Failed to find any GraphQL type definitions in: " + pointer + ";\n - " + errors
188
- .map(function (error) { return error.message; })
189
- .join('\n - '));
190
- }
191
- if (options.cache) {
192
- options.cache[pointer] = results;
193
- }
194
- }
195
- return results;
196
- }
197
-
198
- /**
199
- * Converts a string to 32bit integer
200
- */
201
- function stringToHash(str) {
202
- var hash = 0;
203
- if (str.length === 0) {
204
- return hash;
205
- }
206
- var char;
207
- for (var i = 0; i < str.length; i++) {
208
- char = str.charCodeAt(i);
209
- // tslint:disable-next-line: no-bitwise
210
- hash = (hash << 5) - hash + char;
211
- // tslint:disable-next-line: no-bitwise
212
- hash = hash & hash;
213
- }
214
- return hash;
215
- }
216
- function useStack() {
217
- var fns = [];
218
- for (var _i = 0; _i < arguments.length; _i++) {
219
- fns[_i] = arguments[_i];
220
- }
221
- return function (input) {
222
- function createNext(i) {
223
- if (i >= fns.length) {
224
- return function () { };
225
- }
226
- return function next() {
227
- fns[i](input, createNext(i + 1));
228
- };
229
- }
230
- fns[0](input, createNext(1));
231
- };
232
- }
233
- function useLimit(concurrency) {
234
- return pLimit(concurrency);
235
- }
236
-
237
- function getCustomLoaderByPath(path, cwd) {
238
- try {
239
- var requireFn = createRequire(join(cwd, 'noop.js'));
240
- var requiredModule = requireFn(path);
241
- if (requiredModule) {
242
- if (requiredModule.default && typeof requiredModule.default === 'function') {
243
- return requiredModule.default;
244
- }
245
- if (typeof requiredModule === 'function') {
246
- return requiredModule;
247
- }
248
- }
249
- }
250
- catch (e) { }
251
- return null;
252
- }
253
- function useCustomLoader(loaderPointer, cwd) {
254
- return __awaiter(this, void 0, void 0, function () {
255
- var loader;
256
- return __generator(this, function (_a) {
257
- switch (_a.label) {
258
- case 0:
259
- if (!(typeof loaderPointer === 'string')) return [3 /*break*/, 2];
260
- return [4 /*yield*/, getCustomLoaderByPath(loaderPointer, cwd)];
261
- case 1:
262
- loader = _a.sent();
263
- return [3 /*break*/, 3];
264
- case 2:
265
- if (typeof loaderPointer === 'function') {
266
- loader = loaderPointer;
267
- }
268
- _a.label = 3;
269
- case 3:
270
- if (typeof loader !== 'function') {
271
- throw new Error("Failed to load custom loader: " + loaderPointer);
272
- }
273
- return [2 /*return*/, loader];
274
- }
275
- });
276
- });
277
- }
278
- function useCustomLoaderSync(loaderPointer, cwd) {
279
- var loader;
280
- if (typeof loaderPointer === 'string') {
281
- loader = getCustomLoaderByPath(loaderPointer, cwd);
282
- }
283
- else if (typeof loaderPointer === 'function') {
284
- loader = loaderPointer;
285
- }
286
- if (typeof loader !== 'function') {
287
- throw new Error("Failed to load custom loader: " + loaderPointer);
288
- }
289
- return loader;
290
- }
291
-
292
- function useQueue(options) {
293
- var _this = this;
294
- var queue = [];
295
- var limit = (options === null || options === void 0 ? void 0 : options.concurrency) ? pLimit(options.concurrency) : function (fn) { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) {
296
- return [2 /*return*/, fn()];
297
- }); }); };
298
- return {
299
- add: function (fn) {
300
- queue.push(function () { return limit(fn); });
301
- },
302
- runAll: function () {
303
- return Promise.all(queue.map(function (fn) { return fn(); }));
304
- },
305
- };
306
- }
307
- function useSyncQueue() {
308
- var queue = [];
309
- return {
310
- add: function (fn) {
311
- queue.push(fn);
312
- },
313
- runAll: function () {
314
- var e_1, _a;
315
- try {
316
- for (var queue_1 = __values(queue), queue_1_1 = queue_1.next(); !queue_1_1.done; queue_1_1 = queue_1.next()) {
317
- var fn = queue_1_1.value;
318
- fn();
319
- }
320
- }
321
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
322
- finally {
323
- try {
324
- if (queue_1_1 && !queue_1_1.done && (_a = queue_1.return)) _a.call(queue_1);
325
- }
326
- finally { if (e_1) throw e_1.error; }
327
- }
328
- },
329
- };
330
- }
331
-
332
- var CONCURRENCY_LIMIT = 50;
333
- function collectSources(_a) {
334
- var pointerOptionMap = _a.pointerOptionMap, options = _a.options;
335
- return __awaiter(this, void 0, void 0, function () {
336
- var sources, queue, _b, addSource, collect, pointer, pointerOptions;
337
- return __generator(this, function (_c) {
338
- switch (_c.label) {
339
- case 0:
340
- sources = [];
341
- queue = useQueue({ concurrency: CONCURRENCY_LIMIT });
342
- _b = createHelpers({
343
- sources: sources,
344
- stack: [collectDocumentString, collectCustomLoader, collectFallback],
345
- }), addSource = _b.addSource, collect = _b.collect;
346
- for (pointer in pointerOptionMap) {
347
- pointerOptions = pointerOptionMap[pointer];
348
- collect({
349
- pointer: pointer,
350
- pointerOptions: pointerOptions,
351
- pointerOptionMap: pointerOptionMap,
352
- options: options,
353
- addSource: addSource,
354
- queue: queue.add,
355
- });
356
- }
357
- return [4 /*yield*/, queue.runAll()];
358
- case 1:
359
- _c.sent();
360
- return [2 /*return*/, sources];
361
- }
362
- });
363
- });
364
- }
365
- function collectSourcesSync(_a) {
366
- var pointerOptionMap = _a.pointerOptionMap, options = _a.options;
367
- var sources = [];
368
- var queue = useSyncQueue();
369
- var _b = createHelpers({
370
- sources: sources,
371
- stack: [collectDocumentString, collectCustomLoaderSync, collectFallbackSync],
372
- }), addSource = _b.addSource, collect = _b.collect;
373
- for (var pointer in pointerOptionMap) {
374
- var pointerOptions = pointerOptionMap[pointer];
375
- collect({
376
- pointer: pointer,
377
- pointerOptions: pointerOptions,
378
- pointerOptionMap: pointerOptionMap,
379
- options: options,
380
- addSource: addSource,
381
- queue: queue.add,
382
- });
383
- }
384
- queue.runAll();
385
- return sources;
386
- }
387
- function createHelpers(_a) {
388
- var sources = _a.sources, stack = _a.stack;
389
- var addSource = function (_a) {
390
- var source = _a.source;
391
- sources.push(source);
392
- };
393
- var collect = useStack.apply(void 0, __spreadArray([], __read(stack), false));
394
- return {
395
- addSource: addSource,
396
- collect: collect,
397
- };
398
- }
399
- function addResultOfCustomLoader(_a) {
400
- var pointer = _a.pointer, result = _a.result, addSource = _a.addSource;
401
- if (isSchema(result)) {
402
- addSource({
403
- source: {
404
- location: pointer,
405
- schema: result,
406
- document: getDocumentNodeFromSchema(result),
407
- },
408
- pointer: pointer,
409
- noCache: true,
410
- });
411
- }
412
- else if (result.kind && result.kind === Kind.DOCUMENT) {
413
- addSource({
414
- source: {
415
- document: result,
416
- location: pointer,
417
- },
418
- pointer: pointer,
419
- });
420
- }
421
- else if (result.document) {
422
- addSource({
423
- source: __assign({ location: pointer }, result),
424
- pointer: pointer,
425
- });
426
- }
427
- }
428
- function collectDocumentString(_a, next) {
429
- var pointer = _a.pointer, pointerOptions = _a.pointerOptions, options = _a.options, addSource = _a.addSource, queue = _a.queue;
430
- if (isDocumentString(pointer)) {
431
- return queue(function () {
432
- var source = parseGraphQLSDL(stringToHash(pointer) + ".graphql", pointer, __assign(__assign({}, options), pointerOptions));
433
- addSource({
434
- source: source,
435
- pointer: pointer,
436
- });
437
- });
438
- }
439
- next();
440
- }
441
- function collectCustomLoader(_a, next) {
442
- var _this = this;
443
- var pointer = _a.pointer, pointerOptions = _a.pointerOptions, queue = _a.queue, addSource = _a.addSource, options = _a.options, pointerOptionMap = _a.pointerOptionMap;
444
- if (pointerOptions.loader) {
445
- return queue(function () { return __awaiter(_this, void 0, void 0, function () {
446
- var loader, result;
447
- return __generator(this, function (_a) {
448
- switch (_a.label) {
449
- case 0: return [4 /*yield*/, useCustomLoader(pointerOptions.loader, options.cwd)];
450
- case 1:
451
- loader = _a.sent();
452
- return [4 /*yield*/, loader(pointer, __assign(__assign({}, options), pointerOptions), pointerOptionMap)];
453
- case 2:
454
- result = _a.sent();
455
- if (!result) {
456
- return [2 /*return*/];
457
- }
458
- addResultOfCustomLoader({ pointer: pointer, result: result, addSource: addSource });
459
- return [2 /*return*/];
460
- }
461
- });
462
- }); });
463
- }
464
- next();
465
- }
466
- function collectCustomLoaderSync(_a, next) {
467
- var pointer = _a.pointer, pointerOptions = _a.pointerOptions, queue = _a.queue, addSource = _a.addSource, options = _a.options, pointerOptionMap = _a.pointerOptionMap;
468
- if (pointerOptions.loader) {
469
- return queue(function () {
470
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
471
- // @ts-ignore TODO options.cwd is possibly undefined, but it seems like no test covers this path
472
- var loader = useCustomLoaderSync(pointerOptions.loader, options.cwd);
473
- var result = loader(pointer, __assign(__assign({}, options), pointerOptions), pointerOptionMap);
474
- if (result) {
475
- addResultOfCustomLoader({ pointer: pointer, result: result, addSource: addSource });
476
- }
477
- });
478
- }
479
- next();
480
- }
481
- function collectFallback(_a) {
482
- var _this = this;
483
- var queue = _a.queue, pointer = _a.pointer, options = _a.options, pointerOptions = _a.pointerOptions, addSource = _a.addSource;
484
- return queue(function () { return __awaiter(_this, void 0, void 0, function () {
485
- var sources, sources_1, sources_1_1, source;
486
- var e_1, _a;
487
- return __generator(this, function (_b) {
488
- switch (_b.label) {
489
- case 0: return [4 /*yield*/, loadFile(pointer, __assign(__assign({}, options), pointerOptions))];
490
- case 1:
491
- sources = _b.sent();
492
- if (sources) {
493
- try {
494
- for (sources_1 = __values(sources), sources_1_1 = sources_1.next(); !sources_1_1.done; sources_1_1 = sources_1.next()) {
495
- source = sources_1_1.value;
496
- addSource({ source: source, pointer: pointer });
497
- }
498
- }
499
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
500
- finally {
501
- try {
502
- if (sources_1_1 && !sources_1_1.done && (_a = sources_1.return)) _a.call(sources_1);
503
- }
504
- finally { if (e_1) throw e_1.error; }
505
- }
506
- }
507
- return [2 /*return*/];
508
- }
509
- });
510
- }); });
511
- }
512
- function collectFallbackSync(_a) {
513
- var queue = _a.queue, pointer = _a.pointer, options = _a.options, pointerOptions = _a.pointerOptions, addSource = _a.addSource;
514
- return queue(function () {
515
- var e_2, _a;
516
- var sources = loadFileSync(pointer, __assign(__assign({}, options), pointerOptions));
517
- if (sources) {
518
- try {
519
- for (var sources_2 = __values(sources), sources_2_1 = sources_2.next(); !sources_2_1.done; sources_2_1 = sources_2.next()) {
520
- var source = sources_2_1.value;
521
- addSource({ source: source, pointer: pointer });
522
- }
523
- }
524
- catch (e_2_1) { e_2 = { error: e_2_1 }; }
525
- finally {
526
- try {
527
- if (sources_2_1 && !sources_2_1.done && (_a = sources_2.return)) _a.call(sources_2);
528
- }
529
- finally { if (e_2) throw e_2.error; }
530
- }
531
- }
532
- });
533
- }
534
-
535
- /**
536
- * @internal
537
- */
538
- var filterKind = function (content, filterKinds) {
539
- var e_1, _a, e_2, _b;
540
- if (content && content.definitions && content.definitions.length && filterKinds && filterKinds.length > 0) {
541
- var invalidDefinitions = [];
542
- var validDefinitions = [];
543
- try {
544
- for (var _c = __values(content.definitions), _d = _c.next(); !_d.done; _d = _c.next()) {
545
- var definitionNode = _d.value;
546
- if (filterKinds.includes(definitionNode.kind)) {
547
- invalidDefinitions.push(definitionNode);
548
- }
549
- else {
550
- validDefinitions.push(definitionNode);
551
- }
552
- }
553
- }
554
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
555
- finally {
556
- try {
557
- if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
558
- }
559
- finally { if (e_1) throw e_1.error; }
560
- }
561
- if (invalidDefinitions.length > 0) {
562
- if (env['DEBUG']) {
563
- try {
564
- for (var invalidDefinitions_1 = __values(invalidDefinitions), invalidDefinitions_1_1 = invalidDefinitions_1.next(); !invalidDefinitions_1_1.done; invalidDefinitions_1_1 = invalidDefinitions_1.next()) {
565
- var d = invalidDefinitions_1_1.value;
566
- console.log("Filtered document of kind " + d.kind + " due to filter policy (" + filterKinds.join(', ') + ")");
567
- }
568
- }
569
- catch (e_2_1) { e_2 = { error: e_2_1 }; }
570
- finally {
571
- try {
572
- if (invalidDefinitions_1_1 && !invalidDefinitions_1_1.done && (_b = invalidDefinitions_1.return)) _b.call(invalidDefinitions_1);
573
- }
574
- finally { if (e_2) throw e_2.error; }
575
- }
576
- }
577
- }
578
- return {
579
- kind: Kind.DOCUMENT,
580
- definitions: validDefinitions,
581
- };
582
- }
583
- return content;
584
- };
585
-
586
- function parseSource(_a) {
587
- var partialSource = _a.partialSource, options = _a.options, pointerOptionMap = _a.pointerOptionMap, addValidSource = _a.addValidSource;
588
- if (partialSource) {
589
- var input = prepareInput({
590
- source: partialSource,
591
- options: options,
592
- pointerOptionMap: pointerOptionMap,
593
- });
594
- parseSchema(input);
595
- parseRawSDL(input);
596
- if (input.source.document) {
597
- useKindsFilter(input);
598
- useComments(input);
599
- collectValidSources(input, addValidSource);
600
- }
601
- }
602
- }
603
- //
604
- function prepareInput(_a) {
605
- var source = _a.source, options = _a.options, pointerOptionMap = _a.pointerOptionMap;
606
- var specificOptions = __assign({}, options);
607
- if (source.location) {
608
- specificOptions = __assign(__assign({}, specificOptions), pointerOptionMap[source.location]);
609
- }
610
- return { source: __assign({}, source), options: specificOptions };
611
- }
612
- function parseSchema(input) {
613
- if (input.source.schema) {
614
- input.source.rawSDL = printSchemaWithDirectives(input.source.schema, input.options);
615
- }
616
- }
617
- function parseRawSDL(input) {
618
- if (input.source.rawSDL) {
619
- input.source.document = parseGraphQLSDL(input.source.location, input.source.rawSDL, input.options).document;
620
- }
621
- }
622
- function useKindsFilter(input) {
623
- if (input.options.filterKinds) {
624
- input.source.document = filterKind(input.source.document, input.options.filterKinds);
625
- }
626
- }
627
- function useComments(input) {
628
- if (!input.source.rawSDL && input.source.document) {
629
- input.source.rawSDL = printWithComments(input.source.document);
630
- resetComments();
631
- }
632
- }
633
- function collectValidSources(input, addValidSource) {
634
- var _a;
635
- if (((_a = input.source.document) === null || _a === void 0 ? void 0 : _a.definitions) && input.source.document.definitions.length > 0) {
636
- addValidSource(input.source);
637
- }
638
- }
639
-
640
- var CONCURRENCY_LIMIT$1 = 100;
641
- /**
642
- * Asynchronously loads any GraphQL documents (i.e. executable documents like
643
- * operations and fragments as well as type system definitions) from the
644
- * provided pointers.
645
- * @param pointerOrPointers Pointers to the sources to load the documents from
646
- * @param options Additional options
647
- */
648
- function loadTypedefs(pointerOrPointers, options) {
649
- return __awaiter(this, void 0, void 0, function () {
650
- var _a, ignore, pointerOptionMap, sources, validSources, limit;
651
- var _b;
652
- return __generator(this, function (_c) {
653
- switch (_c.label) {
654
- case 0:
655
- _a = normalizePointers(pointerOrPointers), ignore = _a.ignore, pointerOptionMap = _a.pointerOptionMap;
656
- options.ignore = asArray(options.ignore || []);
657
- (_b = options.ignore).push.apply(_b, __spreadArray([], __read(ignore), false));
658
- applyDefaultOptions(options);
659
- return [4 /*yield*/, collectSources({
660
- pointerOptionMap: pointerOptionMap,
661
- options: options,
662
- })];
663
- case 1:
664
- sources = _c.sent();
665
- validSources = [];
666
- limit = useLimit(CONCURRENCY_LIMIT$1);
667
- return [4 /*yield*/, Promise.all(sources.map(function (partialSource) {
668
- return limit(function () {
669
- return parseSource({
670
- partialSource: partialSource,
671
- options: options,
672
- pointerOptionMap: pointerOptionMap,
673
- addValidSource: function (source) {
674
- validSources.push(source);
675
- },
676
- });
677
- });
678
- }))];
679
- case 2:
680
- _c.sent();
681
- return [2 /*return*/, prepareResult({ options: options, pointerOptionMap: pointerOptionMap, validSources: validSources })];
682
- }
683
- });
684
- });
685
- }
686
- /**
687
- * Synchronously loads any GraphQL documents (i.e. executable documents like
688
- * operations and fragments as well as type system definitions) from the
689
- * provided pointers.
690
- * @param pointerOrPointers Pointers to the sources to load the documents from
691
- * @param options Additional options
692
- */
693
- function loadTypedefsSync(pointerOrPointers, options) {
694
- var e_1, _a;
695
- var _b = normalizePointers(pointerOrPointers), ignore = _b.ignore, pointerOptionMap = _b.pointerOptionMap;
696
- options.ignore = asArray(options.ignore || []).concat(ignore);
697
- applyDefaultOptions(options);
698
- var sources = collectSourcesSync({
699
- pointerOptionMap: pointerOptionMap,
700
- options: options,
701
- });
702
- var validSources = [];
703
- try {
704
- for (var sources_1 = __values(sources), sources_1_1 = sources_1.next(); !sources_1_1.done; sources_1_1 = sources_1.next()) {
705
- var partialSource = sources_1_1.value;
706
- parseSource({
707
- partialSource: partialSource,
708
- options: options,
709
- pointerOptionMap: pointerOptionMap,
710
- addValidSource: function (source) {
711
- validSources.push(source);
712
- },
713
- });
714
- }
715
- }
716
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
717
- finally {
718
- try {
719
- if (sources_1_1 && !sources_1_1.done && (_a = sources_1.return)) _a.call(sources_1);
720
- }
721
- finally { if (e_1) throw e_1.error; }
722
- }
723
- return prepareResult({ options: options, pointerOptionMap: pointerOptionMap, validSources: validSources });
724
- }
725
- //
726
- function prepareResult(_a) {
727
- var options = _a.options, pointerOptionMap = _a.pointerOptionMap, validSources = _a.validSources;
728
- var pointerList = Object.keys(pointerOptionMap);
729
- if (pointerList.length > 0 && validSources.length === 0) {
730
- throw new Error("\n Unable to find any GraphQL type definitions for the following pointers:\n " + pointerList.map(function (p) { return "\n - " + p + "\n "; }));
731
- }
732
- return options.sort
733
- ? validSources.sort(function (left, right) { return compareStrings(left.location, right.location); })
734
- : validSources;
735
- }
736
-
737
- /**
738
- * Kinds of AST nodes that are included in executable documents
739
- */
740
- var OPERATION_KINDS = [Kind.OPERATION_DEFINITION, Kind.FRAGMENT_DEFINITION];
741
- /**
742
- * Kinds of AST nodes that are included in type system definition documents
743
- */
744
- var NON_OPERATION_KINDS = Object.keys(Kind)
745
- .reduce(function (prev, v) { return __spreadArray(__spreadArray([], __read(prev), false), [Kind[v]], false); }, [])
746
- .filter(function (v) { return !OPERATION_KINDS.includes(v); });
747
- /**
748
- * Asynchronously loads executable documents (i.e. operations and fragments) from
749
- * the provided pointers. The pointers may be individual files or a glob pattern.
750
- * The files themselves may be `.graphql` files or `.js` and `.ts` (in which
751
- * case they will be parsed using graphql-tag-pluck).
752
- * @param pointerOrPointers Pointers to the files to load the documents from
753
- * @param options Additional options
754
- */
755
- function loadDocuments(pointerOrPointers, options) {
756
- return loadTypedefs(pointerOrPointers, __assign({ noRequire: true, filterKinds: NON_OPERATION_KINDS }, options));
757
- }
758
- /**
759
- * Synchronously loads executable documents (i.e. operations and fragments) from
760
- * the provided pointers. The pointers may be individual files or a glob pattern.
761
- * The files themselves may be `.graphql` files or `.js` and `.ts` (in which
762
- * case they will be parsed using graphql-tag-pluck).
763
- * @param pointerOrPointers Pointers to the files to load the documents from
764
- * @param options Additional options
765
- */
766
- function loadDocumentsSync(pointerOrPointers, options) {
767
- return loadTypedefsSync(pointerOrPointers, __assign({ noRequire: true, filterKinds: NON_OPERATION_KINDS }, options));
768
- }
769
-
770
- /**
771
- * Asynchronously loads a schema from the provided pointers.
772
- * @param schemaPointers Pointers to the sources to load the schema from
773
- * @param options Additional options
774
- */
775
- function loadSchema(schemaPointers, options) {
776
- return __awaiter(this, void 0, void 0, function () {
777
- var sources, _a, schemas, typeDefs, mergeSchemasOptions, schema;
778
- return __generator(this, function (_b) {
779
- switch (_b.label) {
780
- case 0: return [4 /*yield*/, loadTypedefs(schemaPointers, __assign(__assign({}, options), { filterKinds: OPERATION_KINDS }))];
781
- case 1:
782
- sources = _b.sent();
783
- _a = collectSchemasAndTypeDefs(sources), schemas = _a.schemas, typeDefs = _a.typeDefs;
784
- mergeSchemasOptions = __assign(__assign({}, options), { schemas: schemas, typeDefs: typeDefs });
785
- schema = mergeSchemas(mergeSchemasOptions);
786
- if (options === null || options === void 0 ? void 0 : options.includeSources) {
787
- includeSources(schema, sources);
788
- }
789
- return [2 /*return*/, options.sort ? lexicographicSortSchema(schema) : schema];
790
- }
791
- });
792
- });
793
- }
794
- /**
795
- * Synchronously loads a schema from the provided pointers.
796
- * @param schemaPointers Pointers to the sources to load the schema from
797
- * @param options Additional options
798
- */
799
- function loadSchemaSync(schemaPointers, options) {
800
- var sources = loadTypedefsSync(schemaPointers, __assign({ filterKinds: OPERATION_KINDS }, options));
801
- var _a = collectSchemasAndTypeDefs(sources), schemas = _a.schemas, typeDefs = _a.typeDefs;
802
- var schema = mergeSchemas(__assign({ schemas: schemas, typeDefs: typeDefs }, options));
803
- if (options === null || options === void 0 ? void 0 : options.includeSources) {
804
- includeSources(schema, sources);
805
- }
806
- return options.sort ? lexicographicSortSchema(schema) : schema;
807
- }
808
- function includeSources(schema, sources) {
809
- var e_1, _a;
810
- var finalSources = [];
811
- try {
812
- for (var sources_1 = __values(sources), sources_1_1 = sources_1.next(); !sources_1_1.done; sources_1_1 = sources_1.next()) {
813
- var source = sources_1_1.value;
814
- if (source.rawSDL) {
815
- finalSources.push(new Source(source.rawSDL, source.location));
816
- }
817
- else if (source.document) {
818
- finalSources.push(new Source(print(source.document), source.location));
819
- }
820
- }
821
- }
822
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
823
- finally {
824
- try {
825
- if (sources_1_1 && !sources_1_1.done && (_a = sources_1.return)) _a.call(sources_1);
826
- }
827
- finally { if (e_1) throw e_1.error; }
828
- }
829
- schema.extensions = __assign(__assign({}, schema.extensions), { sources: finalSources, extendedSources: sources });
830
- }
831
- function collectSchemasAndTypeDefs(sources) {
832
- var e_2, _a;
833
- var schemas = [];
834
- var typeDefs = [];
835
- try {
836
- for (var sources_2 = __values(sources), sources_2_1 = sources_2.next(); !sources_2_1.done; sources_2_1 = sources_2.next()) {
837
- var source = sources_2_1.value;
838
- if (source.schema) {
839
- schemas.push(source.schema);
840
- }
841
- else if (source.document) {
842
- typeDefs.push(source.document);
843
- }
844
- }
845
- }
846
- catch (e_2_1) { e_2 = { error: e_2_1 }; }
847
- finally {
848
- try {
849
- if (sources_2_1 && !sources_2_1.done && (_a = sources_2.return)) _a.call(sources_2);
850
- }
851
- finally { if (e_2) throw e_2.error; }
852
- }
853
- return {
854
- schemas: schemas,
855
- typeDefs: typeDefs,
856
- };
857
- }
858
-
859
- export { NON_OPERATION_KINDS, OPERATION_KINDS, filterKind, loadDocuments, loadDocumentsSync, loadSchema, loadSchemaSync, loadTypedefs, loadTypedefsSync };