simple-graphql
Version:
The simple way to generates GraphQL schemas and Sequelize models from your models definition.
129 lines • 6.42 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const lodash_1 = __importDefault(require("lodash"));
const StringHelper_1 = __importDefault(require("../utils/StringHelper"));
const index_1 = require("../index");
exports.default = {
name: 'updateMutation',
defaultOptions: {
enable: false
},
priority: 0,
description: 'Gen `update mutation` for Schema',
applyToSchema: function updateMutation(schema, options, schemas) {
const name = 'update' + StringHelper_1.default.toInitialUpperCase(schema.name);
const changedName = 'changed' + StringHelper_1.default.toInitialUpperCase(schema.name);
const isModelType = (fieldOptions) => fieldOptions.type && schemas[fieldOptions.type] != null;
const valuesInputFieldMap = {};
lodash_1.default.forOwn(schema.config.fields, (value, key) => {
var _a, _b, _c, _d, _e;
if (isModelType(value)) {
if (!key.endsWith('Id')) {
key = key + 'Id';
}
}
if (((_b = (_a = value.metadata) === null || _a === void 0 ? void 0 : _a.graphql) === null || _b === void 0 ? void 0 : _b.hidden) !== true &&
((_d = (_c = value === null || value === void 0 ? void 0 : value.metadata) === null || _c === void 0 ? void 0 : _c.graphql) === null || _d === void 0 ? void 0 : _d.updatable) !== false) {
valuesInputFieldMap[key] = Object.assign(Object.assign({}, value), { nullable: true, metadata: { description: (_e = value.metadata) === null || _e === void 0 ? void 0 : _e.description } });
}
});
const inputFields = {
id: {
type: schema.name + 'Id',
nullable: false
},
values: {
properties: valuesInputFieldMap
}
};
let versionConfig = false;
if (schema instanceof index_1.SGSchema) {
versionConfig = (schema.options.tableOptions || {}).version;
if (versionConfig === true || typeof versionConfig === 'string') {
inputFields[typeof versionConfig === 'string' ? versionConfig : 'version'] = {
type: 'Integer',
nullable: true
};
}
}
if (lodash_1.default.keys(inputFields.values.properties).length === 0) {
return;
}
const { enable } = options, config = __rest(options, ["enable"]);
schema.mutations({
[config.name || name]: {
hookOptions: config.hookOptions,
input: inputFields,
output: {
[changedName]: { type: schema.name }
},
mutateAndGetPayload: function (args, context, info, sgContext) {
return __awaiter(this, void 0, void 0, function* () {
if (args == null || args.values == null) {
throw new Error('Missing update values.');
}
const dbModel = sgContext.models[schema.name];
const values = {};
lodash_1.default.forOwn(schema.config.fields, (value, key) => {
if (isModelType(value)) {
if (!key.endsWith('Id')) {
key = key + 'Id';
}
if (args.values[key] !== undefined) {
values[key] = args.values[key];
}
}
else if (args.values[key] !== undefined) {
values[key] = args.values[key];
}
});
const instance = yield dbModel.findOne({
where: { id: args.id },
lock: true
});
if (!instance) {
throw new Error(schema.name + '[' + args.id + '] not exist.');
}
else {
if (versionConfig === true || typeof versionConfig === 'string') {
const versionField = typeof versionConfig === 'string' ? versionConfig : 'version';
if (args[versionField] != null &&
instance[versionField] !== args[versionField]) {
throw new Error('OptimisticLockingError: Invalid version number.');
}
}
yield instance.update(values);
}
return {
[changedName]: instance
};
});
}
}
});
}
};
//# sourceMappingURL=updateMutationPlugin.js.map