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