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 path_1 = require("path");
|
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 | var document_finder_1 = require("../../utils/document-finder");
|
13 | function isGraphQLFile(globPath) {
|
14 | return documents_from_glob_1.graphQLExtensions.some(function (ext) { return globPath.endsWith(ext); });
|
15 | }
|
16 | function loadSchemaFile(filepath) {
|
17 | var content = fs_1.readFileSync(filepath, {
|
18 | encoding: 'utf-8'
|
19 | });
|
20 | if (/^\# import /i.test(content.trimLeft())) {
|
21 | var importSchema = require('graphql-import').importSchema;
|
22 | return importSchema(filepath);
|
23 | }
|
24 | var foundDoc = document_finder_1.extractDocumentStringFromCodeFile(new graphql_1.Source(content, filepath));
|
25 | if (foundDoc) {
|
26 | return foundDoc;
|
27 | }
|
28 | return content;
|
29 | }
|
30 | var SchemaFromTypedefs = (function () {
|
31 | function SchemaFromTypedefs() {
|
32 | }
|
33 | SchemaFromTypedefs.prototype.canHandle = function (globPath) {
|
34 | return isGlob(globPath) || (isValidPath(globPath) && isGraphQLFile(globPath));
|
35 | };
|
36 | SchemaFromTypedefs.prototype.handle = function (globPath, config, schemaOptions) {
|
37 | var _this = this;
|
38 | var globFiles = glob.sync(globPath, { cwd: process.cwd() });
|
39 | if (!globFiles || globFiles.length === 0) {
|
40 | 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 ");
|
41 | }
|
42 | if (globFiles.length > 1) {
|
43 | return epoxy_1.mergeGraphQLSchemas(globFiles.map(function (filePath) { return _this.loadFileContent(filePath); }).filter(function (f) { return f; }));
|
44 | }
|
45 | else {
|
46 | return graphql_1.parse(loadSchemaFile(globFiles[0]));
|
47 | }
|
48 | };
|
49 | SchemaFromTypedefs.prototype.loadFileContent = function (filePath) {
|
50 | var fileContent = fs_1.readFileSync(filePath, 'utf8');
|
51 | var fileExt = path_1.extname(filePath);
|
52 | if (documents_from_glob_1.graphQLExtensions.includes(fileExt)) {
|
53 | return new graphql_1.Source(fileContent, filePath);
|
54 | }
|
55 | var foundDoc = document_finder_1.extractDocumentStringFromCodeFile(new graphql_1.Source(fileContent, filePath));
|
56 | if (foundDoc) {
|
57 | return new graphql_1.Source(foundDoc, filePath);
|
58 | }
|
59 | return null;
|
60 | };
|
61 | return SchemaFromTypedefs;
|
62 | }());
|
63 | exports.SchemaFromTypedefs = SchemaFromTypedefs;
|
64 |
|
\ | No newline at end of file |