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 chalk_1 = require("chalk");
|
6 | var glob = require("glob");
|
7 | var graphql_tools_1 = require("graphql-tools");
|
8 | var fs_1 = require("fs");
|
9 | var path = require("path");
|
10 | var fs = require("fs");
|
11 | var errors_1 = require("../../errors");
|
12 | var documents_from_glob_1 = require("../documents/documents-from-glob");
|
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.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 | return content;
|
25 | }
|
26 | var SchemaFromTypedefs = (function () {
|
27 | function SchemaFromTypedefs() {
|
28 | }
|
29 | SchemaFromTypedefs.prototype.canHandle = function (globPath) {
|
30 | return isGlob(globPath) || (isValidPath(globPath) && isGraphQLFile(globPath));
|
31 | };
|
32 | SchemaFromTypedefs.prototype.handle = function (globPath, config, schemaOptions) {
|
33 | var globFiles = glob.sync(globPath, { cwd: process.cwd() });
|
34 | if (!globFiles || globFiles.length === 0) {
|
35 | 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 ");
|
36 | }
|
37 | var mergeLogic = function (arr) { return arr; };
|
38 | if ('mergeSchemaFiles' in config) {
|
39 | var patternArr = config.mergeSchemaFiles.split('#');
|
40 | var mergeModuleName = patternArr[0];
|
41 | var mergeFunctionName = patternArr[1];
|
42 | if (!mergeModuleName || !mergeFunctionName) {
|
43 | throw new errors_1.DetailedError("You have to specify your merge logic with 'mergeSchema' option", "\n\n You have to specify your merge logic with 'mergeSchema' option.\n\n The pattern is following:\n \n " + chalk_1.default.bold('path/to/file') + "#" + chalk_1.default.bold('merge') + "\n\n - path/to/file - points to JavaScript module\n - merge - name of exported function\n \n \n CLI:\n\n $ gql-gen --mergeSchema path/to/file#merge\n\n API:\n\n generate({\n mergeSchema: 'path/to/file#merge',\n ...\n });\n\n ");
|
44 | }
|
45 | var localFilePath = path.resolve(process.cwd(), mergeModuleName);
|
46 | var localFileExists = fs.existsSync(localFilePath);
|
47 | var mergeModule = require(localFileExists ? localFilePath : mergeModuleName);
|
48 | if (!(mergeFunctionName in mergeModule)) {
|
49 | throw new errors_1.DetailedError("Provided function couldn't be found", "\n \n Provided " + mergeFunctionName + " function couldn't be found in " + mergeModule + "\n\n You probably forgot to export " + mergeFunctionName + " function\n\n export const " + mergeFunctionName + " ...\n\n ");
|
50 | }
|
51 | mergeLogic = mergeModule[mergeFunctionName];
|
52 | }
|
53 | var typeDefs = globFiles.length > 1
|
54 | ? mergeLogic(globFiles.map(function (filePath) { return fs_1.readFileSync(filePath, 'utf-8'); }))
|
55 | : loadSchemaFile(globFiles[0]);
|
56 | return graphql_tools_1.makeExecutableSchema({
|
57 | typeDefs: typeDefs,
|
58 | allowUndefinedInResolve: true,
|
59 | resolvers: {},
|
60 | resolverValidationOptions: { requireResolversForResolveType: false }
|
61 | });
|
62 | };
|
63 | return SchemaFromTypedefs;
|
64 | }());
|
65 | exports.SchemaFromTypedefs = SchemaFromTypedefs;
|
66 |
|
\ | No newline at end of file |