UNPKG

simple-graphql

Version:

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

103 lines 4.98 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: 'bulkAddMutation', defaultOptions: { enable: false }, priority: 0, description: 'Gen `bulk add mutation` for Schema', applyToSchema: function (schema, options, schemas) { const name = 'bulkAdd' + StringHelper_1.default.toInitialUpperCase(schema.name); const addedName = 'added' + StringHelper_1.default.toInitialUpperCase(schema.name) + 'Edges'; 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; 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: { values: { elements: { properties: inputFields }, nullable: false } }, output: { [addedName]: { elements: { type: schema.name + 'Edge' } } }, mutateAndGetPayload: function ({ values }, context, info, sgContext) { return __awaiter(this, void 0, void 0, function* () { const dbModel = sgContext.models[schema.name]; const bulkDatas = []; for (const value of values) { const attrs = {}; lodash_1.default.forOwn(schema.config.fields, (options, key) => { if (isModelType(options)) { if (!key.endsWith('Id')) { key = key + 'Id'; } if (value[key] !== undefined) { attrs[key] = value[key]; } } else if (value[key] !== undefined) { attrs[key] = value[key]; } }); bulkDatas.push(attrs); } const instances = yield dbModel.bulkCreate(bulkDatas); return { [addedName]: instances.map((instance) => { return { node: instance, cursor: instance.id }; }) }; }); } } }); } }; //# sourceMappingURL=bulkAddMutationPlugin.js.map