UNPKG

1.93 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var graphql_1 = require("graphql");
4var graphql_codegen_core_1 = require("graphql-codegen-core");
5var fs = require("fs");
6var path = require("path");
7var document_finder_1 = require("../../utils/document-finder");
8exports.loadFileContent = function (filePath) {
9 if (fs.existsSync(filePath)) {
10 var fileContent = fs.readFileSync(filePath, 'utf8');
11 var fileExt = path.extname(filePath);
12 if (fileExt === '.graphql' || fileExt === '.gql') {
13 return graphql_codegen_core_1.parse(new graphql_codegen_core_1.Source(fileContent, filePath));
14 }
15 var foundDoc = document_finder_1.extractDocumentStringFromCodeFile(fileContent);
16 if (foundDoc) {
17 return graphql_codegen_core_1.parse(new graphql_codegen_core_1.Source(foundDoc, filePath));
18 }
19 else {
20 return null;
21 }
22 }
23 else {
24 throw new Error("Document file " + filePath + " does not exists!");
25 }
26};
27var effectiveRules = graphql_1.specifiedRules.filter(function (f) { return f.name !== 'NoUnusedFragments'; });
28exports.loadDocumentsSources = function (schema, filePaths) {
29 var loadResults = filePaths
30 .map(function (filePath) { return ({ filePath: filePath, content: exports.loadFileContent(filePath) }); })
31 .filter(function (result) { return result.content; });
32 var errors = loadResults
33 .map(function (result) { return ({
34 filePath: result.filePath,
35 errors: graphql_1.validate(schema, result.content, effectiveRules).filter(function (e) { return e.message.indexOf('Unknown fragment') === -1; })
36 }); })
37 .filter(function (r) { return r.errors.length > 0; });
38 return errors.length > 0 ? errors : graphql_codegen_core_1.concatAST(loadResults.map(function (r) { return r.content; }));
39};
40//# sourceMappingURL=document-loader.js.map
\No newline at end of file