UNPKG

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