simple-graphql
Version:
The simple way to generates GraphQL schemas and Sequelize models from your models definition.
108 lines • 4.04 kB
JavaScript
;
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"));
function default_1(args) {
const dbModel = this;
const schema = this.sgSchema;
const getFieldName = (key, config) => {
if (config.type && dbModel.getSGContext().schemas[config.type] != null) {
return key + 'Id';
}
else {
return key;
}
};
const push = (array, element) => {
if (lodash_1.default.indexOf(array, element) === -1) {
array.push(element);
return true;
}
else {
return false;
}
};
const fieldToSelection = (field) => {
const index = field.indexOf('.');
if (index === -1) {
return { name: field };
}
else {
return {
name: field.substr(0, index),
selections: [fieldToSelection(field.substr(index + 1))]
};
}
};
const getDependentOptions = (option, fieldName) => {
const hasManyConfig = schema.config.associations.hasMany[fieldName];
if (hasManyConfig &&
hasManyConfig.outputStructure === 'Array' &&
(hasManyConfig.conditionFields == null ||
lodash_1.default.keys(hasManyConfig.conditionFields).length === 0)) {
const order = hasManyConfig.order || [['id', 'ASC']];
if (Array.isArray(order)) {
order.forEach((p) => {
if (typeof p[0] === 'string') {
push(option.additionFields, `${fieldName}.${p[0]}`);
}
});
}
}
const linkConfig = schema.config.links[fieldName];
if (linkConfig) {
if (linkConfig.dependentFields) {
linkConfig.dependentFields.forEach((field) => {
const ss = field.split('.');
if (push(option.additionFields, field)) {
option = getDependentOptions(option, ss[0]);
}
});
if (schema.config.fields[fieldName]) {
push(option.attributes, getFieldName(fieldName, schema.config.fields[fieldName]));
}
}
else {
// if no dependentFields, default depend all field
lodash_1.default.forOwn(schema.config.fields, (value, key) => {
push(option.attributes, getFieldName(key, value));
});
}
}
else {
let fieldConfig = schema.config.fields[fieldName];
if (!fieldConfig && fieldName.endsWith('Id')) {
fieldName = fieldName.substr(0, fieldName.length - 2);
fieldConfig = schema.config.fields[fieldName];
}
if (fieldConfig) {
push(option.attributes, getFieldName(fieldName, fieldConfig));
}
else if (fieldName === '*') {
lodash_1.default.forOwn(schema.config.fields, (value, key) => {
push(option.attributes, getFieldName(key, value));
});
}
}
return option;
};
let option = {
additionFields: [],
attributes: [...(args.attributes || []), 'id']
};
if (args.selections) {
args.selections.forEach((selection) => {
if (selection.namedType == null || selection.namedType === schema.name) {
option = getDependentOptions(option, selection.name);
}
});
}
return {
attributes: lodash_1.default.uniq(option.attributes),
additionSelections: option.additionFields.map((field) => fieldToSelection(field))
};
}
exports.default = default_1;
//# sourceMappingURL=parseAttributes.js.map