UNPKG

1.96 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var isGlob = require("is-glob");
4var isValidPath = require("is-valid-path");
5var graphql_1 = require("graphql");
6var glob = require("glob");
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 if (globFiles.length > 1) {
37 return epoxy_1.mergeGraphQLSchemas(globFiles.map(function (filePath) { return fs_1.readFileSync(filePath, 'utf-8'); }));
38 }
39 else {
40 return graphql_1.parse(loadSchemaFile(globFiles[0]));
41 }
42 };
43 return SchemaFromTypedefs;
44}());
45exports.SchemaFromTypedefs = SchemaFromTypedefs;
46//# sourceMappingURL=schema-from-typedefs.js.map
\No newline at end of file