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 graphql_1 = require("graphql");
|
6 | var glob = require("glob");
|
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 | 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 | }());
|
45 | exports.SchemaFromTypedefs = SchemaFromTypedefs;
|
46 |
|
\ | No newline at end of file |