UNPKG

7.63 kBJavaScriptView Raw
1"use strict";
2var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3 return new (P || (P = Promise))(function (resolve, reject) {
4 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6 function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
7 step((generator = generator.apply(thisArg, _arguments || [])).next());
8 });
9};
10var __generator = (this && this.__generator) || function (thisArg, body) {
11 var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
12 return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
13 function verb(n) { return function (v) { return step([n, v]); }; }
14 function step(op) {
15 if (f) throw new TypeError("Generator is already executing.");
16 while (_) try {
17 if (f = 1, y && (t = y[op[0] & 2 ? "return" : op[0] ? "throw" : "next"]) && !(t = t.call(y, op[1])).done) return t;
18 if (y = 0, t) op = [0, t.value];
19 switch (op[0]) {
20 case 0: case 1: t = op; break;
21 case 4: _.label++; return { value: op[1], done: false };
22 case 5: _.label++; y = op[1]; op = [0]; continue;
23 case 7: op = _.ops.pop(); _.trys.pop(); continue;
24 default:
25 if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
26 if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
27 if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
28 if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
29 if (t[2]) _.ops.pop();
30 _.trys.pop(); continue;
31 }
32 op = body.call(thisArg, _);
33 } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
34 if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
35 }
36};
37var __rest = (this && this.__rest) || function (s, e) {
38 var t = {};
39 for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
40 t[p] = s[p];
41 if (s != null && typeof Object.getOwnPropertySymbols === "function")
42 for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) if (e.indexOf(p[i]) < 0)
43 t[p[i]] = s[p[i]];
44 return t;
45};
46Object.defineProperty(exports, "__esModule", { value: true });
47var path_1 = require("path");
48var graphql_1 = require("graphql");
49var utils_1 = require("./utils");
50var extensions_1 = require("./extensions");
51/*
52 * this class can be used for simple usecases where there is no need in per-file API
53 */
54var GraphQLProjectConfig = /** @class */ (function () {
55 function GraphQLProjectConfig(config, configPath, projectName) {
56 utils_1.validateConfig(config);
57 this.config = loadProjectConfig(config, projectName);
58 this.configPath = configPath;
59 this.projectName = projectName;
60 }
61 GraphQLProjectConfig.prototype.resolveConfigPath = function (relativePath) {
62 return path_1.resolve(this.configDir, relativePath);
63 };
64 GraphQLProjectConfig.prototype.includesFile = function (fileUri) {
65 var filePath = fileUri.startsWith('file://') ?
66 fileUri.substr(7) : fileUri;
67 var fullFilePath = filePath.startsWith(this.configDir) ?
68 filePath : path_1.resolve(path_1.join(this.configDir, filePath));
69 var relativePath = path_1.relative(this.configDir, fullFilePath);
70 return ((!this.config.includes ||
71 utils_1.matchesGlobs(relativePath, this.configDir, this.includes)) && !utils_1.matchesGlobs(relativePath, this.configDir, this.excludes));
72 };
73 GraphQLProjectConfig.prototype.getSchema = function () {
74 if (this.schemaPath) {
75 return utils_1.readSchema(this.resolveConfigPath(this.schemaPath));
76 }
77 throw new Error("\"schemaPath\" is required but not provided in " + this.configPath);
78 };
79 GraphQLProjectConfig.prototype.resolveIntrospection = function () {
80 return __awaiter(this, void 0, void 0, function () {
81 return __generator(this, function (_a) {
82 return [2 /*return*/, utils_1.schemaToIntrospection(this.getSchema())];
83 });
84 });
85 };
86 GraphQLProjectConfig.prototype.getSchemaSDL = function () {
87 return graphql_1.printSchema(this.getSchema());
88 };
89 Object.defineProperty(GraphQLProjectConfig.prototype, "configDir", {
90 // Getters
91 get: function () {
92 return path_1.dirname(this.configPath);
93 },
94 enumerable: true,
95 configurable: true
96 });
97 Object.defineProperty(GraphQLProjectConfig.prototype, "schemaPath", {
98 get: function () {
99 return this.config.schemaPath ? this.resolveConfigPath(this.config.schemaPath) : null;
100 },
101 enumerable: true,
102 configurable: true
103 });
104 Object.defineProperty(GraphQLProjectConfig.prototype, "includes", {
105 get: function () {
106 return (this.config.includes || []).map(utils_1.normalizeGlob);
107 },
108 enumerable: true,
109 configurable: true
110 });
111 Object.defineProperty(GraphQLProjectConfig.prototype, "excludes", {
112 get: function () {
113 return (this.config.excludes || []).map(utils_1.normalizeGlob);
114 },
115 enumerable: true,
116 configurable: true
117 });
118 Object.defineProperty(GraphQLProjectConfig.prototype, "extensions", {
119 get: function () {
120 return this.config.extensions || {};
121 },
122 enumerable: true,
123 configurable: true
124 });
125 Object.defineProperty(GraphQLProjectConfig.prototype, "endpointsExtension", {
126 /*
127 extension related helper functions
128 */
129 get: function () {
130 if (!this.extensions.endpoints) {
131 return null;
132 }
133 var endpoints = this.extensions.endpoints;
134 if (typeof endpoints !== 'object' || Array.isArray(endpoints)) {
135 throw new Error(this.configPath + ": \"endpoints\" should be an object");
136 }
137 if (Object.keys(endpoints).length === 0) {
138 return null;
139 }
140 return new extensions_1.GraphQLEndpointsExtension(this.extensions.endpoints, this.configPath);
141 },
142 enumerable: true,
143 configurable: true
144 });
145 return GraphQLProjectConfig;
146}());
147exports.GraphQLProjectConfig = GraphQLProjectConfig;
148function loadProjectConfig(config, projectName) {
149 var projects = config.projects, configBase = __rest(config, ["projects"]);
150 if (projects == null || !Object.keys(projects).length) {
151 return config;
152 }
153 if (!projectName) {
154 throw new Error("Project name must be specified for multiproject config. " +
155 ("Valid project names: " + Object.keys(projects).join(', ')));
156 }
157 var projectConfig = projects[projectName];
158 if (!projectConfig) {
159 throw new Error("\"" + projectName + "\" is not a valid project name. " +
160 ("Valid project names: " + Object.keys(projects).join(', ')));
161 }
162 return utils_1.mergeConfigs(configBase, projectConfig);
163}