-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
72 lines (72 loc) · 2.47 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
"use strict";
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
exports.__esModule = true;
exports.generateSchema = exports.mergeSchemas = exports.combineResolvers = void 0;
var path = require("path");
var fs = require("fs");
var glob = require("fast-glob");
var combineResolvers = function (_a) {
var resolvers = _a.resolvers;
var Query = {};
var Mutation = {};
resolvers.map(function (resolver) {
if (resolver.hasOwnProperty("Query")) {
Query = __assign(__assign({}, Query), resolver.Query);
}
if (resolver.hasOwnProperty("Mutation")) {
Mutation = __assign(__assign({}, Mutation), resolver.Mutation);
}
});
return { Query: Query, Mutation: Mutation };
};
exports.combineResolvers = combineResolvers;
var mergeSchemas = function (_a) {
var pathfiles = _a.pathfiles;
var schemas = [];
glob.sync(pathfiles).forEach(function (file) {
try {
var data = fs.readFileSync(path.resolve(file), "utf8");
schemas.push(data);
}
catch (err) {
throw new Error("Oops Error ! Couldn't merge definitions Type , check schemas");
}
});
return schemas;
};
exports.mergeSchemas = mergeSchemas;
var generateSchema = function (_a) {
var inputPath = _a.inputPath, _b = _a.schemaPath, schemaPath = _b === void 0 ? "" : _b, _c = _a.typeDefsPath, typeDefsPath = _c === void 0 ? "" : _c;
var mergedSchemas = (0, exports.mergeSchemas)({ pathfiles: inputPath });
var schema = mergedSchemas.join(" ");
if (schemaPath !== "") {
fs.writeFile(schemaPath, schema, function (err) {
if (err) {
console.error(err);
return;
}
});
console.log(" ✔ Schema Generated : ", schemaPath);
}
if (typeDefsPath !== "") {
var typeDefs = "export default `" + schema + "`;";
fs.writeFile(typeDefsPath, typeDefs, function (err) {
if (err) {
console.error(err);
return;
}
});
console.log(" ✔ typeDefs Generated : ", typeDefsPath);
}
};
exports.generateSchema = generateSchema;