UNPKG

6.96 kBJavaScriptView Raw
1import { __assign, __awaiter, __generator } from "tslib";
2import { dirname } from 'path';
3import { GraphQLProjectConfig } from './project-config.js';
4import { isMultipleProjectConfig, isSingleProjectConfig, findConfig, getConfig, getConfigSync, findConfigSync, isLegacyProjectConfig, } from './helpers/index.js';
5import { ProjectNotFoundError, ConfigNotFoundError, ConfigEmptyError } from './errors.js';
6import { GraphQLExtensionsRegistry } from './extension.js';
7import { EndpointsExtension } from './extensions/endpoints.js';
8import { isLegacyConfig } from './helpers/cosmiconfig.js';
9var cwd = typeof process !== 'undefined' ? process.cwd() : undefined;
10var defaultConfigName = 'graphql';
11var defaultLoadConfigOptions = {
12 rootDir: cwd,
13 extensions: [],
14 throwOnMissing: true,
15 throwOnEmpty: true,
16 configName: defaultConfigName,
17 legacy: true,
18};
19export function loadConfig(options) {
20 return __awaiter(this, void 0, void 0, function () {
21 var _a, filepath, configName, rootDir, extensions, throwOnEmpty, throwOnMissing, legacy, found, _b, error_1;
22 return __generator(this, function (_c) {
23 switch (_c.label) {
24 case 0:
25 _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;
26 _c.label = 1;
27 case 1:
28 _c.trys.push([1, 6, , 7]);
29 if (!filepath) return [3 /*break*/, 3];
30 return [4 /*yield*/, getConfig({
31 filepath: filepath,
32 configName: configName,
33 legacy: legacy,
34 })];
35 case 2:
36 _b = _c.sent();
37 return [3 /*break*/, 5];
38 case 3: return [4 /*yield*/, findConfig({
39 rootDir: rootDir,
40 configName: configName,
41 legacy: legacy,
42 })];
43 case 4:
44 _b = _c.sent();
45 _c.label = 5;
46 case 5:
47 found = _b;
48 return [2 /*return*/, new GraphQLConfig(found, extensions)];
49 case 6:
50 error_1 = _c.sent();
51 return [2 /*return*/, handleError(error_1, { throwOnMissing: throwOnMissing, throwOnEmpty: throwOnEmpty })];
52 case 7: return [2 /*return*/];
53 }
54 });
55 });
56}
57export function loadConfigSync(options) {
58 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;
59 try {
60 var found = filepath
61 ? getConfigSync({
62 filepath: filepath,
63 configName: configName,
64 legacy: legacy,
65 })
66 : findConfigSync({
67 rootDir: rootDir,
68 configName: configName,
69 legacy: legacy,
70 });
71 return new GraphQLConfig(found, extensions);
72 }
73 catch (error) {
74 return handleError(error, { throwOnMissing: throwOnMissing, throwOnEmpty: throwOnEmpty });
75 }
76}
77function handleError(error, options) {
78 if ((!options.throwOnMissing && error instanceof ConfigNotFoundError) ||
79 (!options.throwOnEmpty && error instanceof ConfigEmptyError)) {
80 return;
81 }
82 throw error;
83}
84var GraphQLConfig = /** @class */ (function () {
85 function GraphQLConfig(raw, extensions) {
86 // TODO: in v5 change projects to `Object.create(null)` and refactor `graphql-codegen-cli` to remove `projects.hasOwnProperty`
87 // https://github.com/dotansimha/graphql-code-generator/blob/3c6abbde7a20515d9a1d55b4003ef365d248efb5/packages/graphql-codegen-cli/src/graphql-config.ts#L62-L72
88 this.projects = {};
89 this._rawConfig = raw.config;
90 this.filepath = raw.filepath;
91 this.dirpath = dirname(raw.filepath);
92 this.extensions = new GraphQLExtensionsRegistry({ cwd: this.dirpath });
93 // Register Endpoints
94 this.extensions.register(EndpointsExtension);
95 for (var _i = 0, extensions_1 = extensions; _i < extensions_1.length; _i++) {
96 var extension = extensions_1[_i];
97 this.extensions.register(extension);
98 }
99 if (isMultipleProjectConfig(this._rawConfig)) {
100 for (var _a = 0, _b = Object.entries(this._rawConfig.projects); _a < _b.length; _a++) {
101 var _c = _b[_a], projectName = _c[0], config = _c[1];
102 this.projects[projectName] = new GraphQLProjectConfig({
103 filepath: this.filepath,
104 name: projectName,
105 config: config,
106 extensionsRegistry: this.extensions,
107 });
108 }
109 }
110 else if (isSingleProjectConfig(this._rawConfig) || isLegacyProjectConfig(this._rawConfig)) {
111 this.projects.default = new GraphQLProjectConfig({
112 filepath: this.filepath,
113 name: 'default',
114 config: this._rawConfig,
115 extensionsRegistry: this.extensions,
116 });
117 }
118 }
119 GraphQLConfig.prototype.getProject = function (name) {
120 if (!name) {
121 return this.getDefault();
122 }
123 var project = this.projects[name];
124 if (!project) {
125 throw new ProjectNotFoundError("Project '".concat(name, "' not found"));
126 }
127 return project;
128 };
129 GraphQLConfig.prototype.getProjectForFile = function (filepath) {
130 // Looks for a project that includes the file or the file is a part of schema or documents
131 for (var _i = 0, _a = Object.values(this.projects); _i < _a.length; _i++) {
132 var project = _a[_i];
133 if (project.match(filepath)) {
134 return project;
135 }
136 }
137 // The file doesn't match any of the project
138 // Looks for a first project that has no `include` and `exclude`
139 for (var _b = 0, _c = Object.values(this.projects); _b < _c.length; _b++) {
140 var project = _c[_b];
141 if (!project.include && !project.exclude) {
142 return project;
143 }
144 }
145 throw new ProjectNotFoundError("File '".concat(filepath, "' doesn't match any project"));
146 };
147 GraphQLConfig.prototype.getDefault = function () {
148 return this.getProject('default');
149 };
150 GraphQLConfig.prototype.isLegacy = function () {
151 return isLegacyConfig(this.filepath);
152 };
153 return GraphQLConfig;
154}());
155export { GraphQLConfig };