1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | var isGlob = require("is-glob");
|
4 | var isValidPath = require("is-valid-path");
|
5 | var glob = require("glob");
|
6 | var graphql_tools_1 = require("graphql-tools");
|
7 | var fs_1 = require("fs");
|
8 | var fs = require("fs");
|
9 | var errors_1 = require("../../errors");
|
10 | var documents_from_glob_1 = require("../documents/documents-from-glob");
|
11 | var epoxy_1 = require("@graphql-modules/epoxy");
|
12 | function isGraphQLFile(globPath) {
|
13 | return documents_from_glob_1.graphQLExtensions.some(function (ext) { return globPath.endsWith(ext); });
|
14 | }
|
15 | function 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 | }
|
25 | var SchemaFromTypedefs = (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 | }());
|
48 | exports.SchemaFromTypedefs = SchemaFromTypedefs;
|
49 |
|
\ | No newline at end of file |