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 glob = require("glob");
|
6 | var graphql_tools_1 = require("graphql-tools");
|
7 | var fs_1 = require("fs");
|
8 | var graphql_import_1 = require("graphql-import");
|
9 | var path = require("path");
|
10 | var fs = require("fs");
|
11 | var SchemaFromTypedefs = (function () {
|
12 | function SchemaFromTypedefs() {
|
13 | }
|
14 | SchemaFromTypedefs.prototype.canHandle = function (globPath) {
|
15 | return isGlob(globPath) || (isValidPath(globPath) && globPath.endsWith('.graphql'));
|
16 | };
|
17 | SchemaFromTypedefs.prototype.handle = function (globPath, cliOptions) {
|
18 | var globFiles = glob.sync(globPath, { cwd: process.cwd() });
|
19 | if (!globFiles || globFiles.length === 0) {
|
20 | throw new Error("Unable to find matching files for glob: " + globPath + "!");
|
21 | }
|
22 | var mergeLogic = function (arr) { return arr; };
|
23 | if ('mergeSchema' in cliOptions) {
|
24 | var patternArr = cliOptions.mergeSchema.split('#');
|
25 | var mergeModuleName = patternArr[0];
|
26 | var mergeFunctionName = patternArr[1];
|
27 | if (!mergeModuleName || !mergeFunctionName) {
|
28 | throw new Error('You have to specify your merge logic with `mergeSchema` option; <mergeModule#mergeFn>');
|
29 | }
|
30 | var localFilePath = path.resolve(process.cwd(), mergeModuleName);
|
31 | var localFileExists = fs.existsSync(localFilePath);
|
32 | var mergeModule = require(localFileExists ? localFilePath : mergeModuleName);
|
33 | if (!(mergeFunctionName in mergeModule)) {
|
34 | throw new Error(mergeFunctionName + " couldn't be found in " + mergeModule);
|
35 | }
|
36 | mergeLogic = mergeModule[mergeFunctionName];
|
37 | }
|
38 | var typeDefs = globFiles.length > 1
|
39 | ? mergeLogic(globFiles.map(function (filePath) { return fs_1.readFileSync(filePath, 'utf-8'); }))
|
40 | : graphql_import_1.importSchema(globFiles[0]);
|
41 | return graphql_tools_1.makeExecutableSchema({
|
42 | typeDefs: typeDefs,
|
43 | allowUndefinedInResolve: true,
|
44 | resolvers: {},
|
45 | resolverValidationOptions: { requireResolversForResolveType: false }
|
46 | });
|
47 | };
|
48 | return SchemaFromTypedefs;
|
49 | }());
|
50 | exports.SchemaFromTypedefs = SchemaFromTypedefs;
|
51 |
|
\ | No newline at end of file |