UNPKG

10.1 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 = 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;
18 if (y = 0, t) op = [op[0] & 2, 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 _this = this;
38Object.defineProperty(exports, "__esModule", { value: true });
39var graphql_1 = require("graphql");
40var errors_1 = require("./errors");
41var introspection_from_url_1 = require("./loaders/schema/introspection-from-url");
42var introspection_from_file_1 = require("./loaders/schema/introspection-from-file");
43var schema_from_string_1 = require("./loaders/schema/schema-from-string");
44var schema_from_typedefs_1 = require("./loaders/schema/schema-from-typedefs");
45var schema_from_export_1 = require("./loaders/schema/schema-from-export");
46var document_from_string_1 = require("./loaders/documents/document-from-string");
47var documents_from_glob_1 = require("./loaders/documents/documents-from-glob");
48function getCustomLoaderByPath(path) {
49 var requiredModule = require(path);
50 if (requiredModule && requiredModule.default && typeof requiredModule.default === 'function') {
51 return requiredModule.default;
52 }
53 if (requiredModule && typeof requiredModule === 'function') {
54 return requiredModule;
55 }
56 return null;
57}
58var documentsHandlers = [new document_from_string_1.DocumentFromString(), new documents_from_glob_1.DocumentsFromGlob()];
59var schemaHandlers = [
60 new introspection_from_url_1.IntrospectionFromUrlLoader(),
61 new introspection_from_file_1.IntrospectionFromFileLoader(),
62 new schema_from_string_1.SchemaFromString(),
63 new schema_from_typedefs_1.SchemaFromTypedefs(),
64 new schema_from_export_1.SchemaFromExport()
65];
66exports.loadSchema = function (schemaDef, config) { return __awaiter(_this, void 0, void 0, function () {
67 var pointToSchema, defObject, loaderString, customSchemaLoader, returnedSchema, e_1, _i, schemaHandlers_1, handler, pointToSchema, options;
68 return __generator(this, function (_a) {
69 switch (_a.label) {
70 case 0:
71 if (!(typeof schemaDef === 'object' &&
72 schemaDef[Object.keys(schemaDef)[0]] &&
73 schemaDef[Object.keys(schemaDef)[0]].loader &&
74 typeof schemaDef[Object.keys(schemaDef)[0]].loader === 'string')) return [3 /*break*/, 6];
75 pointToSchema = Object.keys(schemaDef)[0];
76 defObject = schemaDef[pointToSchema];
77 loaderString = defObject.loader;
78 _a.label = 1;
79 case 1:
80 _a.trys.push([1, 5, , 6]);
81 customSchemaLoader = getCustomLoaderByPath(loaderString);
82 if (!customSchemaLoader) return [3 /*break*/, 3];
83 return [4 /*yield*/, customSchemaLoader(pointToSchema, config, defObject)];
84 case 2:
85 returnedSchema = _a.sent();
86 if (returnedSchema && returnedSchema instanceof graphql_1.GraphQLSchema) {
87 return [2 /*return*/, returnedSchema];
88 }
89 else {
90 throw new Error("Return value of a custom schema loader must be of type \"GraphQLSchema\"!");
91 }
92 return [3 /*break*/, 4];
93 case 3: throw new Error("Unable to find a loader function! Make sure to export a default function from your file");
94 case 4: return [3 /*break*/, 6];
95 case 5:
96 e_1 = _a.sent();
97 throw new errors_1.DetailedError('Failed to load custom schema loader', "\n Failed to load schema from " + pointToSchema + " using loader \"" + loaderString + "\":\n \n " + e_1.message + "\n " + e_1.stack + "\n ", pointToSchema + " using loader \"" + loaderString + "\"");
98 case 6:
99 _i = 0, schemaHandlers_1 = schemaHandlers;
100 _a.label = 7;
101 case 7:
102 if (!(_i < schemaHandlers_1.length)) return [3 /*break*/, 10];
103 handler = schemaHandlers_1[_i];
104 pointToSchema = null;
105 options = {};
106 if (typeof schemaDef === 'string') {
107 pointToSchema = schemaDef;
108 }
109 else if (typeof schemaDef === 'object') {
110 pointToSchema = Object.keys(schemaDef)[0];
111 options = schemaDef[pointToSchema];
112 }
113 return [4 /*yield*/, handler.canHandle(pointToSchema)];
114 case 8:
115 if (_a.sent()) {
116 return [2 /*return*/, handler.handle(pointToSchema, config, options)];
117 }
118 _a.label = 9;
119 case 9:
120 _i++;
121 return [3 /*break*/, 7];
122 case 10: throw new errors_1.DetailedError('Failed to load schema', "\n Failed to load schema from " + schemaDef + ".\n \n GraphQL Code Generator supports:\n - ES Modules and CommonJS exports\n - Introspection JSON File\n - URL of GraphQL endpoint\n - Multiple files with type definitions\n - String in config file\n \n Try to use one of above options and run codegen again.\n \n ");
123 }
124 });
125}); };
126exports.loadDocuments = function (documentDef, config) { return __awaiter(_this, void 0, void 0, function () {
127 var pointToDoc, defObject, loaderString, customDocumentLoader, returned, e_2, _i, documentsHandlers_1, handler;
128 return __generator(this, function (_a) {
129 switch (_a.label) {
130 case 0:
131 if (!(typeof documentDef === 'object' &&
132 documentDef[Object.keys(documentDef)[0]] &&
133 documentDef[Object.keys(documentDef)[0]].loader &&
134 typeof documentDef[Object.keys(documentDef)[0]].loader === 'string')) return [3 /*break*/, 6];
135 pointToDoc = Object.keys(documentDef)[0];
136 defObject = documentDef[pointToDoc];
137 loaderString = defObject.loader;
138 _a.label = 1;
139 case 1:
140 _a.trys.push([1, 5, , 6]);
141 customDocumentLoader = getCustomLoaderByPath(loaderString);
142 if (!customDocumentLoader) return [3 /*break*/, 3];
143 return [4 /*yield*/, customDocumentLoader(pointToDoc, config)];
144 case 2:
145 returned = _a.sent();
146 if (returned && Array.isArray(returned)) {
147 return [2 /*return*/, returned];
148 }
149 else {
150 throw new Error("Return value of a custom schema loader must be an Array of: { filePath: string, content: DocumentNode }");
151 }
152 return [3 /*break*/, 4];
153 case 3: throw new Error("Unable to find a loader function! Make sure to export a default function from your file");
154 case 4: return [3 /*break*/, 6];
155 case 5:
156 e_2 = _a.sent();
157 throw new errors_1.DetailedError('Failed to load custom documents loader', "\n Failed to load documents from " + pointToDoc + " using loader \"" + loaderString + "\":\n \n " + e_2.message + "\n ");
158 case 6:
159 _i = 0, documentsHandlers_1 = documentsHandlers;
160 _a.label = 7;
161 case 7:
162 if (!(_i < documentsHandlers_1.length)) return [3 /*break*/, 10];
163 handler = documentsHandlers_1[_i];
164 return [4 /*yield*/, handler.canHandle(documentDef)];
165 case 8:
166 if (_a.sent()) {
167 return [2 /*return*/, handler.handle(documentDef, config)];
168 }
169 _a.label = 9;
170 case 9:
171 _i++;
172 return [3 /*break*/, 7];
173 case 10: return [2 /*return*/, []];
174 }
175 });
176}); };
177//# sourceMappingURL=load.js.map
\No newline at end of file