UNPKG

simple-graphql

Version:

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

200 lines 9.88 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 sequelize_1 = __importDefault(require("sequelize")); const StringHelper_1 = __importDefault(require("../utils/StringHelper")); const getSearchFields = (additionFields, schema, schemas) => { const isModelType = (fieldOptions) => fieldOptions.type && schemas[fieldOptions.type] != null; const advanceType = (options) => { if (options.elements) { return { properties: { contains: options.elements } }; } else if (options.type === 'Boolean') { return options; } const aType = { ne: options, eq: options, in: { elements: options }, notIn: { elements: options } }; if (options.type === 'Number' || options.type === 'Integer' || options.type === 'Date') { aType.gt = options; aType.gte = options; aType.lt = options; aType.lte = options; aType.between = options; aType.notBetween = options; } if (options.type === 'String') { aType.like = options; aType.notLike = options; aType.startsWith = options; aType.endsWith = options; aType.substring = options; aType.regexp = options; aType.notRegexp = options; } return { properties: aType }; }; const searchFields = {}; lodash_1.default.forOwn(Object.assign(Object.assign({ id: { type: schema.name + 'Id' } }, schema.config.fields), additionFields), (value, key) => { var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k; 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.searchable) !== false) { if ((_f = (_e = value.metadata) === null || _e === void 0 ? void 0 : _e.graphql) === null || _f === void 0 ? void 0 : _f.mapper) { //自定义mapper不需要扩展查询类型 searchFields[key] = { definition: Object.assign(Object.assign({}, value), { nullable: true, metadata: { description: (_g = value.metadata) === null || _g === void 0 ? void 0 : _g.description } }), mapper: (_j = (_h = value.metadata) === null || _h === void 0 ? void 0 : _h.graphql) === null || _j === void 0 ? void 0 : _j.mapper }; } else { searchFields[key] = { definition: advanceType(Object.assign(Object.assign({}, value), { nullable: true, metadata: { description: (_k = value.metadata) === null || _k === void 0 ? void 0 : _k.description } })), mapper: function (option, argValue, sgContext) { if (argValue !== undefined) { option.where[sequelize_1.default.Op.and] = option.where[sequelize_1.default.Op.and] || []; if (argValue == null || typeof argValue === 'boolean') { option.where[sequelize_1.default.Op.and].push({ [key]: argValue }); } else { const keyCondition = {}; for (const opKey of lodash_1.default.keys(argValue)) { if (opKey !== 'contains') { keyCondition[sequelize_1.default.Op[opKey]] = argValue[opKey]; } else { option.where[sequelize_1.default.Op.and].push(sequelize_1.default.literal(`json_contains(\`${key}\`, '${JSON.stringify(argValue[opKey])}' )`)); } } option.where[sequelize_1.default.Op.and].push({ [key]: keyCondition }); } } } }; } } }); return searchFields; }; exports.default = { name: 'pluralQuery', defaultOptions: { enable: false }, priority: 0, description: 'Gen `plural query` for Schema', applyToSchema: function pluralQuery(schema, options, schemas) { const searchFields = getSearchFields(options.conditionFields || {}, schema, schemas); const { enable } = options, config = __rest(options, ["enable"]); schema.queries({ [`${StringHelper_1.default.toInitialLowerCase(config.name || schema.name + 's')}`]: { hookOptions: config.hookOptions, output: { type: schema.name + 'Connection' }, input: Object.assign(Object.assign({}, (lodash_1.default.keys(searchFields).length > 0 ? { condition: { elements: { properties: lodash_1.default.mapValues(searchFields, (fieldConfig) => { const { mapper, definition } = fieldConfig; return definition; }) }, metadata: { description: 'Query Condition' } } } : {})), { sort: { elements: { properties: { field: { enum: lodash_1.default.keys(searchFields) }, order: { enum: ['ASC', 'DESC'] } } }, metadata: { description: 'Define the sort field' } } }), resolve: function (args, context, info, sgContext) { return __awaiter(this, void 0, void 0, function* () { const dbModel = sgContext.models[schema.name]; const { sort = [{ field: 'id', order: 'ASC' }], condition = [] } = args || {}; const queryOption = { where: {}, bind: [], attributes: [] }; if (condition && condition.length > 0) { const where = queryOption.where; for (const c of condition) { queryOption.where = {}; for (const key of lodash_1.default.keys(searchFields)) { yield searchFields[key].mapper(queryOption, c[key], sgContext); } if (queryOption.where[sequelize_1.default.Op.and]) { where[sequelize_1.default.Op.or] = where[sequelize_1.default.Op.or] || []; where[sequelize_1.default.Op.or].push({ [sequelize_1.default.Op.and]: queryOption.where[sequelize_1.default.Op.and] }); } } queryOption.where = where; } return dbModel.resolveRelayConnection({ pagination: args, selectionInfo: info, where: queryOption.where, bind: queryOption.bind, attributes: queryOption.attributes, order: sort.map((s) => [s.field, s.order]) }); }); } } }); } }; //# sourceMappingURL=pluralQueryPlugin.js.map