UNPKG

simple-graphql

Version:

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

88 lines 4.57 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 StringHelper_1 = __importDefault(require("../utils/StringHelper")); exports.default = { name: 'saveMutation', defaultOptions: { enable: false }, priority: 0, description: 'Gen `save mutation` for Schema', applyToSchema: function (schema, options, schemas) { const name = 'save' + StringHelper_1.default.toInitialUpperCase(schema.name); const savedName = 'saved' + StringHelper_1.default.toInitialUpperCase(schema.name); const inputFields = {}; const isModelType = (fieldOptions) => fieldOptions.type && schemas[fieldOptions.type] != null; lodash_1.default.forOwn(schema.config.fields, (value, key) => { var _a, _b, _c, _d, _e, _f, _g; 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 && ((_f = (_e = value.metadata) === null || _e === void 0 ? void 0 : _e.graphql) === null || _f === void 0 ? void 0 : _f.updatable) !== false) { inputFields[key] = Object.assign(Object.assign({}, value), { nullable: false, metadata: { description: (_g = value.metadata) === null || _g === void 0 ? void 0 : _g.description } }); } }); const { enable } = options, config = __rest(options, ["enable"]); schema.mutations({ [config.name || name]: { hookOptions: config.hookOptions, input: inputFields, output: { [savedName]: { type: schema.name } }, mutateAndGetPayload: function (args, context, info, sgContext) { return __awaiter(this, void 0, void 0, function* () { const dbModel = sgContext.models[schema.name]; const attrs = {}; 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]; } }); yield dbModel.upsert(attrs); return { [savedName]: dbModel.findOne({ where: attrs }) }; }); } } }); } }; //# sourceMappingURL=saveMutationPlugin.js.map