UNPKG

simple-graphql

Version:

The simple way to generates GraphQL schemas and Sequelize models from your models definition.

112 lines 5.7 kB
"use strict"; 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 index_1 = require("../index"); const StringHelper_1 = __importDefault(require("../utils/StringHelper")); exports.default = { name: 'addMutation', defaultOptions: { enable: false }, priority: 0, description: 'Gen `add mutation` for Schema', applyToSchema: function (schema, options, schemas) { var _a, _b; const name = 'add' + StringHelper_1.default.toInitialUpperCase(schema.name); const addedName = 'added' + StringHelper_1.default.toInitialUpperCase(schema.name) + 'Edge'; const inputFields = Object.assign({}, (options.additionFields || {})); if (schema instanceof index_1.SGSchema && ((_b = (_a = schema.options.tableOptions) === null || _a === void 0 ? void 0 : _a.primaryKey) === null || _b === void 0 ? void 0 : _b.autoIncrement) === false) { inputFields['id'] = { type: schema.name, nullable: true }; } const isModelType = (fieldOptions) => fieldOptions.type && schemas[fieldOptions.type] != null; lodash_1.default.forOwn(schema.config.fields, (value, key) => { var _a, _b, _c, _d; 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.metadata) === null || _c === void 0 ? void 0 : _c.graphql) === null || _d === void 0 ? void 0 : _d.initializable) !== false) { inputFields[key] = value; } }); const { enable } = options, config = __rest(options, ["enable"]); schema.mutations({ [config.name || name]: { hookOptions: config.hookOptions, input: inputFields, output: { [addedName]: { type: schema.name + 'Edge' } }, mutateAndGetPayload: function (args, context, info, sgContext) { var _a, _b; return __awaiter(this, void 0, void 0, function* () { const dbModel = sgContext.models[schema.name]; const attrs = {}; if (schema instanceof index_1.SGSchema && ((_b = (_a = schema.options.tableOptions) === null || _a === void 0 ? void 0 : _a.primaryKey) === null || _b === void 0 ? void 0 : _b.autoIncrement) === false) { attrs['id'] = args['id']; } lodash_1.default.forOwn(schema.config.fields, (value, key) => { if (isModelType(value)) { if (!key.endsWith('Id')) { key = key + 'Id'; } if (args[key] !== undefined) { attrs[key] = args[key]; } } else if (args[key] !== undefined) { attrs[key] = args[key]; } }); const getInstance = () => __awaiter(this, void 0, void 0, function* () { if (options.duplicateCheck) { const existInstance = yield dbModel.findOne({ where: attrs }); if (existInstance) { return existInstance; } } return dbModel.create(attrs); }); const instance = yield getInstance(); return { [addedName]: { node: instance, cursor: instance.id } }; }); } } }); } }; //# sourceMappingURL=addMutationPlugin.js.map