react-apollo - RN Apollo Client 3.0 - 使用合并功能处理重新获取

标签 react-apollo apollo-client

我最近从 2.0 迁移到 apollo 客户端 3.0。

我有一个查询需要获取更多和分页。

通过做,

const cache = new InMemoryCache({
  typePolicies: {
    Query: {
      fields: {
        getData: {
          // Handles incoming data
          keyArgs: [],
          merge(existing ={/*some default object fields*/}, incoming) {
            return {
              ...existing,
              pageInfo: incoming.pageInfo,
              edges: [...existing.edges, ...incoming.edges],
            };
          },
        },
      },
    },
  },
});

我能够处理初始查询/提取和分页。 但是,我在处理重新获取时遇到了麻烦。 使用此合并功能,重新获取的数据将与现有缓存数据连接起来。 我无法找到如何在合并功能中正确处理此问题。

如果有人知道如何处理这个问题,请告诉我。

最佳答案

我能够通过观察 args 来变通。

const cache = new InMemoryCache({
      typePolicies: {
        Query: {
          fields: {
            getData: {
              // Handles incoming data
              keyArgs: [],
              merge(existing ={/*some default object fields*/}, incoming, {args}) {
                if(args && !args.after){
                    // Initial fetch or refetch
                    return incoming;
                }
                
                // Pagination
                return {
                  ...existing,
                  pageInfo: incoming.pageInfo,
                  edges: [...existing.edges, ...incoming.edges],
                };
              },
            },
          },
        },
      },
    });

关于react-apollo - RN Apollo Client 3.0 - 使用合并功能处理重新获取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63108804/

相关文章:

reactjs - <Query> 与 graphql Hoc 这是在 React apollo 中将 graphql 查询与组件 Hook 的最佳方式

apollo - Apollo 中的嵌套查询缓存重定向

graphql - 如何正确地将变量传递给 apollo useQuery?

reactjs - GraphQL错误: Expected type Int. Int不能表示非整数值

reactjs - 使用 Refetch 和 apollo 重新渲染查询和组件

graphql - 可以使用 apollo-client 跳过部分查询

apollo - 如何强制 Apollo Client 使用缓存数据

graphql - 为什么我的 Apollo 缓存更新没有反射(reflect)在我的查询中?

reactjs - 在 Next.js 应用程序中使用 GraphQL 的推荐方法

react-native - Apollo graphql : writeQuery after mutation does not trigger re-render of flatlist