UNPKG

2.18 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var isGlob = require("is-glob");
4var isValidPath = require("is-valid-path");
5var glob = require("glob");
6var graphql_tools_1 = require("graphql-tools");
7var fs_1 = require("fs");
8var fs = require("fs");
9var errors_1 = require("../../errors");
10var documents_from_glob_1 = require("../documents/documents-from-glob");
11var epoxy_1 = require("@graphql-modules/epoxy");
12function isGraphQLFile(globPath) {
13 return documents_from_glob_1.graphQLExtensions.some(function (ext) { return globPath.endsWith(ext); });
14}
15function loadSchemaFile(filepath) {
16 var content = fs.readFileSync(filepath, {
17 encoding: 'utf-8'
18 });
19 if (/^\# import /i.test(content.trimLeft())) {
20 var importSchema = require('graphql-import').importSchema;
21 return importSchema(filepath);
22 }
23 return content;
24}
25var SchemaFromTypedefs = /** @class */ (function () {
26 function SchemaFromTypedefs() {
27 }
28 SchemaFromTypedefs.prototype.canHandle = function (globPath) {
29 return isGlob(globPath) || (isValidPath(globPath) && isGraphQLFile(globPath));
30 };
31 SchemaFromTypedefs.prototype.handle = function (globPath, config, schemaOptions) {
32 var globFiles = glob.sync(globPath, { cwd: process.cwd() });
33 if (!globFiles || globFiles.length === 0) {
34 throw new errors_1.DetailedError('Unable to find matching files', "\n \n Unable to find matching files for glob: " + globPath + " in directory: " + process.cwd() + "\n ");
35 }
36 var documentNode = globFiles.length > 1
37 ? epoxy_1.mergeGraphQLSchemas(globFiles.map(function (filePath) { return fs_1.readFileSync(filePath, 'utf-8'); }))
38 : loadSchemaFile(globFiles[0]);
39 return graphql_tools_1.makeExecutableSchema({
40 typeDefs: documentNode,
41 allowUndefinedInResolve: true,
42 resolvers: {},
43 resolverValidationOptions: { requireResolversForResolveType: false }
44 });
45 };
46 return SchemaFromTypedefs;
47}());
48exports.SchemaFromTypedefs = SchemaFromTypedefs;
49//# sourceMappingURL=schema-from-typedefs.js.map
\No newline at end of file