Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Graphql 使用TypeORM getManyWithCount如何为分页生成PageInfo_Graphql_Apollo_Apollo Client_Relay_Typeorm - Fatal编程技术网

Graphql 使用TypeORM getManyWithCount如何为分页生成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>:

我正在为GraphQL服务器实现中继式分页,并使用优秀的TypeORM库

我想找到在查询后创建PageInfo对象的最佳方法:

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
  };
}