simple-graphql
Version:
The simple way to generates GraphQL schemas and Sequelize models from your models definition.
137 lines • 7.54 kB
JavaScript
;
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 sequelize_1 = __importDefault(require("sequelize"));
exports.default = {
name: 'genHasManyLink',
defaultOptions: {
enable: true
},
priority: 99,
description: 'Gen `HasManyLink` for Schema',
applyToSchema: function hasManyLinkedField(schema, options, schemas) {
if (schema instanceof index_1.SGSchema) {
lodash_1.default.forOwn(schema.config.associations.hasMany, (config, key) => {
if (config.hidden) {
return;
}
const args = {};
const conditionFields = {};
lodash_1.default.forOwn(config.conditionFields || {}, function (value, key) {
var _a, _b;
return __awaiter(this, void 0, void 0, function* () {
conditionFields[key] = {
definition: value,
mapper: ((_b = (_a = value.metadata) === null || _a === void 0 ? void 0 : _a.graphql) === null || _b === void 0 ? void 0 : _b.mapper) ||
function (option, argValue) {
if (argValue !== undefined) {
option.where[sequelize_1.default.Op.and] =
option.where[sequelize_1.default.Op.and] || [];
option.where[sequelize_1.default.Op.and].push({ [key]: argValue });
}
}
};
});
});
if (conditionFields && lodash_1.default.keys(conditionFields).length > 0) {
args.condition = {
properties: lodash_1.default.mapValues(conditionFields, (field) => {
const { mapper, definition } = field;
return definition;
})
};
}
schema.links({
[key]: {
hookOptions: config.hookOptions,
description: config.description,
input: args,
output: config.outputStructure === 'Array'
? { elements: { type: config.target } }
: { type: config.target + 'Connection' },
dependentFields: [config.sourceKey || 'id'],
resolve: function (root, args, context, info, sgContext) {
return __awaiter(this, void 0, void 0, function* () {
if (root[key] !== undefined &&
(config.conditionFields == null ||
lodash_1.default.keys(config.conditionFields).length === 0)) {
return root[key] || [];
}
const queryOption = {
where: Object.assign({}, (config.scope || {})),
bind: [],
attributes: []
};
if (args && args.condition) {
for (const key of lodash_1.default.keys(conditionFields)) {
yield conditionFields[key].mapper(queryOption, args.condition[key], sgContext);
}
}
const sourceKey = config.sourceKey || 'id';
const foreignKey = config.foreignKey || config.foreignField + 'Id';
queryOption.where[foreignKey] = root[sourceKey];
const dbModel = sgContext.models[config.target];
if (config.outputStructure === 'Array') {
const option = dbModel.resolveQueryOption({
order: config.order || [['id', 'ASC']],
info: info,
attributes: queryOption.attributes
});
const findOptions = {
where: queryOption.where,
bind: queryOption.bind,
include: option.include,
attributes: option.attributes,
order: option.order
};
if (dbModel.withCache) {
return dbModel.withCache().findAll(findOptions);
}
else {
return dbModel.findAll(findOptions);
}
}
else {
const _a = args || {}, { condition } = _a, relayArgs = __rest(_a, ["condition"]);
return sgContext.models[config.target].resolveRelayConnection({
pagination: relayArgs,
selectionInfo: info,
where: queryOption.where,
bind: queryOption.bind,
attributes: queryOption.attributes,
order: config.order
});
}
});
}
}
});
});
}
}
};
//# sourceMappingURL=genHasManyLinkPlugin.js.map