UNPKG

8.55 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_toolkit_1 = require("graphql-toolkit");
40var graphql_1 = require("graphql");
41var errors_1 = require("./errors");
42function getCustomLoaderByPath(path) {
43 var requiredModule = require(path);
44 if (requiredModule && requiredModule.default && typeof requiredModule.default === 'function') {
45 return requiredModule.default;
46 }
47 if (requiredModule && typeof requiredModule === 'function') {
48 return requiredModule;
49 }
50 return null;
51}
52exports.loadSchema = function (schemaDef, config) { return __awaiter(_this, void 0, void 0, function () {
53 var pointToSchema, defObject, loaderString, customSchemaLoader, returnedSchema, e_1, pointToSchema, options;
54 return __generator(this, function (_a) {
55 switch (_a.label) {
56 case 0:
57 if (!(typeof schemaDef === 'object' &&
58 schemaDef[Object.keys(schemaDef)[0]] &&
59 schemaDef[Object.keys(schemaDef)[0]].loader &&
60 typeof schemaDef[Object.keys(schemaDef)[0]].loader === 'string')) return [3 /*break*/, 6];
61 pointToSchema = Object.keys(schemaDef)[0];
62 defObject = schemaDef[pointToSchema];
63 loaderString = defObject.loader;
64 _a.label = 1;
65 case 1:
66 _a.trys.push([1, 5, , 6]);
67 customSchemaLoader = getCustomLoaderByPath(loaderString);
68 if (!customSchemaLoader) return [3 /*break*/, 3];
69 return [4 /*yield*/, customSchemaLoader(pointToSchema, config, defObject)];
70 case 2:
71 returnedSchema = _a.sent();
72 if (returnedSchema && returnedSchema instanceof graphql_1.GraphQLSchema) {
73 return [2 /*return*/, returnedSchema];
74 }
75 else {
76 throw new Error("Return value of a custom schema loader must be of type \"GraphQLSchema\"!");
77 }
78 return [3 /*break*/, 4];
79 case 3: throw new Error("Unable to find a loader function! Make sure to export a default function from your file");
80 case 4: return [3 /*break*/, 6];
81 case 5:
82 e_1 = _a.sent();
83 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 + "\"");
84 case 6:
85 try {
86 pointToSchema = null;
87 options = {};
88 if (typeof schemaDef === 'string') {
89 pointToSchema = schemaDef;
90 }
91 else if (typeof schemaDef === 'object') {
92 pointToSchema = Object.keys(schemaDef)[0];
93 options = schemaDef[pointToSchema];
94 }
95 if (config.pluckConfig) {
96 options.tagPluck = config.pluckConfig;
97 }
98 return [2 /*return*/, graphql_toolkit_1.loadSchema(pointToSchema, options)];
99 }
100 catch (e) {
101 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 ");
102 }
103 return [2 /*return*/];
104 }
105 });
106}); };
107exports.loadDocuments = function (documentDef, config) { return __awaiter(_this, void 0, void 0, function () {
108 var pointToDoc, defObject, loaderString, customDocumentLoader, returned, e_2;
109 return __generator(this, function (_a) {
110 switch (_a.label) {
111 case 0:
112 if (!(typeof documentDef === 'object' &&
113 documentDef[Object.keys(documentDef)[0]] &&
114 documentDef[Object.keys(documentDef)[0]].loader &&
115 typeof documentDef[Object.keys(documentDef)[0]].loader === 'string')) return [3 /*break*/, 6];
116 pointToDoc = Object.keys(documentDef)[0];
117 defObject = documentDef[pointToDoc];
118 loaderString = defObject.loader;
119 _a.label = 1;
120 case 1:
121 _a.trys.push([1, 5, , 6]);
122 customDocumentLoader = getCustomLoaderByPath(loaderString);
123 if (!customDocumentLoader) return [3 /*break*/, 3];
124 return [4 /*yield*/, customDocumentLoader(pointToDoc, config)];
125 case 2:
126 returned = _a.sent();
127 if (returned && Array.isArray(returned)) {
128 return [2 /*return*/, returned];
129 }
130 else {
131 throw new Error("Return value of a custom schema loader must be an Array of: { filePath: string, content: DocumentNode }");
132 }
133 return [3 /*break*/, 4];
134 case 3: throw new Error("Unable to find a loader function! Make sure to export a default function from your file");
135 case 4: return [3 /*break*/, 6];
136 case 5:
137 e_2 = _a.sent();
138 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 ");
139 case 6: return [2 /*return*/, graphql_toolkit_1.loadDocuments(documentDef, config.pluckConfig
140 ? {
141 tagPluck: config.pluckConfig
142 }
143 : {})];
144 }
145 });
146}); };
147//# sourceMappingURL=load.js.map
\No newline at end of file