UNPKG

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