UNPKG

@grapi/server

Version:

Grapi Schema Generator For GraphQL Server

54 lines (53 loc) 2.33 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const dataModel_1 = require("../dataModel"); const hooks_1 = require("../hooks"); const lodash_1 = require("../lodash"); const baseType_1 = __importDefault(require("./baseType")); const whereInput_1 = __importDefault(require("./whereInput")); class DeletePlugin { whereInputPlugin; baseTypePlugin; hook; constructor({ hook }) { this.hook = hook; } setPlugins(plugins) { this.whereInputPlugin = plugins.find(plugin => plugin instanceof whereInput_1.default); this.baseTypePlugin = plugins.find(plugin => plugin instanceof baseType_1.default); } visitModel(model, context) { const { root } = context; const directives = model.getDirectives(dataModel_1.DirectiveModelAction.Delete); const mutationName = DeletePlugin.getInputName(model); const where = this.whereInputPlugin.getWhereUniqueInputName(model); const returnType = `${model.getNamings().capitalSingular}`; root.addMutation(`${mutationName}(where: ${where}!): ${returnType}!${directives}`); } resolveInMutation({ model, dataSource }) { const inputName = DeletePlugin.getInputName(model); const wrapDelete = (0, lodash_1.get)(this.hook, [model.getName(), 'wrapDelete']); return { [inputName]: async (root, args, context) => { const whereUnique = this.whereInputPlugin.parseUniqueWhere(args.where); const object = await (0, hooks_1.findUniqueObjectOnModel)(args.where, model); if (!wrapDelete) { await dataSource.delete(whereUnique, context); return object; } const deleteContext = { where: args.where, response: {}, graphqlContext: context }; await wrapDelete(deleteContext, async () => { await dataSource.delete(whereUnique, context); }); return object; }, }; } static getInputName(model) { return `delete${model.getNamings().capitalSingular}`; } } exports.default = DeletePlugin;