Graphql 使用TypeORM getManyWithCount如何为分页生成PageInfo
我正在为GraphQL服务器实现中继式分页,并使用优秀的TypeORM库 我想找到在查询后创建PageInfo对象的最佳方法:Graphql 使用TypeORM getManyWithCount如何为分页生成PageInfo,graphql,apollo,apollo-client,relay,typeorm,Graphql,Apollo,Apollo Client,Relay,Typeorm,我正在为GraphQL服务器实现中继式分页,并使用优秀的TypeORM库 我想找到在查询后创建PageInfo对象的最佳方法: type PageInfo { endCursor: String hasNextPage: Boolean! hasPreviousPage: Boolean! startCursor: String } 这将非常有效,但请记住,totalEntitiesCount不是表中所有条目的计数,而只是与光标条件匹配的条目的计数(note.notedAt>:
type PageInfo {
endCursor: String
hasNextPage: Boolean!
hasPreviousPage: Boolean!
startCursor: String
}
这将非常有效,但请记住,
totalEntitiesCount
不是表中所有条目的计数,而只是与光标条件匹配的条目的计数(note.notedAt>:after
或note.notedAt>)
if (before) {
qb = qb.andWhere('note.notedAt <=(:before)', { before });
}
if (after) {
qb = qb.andWhere('note.notedAt >(:after)', { after });
}
qb = qb.take(args.take)
const [entities, totalEntitesCount] = qb.getManyWithCount()
function createPageInfo(
noteEdges: Array<{ node: Note; cursor: Date }>,
totalCount: number,
findOptions: NoteFindOptions
) {
let hasNextPage: boolean;
let hasPreviousPage: boolean;
if(findOptions.after) {
hasPreviousPage = true;
hasNextPage = (noteEdges.length < totalCount)
} else if (findOptions.before) {
hasNextPage = true;
hasPreviousPage = (noteEdges.length < totalCount)
} else {
hasPreviousPage = false;
hasNextPage = (noteEdges.length < totalCount)
}
return {
startCursor: noteEdges[0].cursor,
endCursor: noteEdges[noteEdges.length - 1].cursor,
hasNextPage,
hasPreviousPage
};
}