UNPKG

8.11 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 path = require('path');
10const fs = require('fs');
11const process = require('process');
12const _import = require('@graphql-tools/import/es5');
13const globby = _interopDefault(require('globby'));
14const unixify = _interopDefault(require('unixify'));
15
16var readFile = fs.promises.readFile, access = fs.promises.access;
17var FILE_EXTENSIONS = ['.gql', '.gqls', '.graphql', '.graphqls'];
18function isGraphQLImportFile(rawSDL) {
19 var trimmedRawSDL = rawSDL.trim();
20 return trimmedRawSDL.startsWith('# import') || trimmedRawSDL.startsWith('#import');
21}
22function createGlobbyOptions(options) {
23 return tslib.__assign(tslib.__assign({ absolute: true }, options), { ignore: [] });
24}
25var buildIgnoreGlob = function (path) { return "!" + path; };
26/**
27 * This loader loads documents and type definitions from `.graphql` files.
28 *
29 * You can load a single source:
30 *
31 * ```js
32 * const schema = await loadSchema('schema.graphql', {
33 * loaders: [
34 * new GraphQLFileLoader()
35 * ]
36 * });
37 * ```
38 *
39 * Or provide a glob pattern to load multiple sources:
40 *
41 * ```js
42 * const schema = await loadSchema('graphql/*.graphql', {
43 * loaders: [
44 * new GraphQLFileLoader()
45 * ]
46 * });
47 * ```
48 */
49var GraphQLFileLoader = /** @class */ (function () {
50 function GraphQLFileLoader() {
51 }
52 GraphQLFileLoader.prototype.canLoad = function (pointer, options) {
53 return tslib.__awaiter(this, void 0, void 0, function () {
54 var normalizedFilePath, _a;
55 return tslib.__generator(this, function (_b) {
56 switch (_b.label) {
57 case 0:
58 if (!utils.isValidPath(pointer)) return [3 /*break*/, 4];
59 if (!FILE_EXTENSIONS.find(function (extension) { return pointer.endsWith(extension); })) return [3 /*break*/, 4];
60 normalizedFilePath = path.isAbsolute(pointer) ? pointer : path.resolve(options.cwd || process.cwd(), pointer);
61 _b.label = 1;
62 case 1:
63 _b.trys.push([1, 3, , 4]);
64 return [4 /*yield*/, access(normalizedFilePath)];
65 case 2:
66 _b.sent();
67 return [2 /*return*/, true];
68 case 3:
69 _a = _b.sent();
70 return [2 /*return*/, false];
71 case 4: return [2 /*return*/, false];
72 }
73 });
74 });
75 };
76 GraphQLFileLoader.prototype.canLoadSync = function (pointer, options) {
77 if (utils.isValidPath(pointer)) {
78 if (FILE_EXTENSIONS.find(function (extension) { return pointer.endsWith(extension); })) {
79 var normalizedFilePath = path.isAbsolute(pointer) ? pointer : path.resolve(options.cwd || process.cwd(), pointer);
80 return fs.existsSync(normalizedFilePath);
81 }
82 }
83 return false;
84 };
85 GraphQLFileLoader.prototype._buildGlobs = function (glob, options) {
86 var ignores = utils.asArray(options.ignore || []);
87 var globs = tslib.__spreadArray([unixify(glob)], tslib.__read(ignores.map(function (v) { return buildIgnoreGlob(unixify(v)); })));
88 return globs;
89 };
90 GraphQLFileLoader.prototype.resolveGlobs = function (glob, options) {
91 return tslib.__awaiter(this, void 0, void 0, function () {
92 var globs, result;
93 return tslib.__generator(this, function (_a) {
94 switch (_a.label) {
95 case 0:
96 globs = this._buildGlobs(glob, options);
97 return [4 /*yield*/, globby(globs, createGlobbyOptions(options))];
98 case 1:
99 result = _a.sent();
100 return [2 /*return*/, result];
101 }
102 });
103 });
104 };
105 GraphQLFileLoader.prototype.resolveGlobsSync = function (glob, options) {
106 var globs = this._buildGlobs(glob, options);
107 var result = globby.sync(globs, createGlobbyOptions(options));
108 return result;
109 };
110 GraphQLFileLoader.prototype.load = function (pointer, options) {
111 return tslib.__awaiter(this, void 0, void 0, function () {
112 var resolvedPaths, finalResult;
113 var _this = this;
114 return tslib.__generator(this, function (_a) {
115 switch (_a.label) {
116 case 0: return [4 /*yield*/, this.resolveGlobs(pointer, options)];
117 case 1:
118 resolvedPaths = _a.sent();
119 finalResult = [];
120 return [4 /*yield*/, Promise.all(resolvedPaths.map(function (path$1) { return tslib.__awaiter(_this, void 0, void 0, function () {
121 var normalizedFilePath, rawSDL;
122 return tslib.__generator(this, function (_a) {
123 switch (_a.label) {
124 case 0: return [4 /*yield*/, this.canLoad(path$1, options)];
125 case 1:
126 if (!_a.sent()) return [3 /*break*/, 3];
127 normalizedFilePath = path.isAbsolute(path$1) ? path$1 : path.resolve(options.cwd || process.cwd(), path$1);
128 return [4 /*yield*/, readFile(normalizedFilePath, { encoding: 'utf8' })];
129 case 2:
130 rawSDL = _a.sent();
131 finalResult.push(this.handleFileContent(rawSDL, normalizedFilePath, options));
132 _a.label = 3;
133 case 3: return [2 /*return*/];
134 }
135 });
136 }); }))];
137 case 2:
138 _a.sent();
139 return [2 /*return*/, finalResult];
140 }
141 });
142 });
143 };
144 GraphQLFileLoader.prototype.loadSync = function (pointer, options) {
145 var e_1, _a;
146 var resolvedPaths = this.resolveGlobsSync(pointer, options);
147 var finalResult = [];
148 try {
149 for (var resolvedPaths_1 = tslib.__values(resolvedPaths), resolvedPaths_1_1 = resolvedPaths_1.next(); !resolvedPaths_1_1.done; resolvedPaths_1_1 = resolvedPaths_1.next()) {
150 var path$1 = resolvedPaths_1_1.value;
151 if (this.canLoadSync(path$1, options)) {
152 var normalizedFilePath = path.isAbsolute(path$1) ? path$1 : path.resolve(options.cwd || process.cwd(), path$1);
153 var rawSDL = fs.readFileSync(normalizedFilePath, { encoding: 'utf8' });
154 finalResult.push(this.handleFileContent(rawSDL, normalizedFilePath, options));
155 }
156 }
157 }
158 catch (e_1_1) { e_1 = { error: e_1_1 }; }
159 finally {
160 try {
161 if (resolvedPaths_1_1 && !resolvedPaths_1_1.done && (_a = resolvedPaths_1.return)) _a.call(resolvedPaths_1);
162 }
163 finally { if (e_1) throw e_1.error; }
164 }
165 return finalResult;
166 };
167 GraphQLFileLoader.prototype.handleFileContent = function (rawSDL, pointer, options) {
168 if (!options.skipGraphQLImport && isGraphQLImportFile(rawSDL)) {
169 var document_1 = _import.processImport(pointer, options.cwd);
170 return {
171 location: pointer,
172 document: document_1,
173 };
174 }
175 return utils.parseGraphQLSDL(pointer, rawSDL, options);
176 };
177 return GraphQLFileLoader;
178}());
179
180exports.GraphQLFileLoader = GraphQLFileLoader;