UNPKG

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