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

RN Apollo Client 3.0 - handle refetch with merge function

我最近从 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],
            };
          },
        },
      },
    },
  },
});

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

如果有人知道如何处理,请告诉我。

我能够通过观察 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],
                };
              },
            },
          },
        },
      },
    });