1 | import { normalize, isAbsolute, relative, dirname } from 'path';
|
2 | import minimatch from 'minimatch';
|
3 | import { cosmiconfigSync, defaultLoaders, cosmiconfig } from 'cosmiconfig';
|
4 | import loadTs from '@endemolshinegroup/cosmiconfig-typescript-loader';
|
5 | import { loadToml } from 'cosmiconfig-toml-loader';
|
6 | import { env } from 'string-env-interpolation';
|
7 | import { GraphQLFileLoader } from '@graphql-tools/graphql-file-loader';
|
8 | import { UrlLoader } from '@graphql-tools/url-loader';
|
9 | import { JsonFileLoader } from '@graphql-tools/json-file-loader';
|
10 | import { loadTypedefs, loadTypedefsSync, loadDocuments, loadDocumentsSync, loadSchema, OPERATION_KINDS, loadSchemaSync } from '@graphql-tools/load';
|
11 | import { mergeTypeDefs } from '@graphql-tools/merge';
|
12 | import { buildASTSchema, print } from 'graphql';
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 | var extendStatics = function(d, b) {
|
31 | extendStatics = Object.setPrototypeOf ||
|
32 | ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
33 | function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
34 | return extendStatics(d, b);
|
35 | };
|
36 |
|
37 | function __extends(d, b) {
|
38 | if (typeof b !== "function" && b !== null)
|
39 | throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
40 | extendStatics(d, b);
|
41 | function __() { this.constructor = d; }
|
42 | d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
43 | }
|
44 |
|
45 | var __assign = function() {
|
46 | __assign = Object.assign || function __assign(t) {
|
47 | for (var s, i = 1, n = arguments.length; i < n; i++) {
|
48 | s = arguments[i];
|
49 | for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
|
50 | }
|
51 | return t;
|
52 | };
|
53 | return __assign.apply(this, arguments);
|
54 | };
|
55 |
|
56 | function __awaiter(thisArg, _arguments, P, generator) {
|
57 | function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
58 | return new (P || (P = Promise))(function (resolve, reject) {
|
59 | function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
60 | function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
61 | function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
62 | step((generator = generator.apply(thisArg, _arguments || [])).next());
|
63 | });
|
64 | }
|
65 |
|
66 | function __generator(thisArg, body) {
|
67 | var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
68 | return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
69 | function verb(n) { return function (v) { return step([n, v]); }; }
|
70 | function step(op) {
|
71 | if (f) throw new TypeError("Generator is already executing.");
|
72 | while (_) try {
|
73 | if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
74 | if (y = 0, t) op = [op[0] & 2, t.value];
|
75 | switch (op[0]) {
|
76 | case 0: case 1: t = op; break;
|
77 | case 4: _.label++; return { value: op[1], done: false };
|
78 | case 5: _.label++; y = op[1]; op = [0]; continue;
|
79 | case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
80 | default:
|
81 | if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
82 | if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
83 | if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
84 | if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
85 | if (t[2]) _.ops.pop();
|
86 | _.trys.pop(); continue;
|
87 | }
|
88 | op = body.call(thisArg, _);
|
89 | } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
90 | if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
91 | }
|
92 | }
|
93 |
|
94 | function ExtendableBuiltin(cls) {
|
95 | function ExtendableBuiltin() {
|
96 | cls.apply(this, arguments);
|
97 | }
|
98 | ExtendableBuiltin.prototype = Object.create(cls.prototype);
|
99 | Object.setPrototypeOf(ExtendableBuiltin, cls);
|
100 | return ExtendableBuiltin;
|
101 | }
|
102 | function composeMessage() {
|
103 | var lines = [];
|
104 | for (var _i = 0; _i < arguments.length; _i++) {
|
105 | lines[_i] = arguments[_i];
|
106 | }
|
107 | return lines.join('\n');
|
108 | }
|
109 | var ConfigNotFoundError = (function (_super) {
|
110 | __extends(ConfigNotFoundError, _super);
|
111 | function ConfigNotFoundError(message) {
|
112 | var _this = _super.call(this, message) || this;
|
113 | _this.name = _this.constructor.name;
|
114 | _this.message = message;
|
115 | return _this;
|
116 | }
|
117 | return ConfigNotFoundError;
|
118 | }(ExtendableBuiltin(Error)));
|
119 | var ConfigEmptyError = (function (_super) {
|
120 | __extends(ConfigEmptyError, _super);
|
121 | function ConfigEmptyError(message) {
|
122 | var _this = _super.call(this, message) || this;
|
123 | _this.name = _this.constructor.name;
|
124 | _this.message = message;
|
125 | return _this;
|
126 | }
|
127 | return ConfigEmptyError;
|
128 | }(ExtendableBuiltin(Error)));
|
129 | var ConfigInvalidError = (function (_super) {
|
130 | __extends(ConfigInvalidError, _super);
|
131 | function ConfigInvalidError(message) {
|
132 | var _this = _super.call(this, message) || this;
|
133 | _this.name = _this.constructor.name;
|
134 | _this.message = message;
|
135 | return _this;
|
136 | }
|
137 | return ConfigInvalidError;
|
138 | }(ExtendableBuiltin(Error)));
|
139 | var ProjectNotFoundError = (function (_super) {
|
140 | __extends(ProjectNotFoundError, _super);
|
141 | function ProjectNotFoundError(message) {
|
142 | var _this = _super.call(this, message) || this;
|
143 | _this.name = _this.constructor.name;
|
144 | _this.message = message;
|
145 | return _this;
|
146 | }
|
147 | return ProjectNotFoundError;
|
148 | }(ExtendableBuiltin(Error)));
|
149 | var LoadersMissingError = (function (_super) {
|
150 | __extends(LoadersMissingError, _super);
|
151 | function LoadersMissingError(message) {
|
152 | var _this = _super.call(this, message) || this;
|
153 | _this.name = _this.constructor.name;
|
154 | _this.message = message;
|
155 | return _this;
|
156 | }
|
157 | return LoadersMissingError;
|
158 | }(ExtendableBuiltin(Error)));
|
159 | var LoaderNoResultError = (function (_super) {
|
160 | __extends(LoaderNoResultError, _super);
|
161 | function LoaderNoResultError(message) {
|
162 | var _this = _super.call(this, message) || this;
|
163 | _this.name = _this.constructor.name;
|
164 | _this.message = message;
|
165 | return _this;
|
166 | }
|
167 | return LoaderNoResultError;
|
168 | }(ExtendableBuiltin(Error)));
|
169 | var ExtensionMissingError = (function (_super) {
|
170 | __extends(ExtensionMissingError, _super);
|
171 | function ExtensionMissingError(message) {
|
172 | var _this = _super.call(this, message) || this;
|
173 | _this.name = _this.constructor.name;
|
174 | _this.message = message;
|
175 | return _this;
|
176 | }
|
177 | return ExtensionMissingError;
|
178 | }(ExtendableBuiltin(Error)));
|
179 |
|
180 | var legacySearchPlaces = [
|
181 | '.graphqlconfig',
|
182 | '.graphqlconfig.json',
|
183 | '.graphqlconfig.yaml',
|
184 | '.graphqlconfig.yml',
|
185 | ];
|
186 | function isLegacyConfig(filepath) {
|
187 | filepath = filepath.toLowerCase();
|
188 | return legacySearchPlaces.some(function (name) { return filepath.endsWith(name); });
|
189 | }
|
190 | function transformContent(content) {
|
191 | return env(content);
|
192 | }
|
193 | var createCustomLoader = function (loader) {
|
194 | return function (filepath, content) {
|
195 | return loader(filepath, transformContent(content));
|
196 | };
|
197 | };
|
198 | function createCosmiConfig(moduleName, _a) {
|
199 | var legacy = _a.legacy;
|
200 | var options = prepareCosmiconfig(moduleName, {
|
201 | legacy: legacy,
|
202 | });
|
203 | return cosmiconfig(moduleName, options);
|
204 | }
|
205 | function createCosmiConfigSync(moduleName, _a) {
|
206 | var legacy = _a.legacy;
|
207 | var options = prepareCosmiconfig(moduleName, {
|
208 | legacy: legacy,
|
209 | });
|
210 | return cosmiconfigSync(moduleName, options);
|
211 | }
|
212 | function prepareCosmiconfig(moduleName, _a) {
|
213 | var legacy = _a.legacy;
|
214 | var loadYaml = createCustomLoader(defaultLoaders['.yaml']);
|
215 | var loadTomlCustom = createCustomLoader(loadToml);
|
216 | var loadJson = createCustomLoader(defaultLoaders['.json']);
|
217 | var searchPlaces = [
|
218 | "#.config.ts",
|
219 | "#.config.js",
|
220 | '#.config.json',
|
221 | '#.config.yaml',
|
222 | '#.config.yml',
|
223 | '#.config.toml',
|
224 | '.#rc',
|
225 | '.#rc.ts',
|
226 | '.#rc.js',
|
227 | '.#rc.json',
|
228 | '.#rc.yml',
|
229 | '.#rc.yaml',
|
230 | '.#rc.toml',
|
231 | 'package.json',
|
232 | ];
|
233 | if (legacy) {
|
234 | searchPlaces.push.apply(searchPlaces, legacySearchPlaces);
|
235 | }
|
236 |
|
237 |
|
238 | return {
|
239 | searchPlaces: searchPlaces.map(function (place) { return place.replace('#', moduleName); }),
|
240 | loaders: {
|
241 | '.ts': loadTs,
|
242 | '.js': defaultLoaders['.js'],
|
243 | '.json': loadJson,
|
244 | '.yaml': loadYaml,
|
245 | '.yml': loadYaml,
|
246 | '.toml': loadTomlCustom,
|
247 | noExt: loadYaml,
|
248 | },
|
249 | };
|
250 | }
|
251 |
|
252 | var cwd = typeof process !== 'undefined' ? process.cwd() : undefined;
|
253 | function findConfig(_a) {
|
254 | var _b = _a.rootDir, rootDir = _b === void 0 ? cwd : _b, _c = _a.legacy, legacy = _c === void 0 ? true : _c, configName = _a.configName;
|
255 | return __awaiter(this, void 0, void 0, function () {
|
256 | var _d;
|
257 | var _e;
|
258 | return __generator(this, function (_f) {
|
259 | switch (_f.label) {
|
260 | case 0:
|
261 | validate({ rootDir: rootDir });
|
262 | _d = resolve;
|
263 | _e = {
|
264 | rootDir: rootDir
|
265 | };
|
266 | return [4 , createCosmiConfig(configName, { legacy: legacy }).search(rootDir)];
|
267 | case 1: return [2 , _d.apply(void 0, [(_e.result = _f.sent(),
|
268 | _e)])];
|
269 | }
|
270 | });
|
271 | });
|
272 | }
|
273 | function findConfigSync(_a) {
|
274 | var _b = _a.rootDir, rootDir = _b === void 0 ? cwd : _b, _c = _a.legacy, legacy = _c === void 0 ? true : _c, configName = _a.configName;
|
275 | validate({ rootDir: rootDir });
|
276 | return resolve({
|
277 | rootDir: rootDir,
|
278 | result: createCosmiConfigSync(configName, { legacy: legacy }).search(rootDir),
|
279 | });
|
280 | }
|
281 |
|
282 | function validate(_a) {
|
283 | var rootDir = _a.rootDir;
|
284 | if (!rootDir) {
|
285 | throw new Error("Defining a root directory is required");
|
286 | }
|
287 | }
|
288 | function resolve(_a) {
|
289 | var result = _a.result, rootDir = _a.rootDir;
|
290 | if (!result) {
|
291 | throw new ConfigNotFoundError(composeMessage("GraphQL Config file is not available in the provided config directory: " + rootDir, "Please check the config directory."));
|
292 | }
|
293 | if (result.isEmpty) {
|
294 | throw new ConfigEmptyError(composeMessage("GraphQL Config file is empty.", "Please check " + result.filepath));
|
295 | }
|
296 | return {
|
297 | config: result.config,
|
298 | filepath: result.filepath,
|
299 | };
|
300 | }
|
301 |
|
302 | function getConfig(_a) {
|
303 | var filepath = _a.filepath, configName = _a.configName, _b = _a.legacy, legacy = _b === void 0 ? true : _b;
|
304 | return __awaiter(this, void 0, void 0, function () {
|
305 | var _c;
|
306 | var _d;
|
307 | return __generator(this, function (_e) {
|
308 | switch (_e.label) {
|
309 | case 0:
|
310 | validate$1({ filepath: filepath });
|
311 | _c = resolve$1;
|
312 | _d = {};
|
313 | return [4 , createCosmiConfig(configName, { legacy: legacy }).load(filepath)];
|
314 | case 1: return [2 , _c.apply(void 0, [(_d.result = _e.sent(),
|
315 | _d.filepath = filepath,
|
316 | _d)])];
|
317 | }
|
318 | });
|
319 | });
|
320 | }
|
321 | function getConfigSync(_a) {
|
322 | var filepath = _a.filepath, configName = _a.configName, _b = _a.legacy, legacy = _b === void 0 ? true : _b;
|
323 | validate$1({ filepath: filepath });
|
324 | return resolve$1({
|
325 | result: createCosmiConfigSync(configName, { legacy: legacy }).load(filepath),
|
326 | filepath: filepath,
|
327 | });
|
328 | }
|
329 |
|
330 | function resolve$1(_a) {
|
331 | var result = _a.result, filepath = _a.filepath;
|
332 | if (!result) {
|
333 | throw new ConfigNotFoundError(composeMessage("GraphQL Config file is not available: " + filepath, "Please check the config filepath."));
|
334 | }
|
335 | if (result.isEmpty) {
|
336 | throw new ConfigEmptyError(composeMessage("GraphQL Config file is empty.", "Please check " + result.filepath));
|
337 | }
|
338 | return {
|
339 | config: result.config,
|
340 | filepath: result.filepath,
|
341 | };
|
342 | }
|
343 | function validate$1(_a) {
|
344 | var filepath = _a.filepath;
|
345 | if (!filepath) {
|
346 | throw new Error("Defining a file path is required");
|
347 | }
|
348 | }
|
349 |
|
350 | function isMultipleProjectConfig(config) {
|
351 | return typeof config.projects === 'object';
|
352 | }
|
353 | function isSingleProjectConfig(config) {
|
354 | return typeof config.schema !== 'undefined';
|
355 | }
|
356 | function isLegacyProjectConfig(config) {
|
357 | return (typeof config.schemaPath !== 'undefined' ||
|
358 | typeof config.includes !== 'undefined' ||
|
359 | typeof config.excludes !== 'undefined');
|
360 | }
|
361 | function useMiddleware(fns) {
|
362 | return function (input) {
|
363 | if (fns.length) {
|
364 | return fns.reduce(function (obj, cb) { return cb(obj); }, input);
|
365 | }
|
366 | return input;
|
367 | };
|
368 | }
|
369 |
|
370 | var GraphQLProjectConfig = (function () {
|
371 | function GraphQLProjectConfig(_a) {
|
372 | var filepath = _a.filepath, name = _a.name, config = _a.config, extensionsRegistry = _a.extensionsRegistry;
|
373 | this.filepath = filepath;
|
374 | this.dirpath = dirname(filepath);
|
375 | this.name = name;
|
376 | if (isLegacyProjectConfig(config)) {
|
377 | this.extensions = config.extensions || {};
|
378 | this.schema = config.schemaPath;
|
379 | this.include = config.includes;
|
380 | this.exclude = config.excludes;
|
381 | this.isLegacy = true;
|
382 | }
|
383 | else {
|
384 | this.extensions = config.extensions || {};
|
385 | this.schema = config.schema;
|
386 | this.documents = config.documents;
|
387 | this.include = config.include;
|
388 | this.exclude = config.exclude;
|
389 | this.isLegacy = false;
|
390 | }
|
391 | this._extensionsRegistry = extensionsRegistry;
|
392 | }
|
393 | GraphQLProjectConfig.prototype.hasExtension = function (name) {
|
394 | return !!this.extensions[name];
|
395 | };
|
396 | GraphQLProjectConfig.prototype.extension = function (name) {
|
397 | if (this.isLegacy) {
|
398 | var extension_1 = this.extensions[name];
|
399 | if (!extension_1) {
|
400 | throw new ExtensionMissingError("Project " + this.name + " is missing " + name + " extension");
|
401 | }
|
402 | return extension_1;
|
403 | }
|
404 | var extension = this._extensionsRegistry.get(name);
|
405 | if (!extension) {
|
406 | throw new ExtensionMissingError("Project " + this.name + " is missing " + name + " extension");
|
407 | }
|
408 | return __assign(__assign({}, this.extensions[name]), { schema: this.schema, documents: this.documents, include: this.include, exclude: this.exclude });
|
409 | };
|
410 | GraphQLProjectConfig.prototype.getSchema = function (out) {
|
411 | return __awaiter(this, void 0, void 0, function () {
|
412 | return __generator(this, function (_a) {
|
413 | return [2 , this.loadSchema(this.schema, out)];
|
414 | });
|
415 | });
|
416 | };
|
417 | GraphQLProjectConfig.prototype.getSchemaSync = function (out) {
|
418 | return this.loadSchemaSync(this.schema, out);
|
419 | };
|
420 |
|
421 | GraphQLProjectConfig.prototype.getDocuments = function () {
|
422 | return __awaiter(this, void 0, void 0, function () {
|
423 | return __generator(this, function (_a) {
|
424 | if (!this.documents) {
|
425 | return [2 , []];
|
426 | }
|
427 | return [2 , this.loadDocuments(this.documents)];
|
428 | });
|
429 | });
|
430 | };
|
431 | GraphQLProjectConfig.prototype.getDocumentsSync = function () {
|
432 | if (!this.documents) {
|
433 | return [];
|
434 | }
|
435 | return this.loadDocumentsSync(this.documents);
|
436 | };
|
437 | GraphQLProjectConfig.prototype.loadSchema = function (pointer, out, options) {
|
438 | return __awaiter(this, void 0, void 0, function () {
|
439 | return __generator(this, function (_a) {
|
440 | return [2 , this._extensionsRegistry.loaders.schema.loadSchema(pointer, out, options)];
|
441 | });
|
442 | });
|
443 | };
|
444 | GraphQLProjectConfig.prototype.loadSchemaSync = function (pointer, out, options) {
|
445 | return this._extensionsRegistry.loaders.schema.loadSchemaSync(pointer, out, options);
|
446 | };
|
447 |
|
448 | GraphQLProjectConfig.prototype.loadDocuments = function (pointer, options) {
|
449 | return __awaiter(this, void 0, void 0, function () {
|
450 | return __generator(this, function (_a) {
|
451 | if (!pointer) {
|
452 | return [2 , []];
|
453 | }
|
454 | return [2 , this._extensionsRegistry.loaders.documents.loadDocuments(pointer, options)];
|
455 | });
|
456 | });
|
457 | };
|
458 | GraphQLProjectConfig.prototype.loadDocumentsSync = function (pointer, options) {
|
459 | if (!pointer) {
|
460 | return [];
|
461 | }
|
462 | return this._extensionsRegistry.loaders.documents.loadDocumentsSync(pointer, options);
|
463 | };
|
464 |
|
465 | GraphQLProjectConfig.prototype.match = function (filepath) {
|
466 | var _this = this;
|
467 | var isSchemaOrDocument = [this.schema, this.documents].some(function (pointer) {
|
468 | return match(filepath, _this.dirpath, pointer);
|
469 | });
|
470 | if (isSchemaOrDocument) {
|
471 | return true;
|
472 | }
|
473 | var isExcluded = this.exclude
|
474 | ? match(filepath, this.dirpath, this.exclude)
|
475 | : false;
|
476 | if (isExcluded) {
|
477 | return false;
|
478 | }
|
479 | var isIncluded = this.include
|
480 | ? match(filepath, this.dirpath, this.include)
|
481 | : false;
|
482 | if (isIncluded) {
|
483 | return true;
|
484 | }
|
485 | return false;
|
486 | };
|
487 | return GraphQLProjectConfig;
|
488 | }());
|
489 |
|
490 | function match(filepath, dirpath, pointer) {
|
491 | if (!pointer) {
|
492 | return false;
|
493 | }
|
494 | if (Array.isArray(pointer)) {
|
495 | return pointer.some(function (p) { return match(filepath, dirpath, p); });
|
496 | }
|
497 | if (typeof pointer === 'string') {
|
498 | var normalizedFilepath = normalize(isAbsolute(filepath) ? relative(dirpath, filepath) : filepath);
|
499 | return minimatch(normalizedFilepath, normalize(pointer), { dot: true });
|
500 | }
|
501 | if (typeof pointer === 'object') {
|
502 | return match(filepath, dirpath, Object.keys(pointer)[0]);
|
503 | }
|
504 | return false;
|
505 | }
|
506 |
|
507 | var LoadersRegistry = (function () {
|
508 | function LoadersRegistry(_a) {
|
509 | var cwd = _a.cwd;
|
510 | this._loaders = new Set();
|
511 | this._middlewares = [];
|
512 | this.cwd = cwd;
|
513 | }
|
514 | LoadersRegistry.prototype.register = function (loader) {
|
515 | this._loaders.add(loader);
|
516 | };
|
517 | LoadersRegistry.prototype.override = function (loaders) {
|
518 | this._loaders = new Set(loaders);
|
519 | };
|
520 | LoadersRegistry.prototype.use = function (middleware) {
|
521 | this._middlewares.push(middleware);
|
522 | };
|
523 | LoadersRegistry.prototype.loadTypeDefs = function (pointer, options) {
|
524 | return __awaiter(this, void 0, void 0, function () {
|
525 | return __generator(this, function (_a) {
|
526 | return [2 , loadTypedefs(pointer, __assign({ loaders: Array.from(this._loaders), cwd: this.cwd }, options))];
|
527 | });
|
528 | });
|
529 | };
|
530 | LoadersRegistry.prototype.loadTypeDefsSync = function (pointer, options) {
|
531 | return loadTypedefsSync(pointer, this.createOptions(options));
|
532 | };
|
533 | LoadersRegistry.prototype.loadDocuments = function (pointer, options) {
|
534 | return __awaiter(this, void 0, void 0, function () {
|
535 | return __generator(this, function (_a) {
|
536 | return [2 , loadDocuments(pointer, this.createOptions(options))];
|
537 | });
|
538 | });
|
539 | };
|
540 | LoadersRegistry.prototype.loadDocumentsSync = function (pointer, options) {
|
541 | return loadDocumentsSync(pointer, this.createOptions(options));
|
542 | };
|
543 | LoadersRegistry.prototype.loadSchema = function (pointer, out, options) {
|
544 | return __awaiter(this, void 0, void 0, function () {
|
545 | var loadSchemaOptions, schemaDoc, _a;
|
546 | return __generator(this, function (_b) {
|
547 | switch (_b.label) {
|
548 | case 0:
|
549 | out = out || 'GraphQLSchema';
|
550 | loadSchemaOptions = this.createOptions(options);
|
551 | if (out === 'GraphQLSchema' && !this._middlewares.length) {
|
552 | return [2 , loadSchema(pointer, loadSchemaOptions)];
|
553 | }
|
554 | _a = this.transformSchemaSources;
|
555 | return [4 , loadTypedefs(pointer, __assign({ filterKinds: OPERATION_KINDS }, loadSchemaOptions))];
|
556 | case 1:
|
557 | schemaDoc = _a.apply(this, [_b.sent()]);
|
558 |
|
559 | return [2 , this.castSchema(schemaDoc, out)];
|
560 | }
|
561 | });
|
562 | });
|
563 | };
|
564 | LoadersRegistry.prototype.loadSchemaSync = function (pointer, out, options) {
|
565 | out = out || 'GraphQLSchema';
|
566 | var loadSchemaOptions = this.createOptions(options);
|
567 | if (out === 'GraphQLSchema' && !this._middlewares.length) {
|
568 | return loadSchemaSync(pointer, loadSchemaOptions);
|
569 | }
|
570 | var schemaDoc = this.transformSchemaSources(loadTypedefsSync(pointer, __assign({ filterKinds: OPERATION_KINDS }, loadSchemaOptions)));
|
571 | return this.castSchema(schemaDoc, out);
|
572 | };
|
573 | LoadersRegistry.prototype.createOptions = function (options) {
|
574 | return __assign({ loaders: Array.from(this._loaders), cwd: this.cwd }, options);
|
575 | };
|
576 | LoadersRegistry.prototype.transformSchemaSources = function (sources) {
|
577 | var documents = sources.map(function (source) { return source.document; });
|
578 | var document = mergeTypeDefs(documents);
|
579 | return useMiddleware(this._middlewares)(document);
|
580 | };
|
581 | LoadersRegistry.prototype.castSchema = function (doc, out) {
|
582 | if (out === 'DocumentNode') {
|
583 | return doc;
|
584 | }
|
585 | if (out === 'GraphQLSchema') {
|
586 | return buildASTSchema(doc);
|
587 | }
|
588 | return print(doc);
|
589 | };
|
590 | return LoadersRegistry;
|
591 | }());
|
592 |
|
593 | var GraphQLExtensionsRegistry = (function () {
|
594 | function GraphQLExtensionsRegistry(_a) {
|
595 | var cwd = _a.cwd;
|
596 | this._extensions = {};
|
597 | this.loaders = {
|
598 | schema: new LoadersRegistry({ cwd: cwd }),
|
599 | documents: new LoadersRegistry({ cwd: cwd }),
|
600 | };
|
601 |
|
602 | this.loaders.schema.register(new GraphQLFileLoader());
|
603 | this.loaders.schema.register(new UrlLoader());
|
604 | this.loaders.schema.register(new JsonFileLoader());
|
605 |
|
606 | this.loaders.documents.register(new GraphQLFileLoader());
|
607 | }
|
608 | GraphQLExtensionsRegistry.prototype.register = function (extensionFn) {
|
609 | var extension = extensionFn({
|
610 | logger: {},
|
611 | loaders: this.loaders,
|
612 | });
|
613 | this._extensions[extension.name] = extension;
|
614 | };
|
615 | GraphQLExtensionsRegistry.prototype.has = function (extensionName) {
|
616 | return !!this._extensions[extensionName];
|
617 | };
|
618 | GraphQLExtensionsRegistry.prototype.get = function (extensionName) {
|
619 | return this._extensions[extensionName];
|
620 | };
|
621 | GraphQLExtensionsRegistry.prototype.names = function () {
|
622 | return Object.keys(this._extensions);
|
623 | };
|
624 | GraphQLExtensionsRegistry.prototype.forEach = function (cb) {
|
625 | for (var extensionName in this._extensions) {
|
626 | cb(this._extensions[extensionName]);
|
627 | }
|
628 | };
|
629 | return GraphQLExtensionsRegistry;
|
630 | }());
|
631 |
|
632 | var EndpointsExtension = function () {
|
633 | return {
|
634 | name: 'endpoints',
|
635 | };
|
636 | };
|
637 |
|
638 | var cwd$1 = typeof process !== 'undefined' ? process.cwd() : undefined;
|
639 | var defaultConfigName = 'graphql';
|
640 | var defaultLoadConfigOptions = {
|
641 | rootDir: cwd$1,
|
642 | extensions: [],
|
643 | throwOnMissing: true,
|
644 | throwOnEmpty: true,
|
645 | configName: defaultConfigName,
|
646 | legacy: true,
|
647 | };
|
648 | function loadConfig(options) {
|
649 | return __awaiter(this, void 0, void 0, function () {
|
650 | var _a, filepath, configName, rootDir, extensions, throwOnEmpty, throwOnMissing, legacy, found, _b, error_1;
|
651 | return __generator(this, function (_c) {
|
652 | switch (_c.label) {
|
653 | case 0:
|
654 | _a = __assign(__assign({}, defaultLoadConfigOptions), options), filepath = _a.filepath, configName = _a.configName, rootDir = _a.rootDir, extensions = _a.extensions, throwOnEmpty = _a.throwOnEmpty, throwOnMissing = _a.throwOnMissing, legacy = _a.legacy;
|
655 | _c.label = 1;
|
656 | case 1:
|
657 | _c.trys.push([1, 6, , 7]);
|
658 | if (!filepath) return [3 , 3];
|
659 | return [4 , getConfig({
|
660 | filepath: filepath,
|
661 | configName: configName,
|
662 | legacy: legacy,
|
663 | })];
|
664 | case 2:
|
665 | _b = _c.sent();
|
666 | return [3 , 5];
|
667 | case 3: return [4 , findConfig({
|
668 | rootDir: rootDir,
|
669 | configName: configName,
|
670 | legacy: legacy,
|
671 | })];
|
672 | case 4:
|
673 | _b = _c.sent();
|
674 | _c.label = 5;
|
675 | case 5:
|
676 | found = _b;
|
677 | return [2 , new GraphQLConfig(found, extensions)];
|
678 | case 6:
|
679 | error_1 = _c.sent();
|
680 | return [2 , handleError(error_1, { throwOnMissing: throwOnMissing, throwOnEmpty: throwOnEmpty })];
|
681 | case 7: return [2 ];
|
682 | }
|
683 | });
|
684 | });
|
685 | }
|
686 | function loadConfigSync(options) {
|
687 | var _a = __assign(__assign({}, defaultLoadConfigOptions), options), filepath = _a.filepath, configName = _a.configName, rootDir = _a.rootDir, extensions = _a.extensions, throwOnEmpty = _a.throwOnEmpty, throwOnMissing = _a.throwOnMissing, legacy = _a.legacy;
|
688 | try {
|
689 | var found = filepath
|
690 | ? getConfigSync({
|
691 | filepath: filepath,
|
692 | configName: configName,
|
693 | legacy: legacy,
|
694 | })
|
695 | : findConfigSync({
|
696 | rootDir: rootDir,
|
697 | configName: configName,
|
698 | legacy: legacy,
|
699 | });
|
700 | return new GraphQLConfig(found, extensions);
|
701 | }
|
702 | catch (error) {
|
703 | return handleError(error, { throwOnMissing: throwOnMissing, throwOnEmpty: throwOnEmpty });
|
704 | }
|
705 | }
|
706 | function handleError(error, options) {
|
707 | if ((!options.throwOnMissing && error instanceof ConfigNotFoundError) ||
|
708 | (!options.throwOnEmpty && error instanceof ConfigEmptyError)) {
|
709 | return;
|
710 | }
|
711 | throw error;
|
712 | }
|
713 | var GraphQLConfig = (function () {
|
714 | function GraphQLConfig(raw, extensions) {
|
715 | var _this = this;
|
716 | this._rawConfig = raw.config;
|
717 | this.filepath = raw.filepath;
|
718 | this.dirpath = dirname(raw.filepath);
|
719 | this.extensions = new GraphQLExtensionsRegistry({ cwd: this.dirpath });
|
720 |
|
721 | this.extensions.register(EndpointsExtension);
|
722 | extensions.forEach(function (extension) {
|
723 | _this.extensions.register(extension);
|
724 | });
|
725 | this.projects = {};
|
726 | if (isMultipleProjectConfig(this._rawConfig)) {
|
727 | for (var projectName in this._rawConfig.projects) {
|
728 | var config = this._rawConfig.projects[projectName];
|
729 | this.projects[projectName] = new GraphQLProjectConfig({
|
730 | filepath: this.filepath,
|
731 | name: projectName,
|
732 | config: config,
|
733 | extensionsRegistry: this.extensions,
|
734 | });
|
735 | }
|
736 | }
|
737 | else if (isSingleProjectConfig(this._rawConfig)) {
|
738 | this.projects['default'] = new GraphQLProjectConfig({
|
739 | filepath: this.filepath,
|
740 | name: 'default',
|
741 | config: this._rawConfig,
|
742 | extensionsRegistry: this.extensions,
|
743 | });
|
744 | }
|
745 | else if (isLegacyProjectConfig(this._rawConfig)) {
|
746 | this.projects['default'] = new GraphQLProjectConfig({
|
747 | filepath: this.filepath,
|
748 | name: 'default',
|
749 | config: this._rawConfig,
|
750 | extensionsRegistry: this.extensions,
|
751 | });
|
752 | }
|
753 | }
|
754 | GraphQLConfig.prototype.getProject = function (name) {
|
755 | if (!name) {
|
756 | return this.getDefault();
|
757 | }
|
758 | var project = this.projects[name];
|
759 | if (!project) {
|
760 | throw new ProjectNotFoundError("Project '" + name + "' not found");
|
761 | }
|
762 | return project;
|
763 | };
|
764 | GraphQLConfig.prototype.getProjectForFile = function (filepath) {
|
765 |
|
766 | for (var projectName in this.projects) {
|
767 | if (this.projects.hasOwnProperty(projectName)) {
|
768 | var project = this.projects[projectName];
|
769 | if (project.match(filepath)) {
|
770 | return project;
|
771 | }
|
772 | }
|
773 | }
|
774 |
|
775 |
|
776 | for (var projectName in this.projects) {
|
777 | if (this.projects.hasOwnProperty(projectName)) {
|
778 | var project = this.projects[projectName];
|
779 | if (!project.include && !project.exclude) {
|
780 | return project;
|
781 | }
|
782 | }
|
783 | }
|
784 | throw new ProjectNotFoundError("File '" + filepath + "' doesn't match any project");
|
785 | };
|
786 | GraphQLConfig.prototype.getDefault = function () {
|
787 | return this.getProject('default');
|
788 | };
|
789 | GraphQLConfig.prototype.isLegacy = function () {
|
790 | return isLegacyConfig(this.filepath);
|
791 | };
|
792 | return GraphQLConfig;
|
793 | }());
|
794 |
|
795 | export { ConfigEmptyError, ConfigInvalidError, ConfigNotFoundError, ExtensionMissingError, GraphQLConfig, GraphQLProjectConfig, LoaderNoResultError, LoadersMissingError, ProjectNotFoundError, composeMessage, loadConfig, loadConfigSync };
|