简体   繁体   English

如何在graphql Apollo客户端中实现基于游标的分页?

[英]How to implement cursor based pagination in graphql Apollo client?

I am following the official docs for cursor based Apollo Client implementation.我正在关注基于游标的 Apollo Client 实现的官方文档 I have already done the offset based pagination but cursor based seemed to be better fit in my case.我已经完成了基于偏移量的分页,但基于光标的分页似乎更适合我的情况。 The very first line is giving an error第一行给出了一个错误

const { data: { comments, cursor }, loading, fetchMore } = useQuery( MORE_COMMENTS_QUERY );

cannot read property 'comments' of undefined . cannot read property 'comments' of undefined This must be because data is still undefined.这一定是因为数据仍未定义。 Is the documentation missing something or am I missing something ?文档是遗漏了什么还是我遗漏了什么?

It's the docs.这是文档。 You should either provide a default value for data:您应该为数据提供一个默认值:

const { data: { comments, cursor } = {}, loading, fetchMore } = useQuery(...)

or else check whether it exists before accessing properties on it.或者在访问它的属性之前检查它是否存在。

const { data, loading, fetchMore } = useQuery(...)
if (data) {
  const { comments, cursor } = data
}

The latter is preferred because depending on the schema, it's possible for the query to complete and the data to be null (if you had errors in the response) and the default value won't be applied if data is null , only if it's undefined .后者是首选,因为根据架构,查询可能会完成并且数据可能为null (如果您在响应中有错误)并且如果datanull ,则不会应用默认值,只有在undefined .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM