1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | var graphql_1 = require("graphql");
|
4 | var graphql_codegen_core_1 = require("graphql-codegen-core");
|
5 | var fs = require("fs");
|
6 | var path = require("path");
|
7 | var document_finder_1 = require("../../utils/document-finder");
|
8 | exports.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 | };
|
27 | var effectiveRules = graphql_1.specifiedRules.filter(function (f) { return f.name !== 'NoUnusedFragments'; });
|
28 | var IGNORED_VALIDATION_ERRORS = ['Unknown fragment', 'Unknown directive'];
|
29 | exports.loadDocumentsSources = function (schema, filePaths) {
|
30 | var loadResults = filePaths
|
31 | .map(function (filePath) { return ({ filePath: filePath, content: exports.loadFileContent(filePath) }); })
|
32 | .filter(function (result) { return result.content; });
|
33 | var errors = loadResults
|
34 | .map(function (result) { return ({
|
35 | filePath: result.filePath,
|
36 | errors: graphql_1.validate(schema, result.content, effectiveRules).filter(function (e) { return !IGNORED_VALIDATION_ERRORS.find(function (ignoredErr) { return e.message.indexOf(ignoredErr) > -1; }); })
|
37 | }); })
|
38 | .filter(function (r) { return r.errors.length > 0; });
|
39 | return errors.length > 0 ? errors : graphql_codegen_core_1.concatAST(loadResults.map(function (r) { return r.content; }));
|
40 | };
|
41 |
|
\ | No newline at end of file |