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