simple-graphql
Version:
The simple way to generates GraphQL schemas and Sequelize models from your models definition.
181 lines • 7.31 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 __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 isPrimaryOrder = (sgContext, schema, orderConfig) => {
if (lodash_1.default.isArray(orderConfig)) {
for (let i of orderConfig) {
if (i.model && i.as) {
if (schema.config.associations.hasMany[i.as]) {
return false;
}
else {
const config = schema.config.associations.belongsTo[i.as] ||
schema.config.associations.hasOne[i.as];
if (config) {
schema = sgContext.models[config.target].sgSchema;
}
else {
return true;
}
}
}
else {
return true;
}
}
}
return true;
};
function default_1(args) {
return __awaiter(this, void 0, void 0, function* () {
const dbModel = this;
const sgContext = dbModel.getSGContext();
let { pagination = {}, include = [], where = {}, attributes, bind = [], subQuery } = args;
let { after, first = 100, before, last } = pagination;
const option = dbModel.resolveQueryOption({
info: args.selectionInfo,
path: 'edges.node',
include: include,
order: args.order || [['id', 'ASC']],
attributes: attributes
});
include = option.include;
let order = option.order;
attributes = option.attributes;
const getSelections = (info) => {
if (!info) {
return [];
}
const fragments = info.fragments || {};
let selections = [];
(info.fieldNodes || []).forEach((node) => {
selections = lodash_1.default.union(selections, dbModel.parseSelections(fragments, node.selectionSet && node.selectionSet.selections));
});
return selections;
};
// 如果需要获取后面分页 或者 count 值,才需要计算
const needCount = last != null ||
before != null ||
getSelections(args.selectionInfo).find((s) => s.name === 'count' || s.name.startsWith('pageInfo')) != null;
const getCountResult = () => __awaiter(this, void 0, void 0, function* () {
if (!needCount) {
return 0;
}
else {
return dbModel.withCache
? yield dbModel.withCache().count({
distinct: true,
include: include,
where: where
// bind: bind #TODO 需要测试
})
: yield dbModel.count({
distinct: true,
include: include,
where: where
// bind: bind #TODO 需要测试
});
}
});
const count = yield getCountResult();
if (last != null || before != null) {
first = last || 100;
before = before || '' + (count + 1);
after = '' + (count - (parseInt(before) - 1));
order = order.map((orderItem) => {
if (Array.isArray(orderItem)) {
const revertItem = [...orderItem];
if (isPrimaryOrder(sgContext, dbModel.sgSchema, revertItem)) {
switch (revertItem[revertItem.length - 1].toLocaleUpperCase()) {
case 'ASC':
revertItem[revertItem.length - 1] = 'DESC';
break;
case 'DESC':
revertItem[revertItem.length - 1] = 'ASC';
break;
default:
revertItem.push('DESC');
}
}
}
return orderItem;
});
}
const offset = Math.max(after != null ? parseInt(after) : 0, 0);
const getRows = function () {
return __awaiter(this, void 0, void 0, function* () {
if (dbModel.hasSelection({
info: args.selectionInfo,
path: 'edges'
})) {
const findOptions = {
include: include,
where: where,
attributes: attributes,
bind: bind,
order: order,
limit: first,
offset: offset,
subQuery: subQuery
};
return dbModel.withCache
? yield dbModel.withCache().findAll(findOptions)
: yield dbModel.findAll(findOptions);
}
return [];
});
};
const rows = yield getRows();
let index = 0;
if (last || before) {
return {
pageInfo: {
startCursor: count - (offset + rows.length) + 1,
endCursor: count - offset,
hasPreviousPage: count - (offset + rows.length) > 0,
hasNextPage: offset > 0
},
edges: rows
.map((node) => {
return {
node: node,
cursor: count - offset - ++index + 1
};
})
.reverse(),
count: count
};
}
else {
return {
pageInfo: {
startCursor: offset + 1,
endCursor: offset + rows.length,
hasPreviousPage: offset > 0,
hasNextPage: offset + rows.length < count
},
edges: rows.map((node) => {
return {
node: node,
cursor: offset + ++index
};
}),
count: count
};
}
});
}
exports.default = default_1;
//# sourceMappingURL=resolveRelayConnection.js.map