-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathschema.js
56 lines (56 loc) · 2.82 KB
/
schema.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
"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);
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
exports.__esModule = true;
var debug_1 = __importDefault(require("debug"));
var mutation_1 = __importDefault(require("./root/mutation"));
var query_1 = __importDefault(require("./root/query"));
var subscriptions_1 = __importDefault(require("./root/subscriptions"));
var debug = (0, debug_1["default"])('gsg');
function generateSchema(_a) {
var graphqlSchemaDeclaration = _a.graphqlSchemaDeclaration, types = _a.types, models = _a.models, customMutations = _a.customMutations, _b = _a.globalPreCallback, globalPreCallback = _b === void 0 ? function () { return null; } : _b, pubSubInstance = _a.pubSubInstance, callWebhook = _a.callWebhook;
var mutationExists = !!customMutations ||
Object.values(graphqlSchemaDeclaration).some(function (type) {
if (type.actions) {
return ['create', 'delete', 'update'].some(function (action) {
return type.actions.includes(action);
});
}
return !!type.additionalMutations;
});
var definition = __assign({ query: (0, query_1["default"])(graphqlSchemaDeclaration, types.outputTypes, models, globalPreCallback) }, (mutationExists && {
mutation: (0, mutation_1["default"])(graphqlSchemaDeclaration, types.inputTypes, types.outputTypes, models, globalPreCallback, customMutations, pubSubInstance, callWebhook)
}));
// Do not generate subscriptions if no ways of propagating information is defined.
if (pubSubInstance) {
definition.subscription = (0, subscriptions_1["default"])(graphqlSchemaDeclaration, types, pubSubInstance);
}
var resolverTab = ['list', 'create', 'update', 'delete', 'count'];
Object.keys(graphqlSchemaDeclaration).forEach(function (key) {
var schema = graphqlSchemaDeclaration[key];
if (!schema.before) {
resolverTab.forEach(function (resolverName) {
var resolverBefore = schema[resolverName] && schema[resolverName].before
? schema[resolverName].before
: undefined;
if (!resolverBefore) {
debug("The ".concat(resolverName, " resolver of ").concat(key, " has no before hook. This may represent a security issue!"));
}
});
}
});
return definition;
}
exports["default"] = generateSchema;