Apollo Client fetchMore from useQuery在每次提取后都会重复网络请求



我们有一个web应用程序。我们一直在广泛使用Apollo+Rreact的useQuery,为用户提供无限滚动体验。我们最近意识到,在第二次使用fetchMore后,组件没有加载资产。在网络选项卡上,第一个和第二个请求一直循环。

经过一些调试,我意识到当我们使用fetchMore时,我需要在Apollo中有一个合并函数来合并数据。

我更改了代码。将typePolicies中的AssetData添加到下面:

export const getApolloClient = (): ApolloClient<NormalizedCacheObject> =>
new ApolloClient({
cache: new InvalidationPolicyCache({
typePolicies: {
AssetData: {
keyFields: [],
fields: {
assets: {
merge(existing = [], incoming: string[], { args }) {
return [...existing, ...incoming];
}
}
}
},
},
invalidationPolicies: {
types: {
Asset: {
timeToLive: 29 * 60 * 1000
}
}
}
}),
link: new HttpLink({ fetch })
});

这有助于打破循环。但我仍然看到,在每次fetchMore请求之后,会触发另一个对原始查询的重复网络调用。这实际上搅乱了阿波罗的整个合并功能。我会有很多重复的数据。很多人都报告了类似的问题,我尝试了许多建议的解决方案[https://github.com/apollographql/apollo-client/issues/7307][1] [https://github.com/apollographql/apollo-client/issues/7939][2] [https://github.com/apollographql/apollo-client/issues/9668][3]

但没有什么能帮我解决问题。

这是我的问题:

const { loading, data, error, fetchMore, networkStatus } = useQuery<
AssetsQuery
>(assetsQuery, {
variables: {
role,
version: version,
offset: 0,
limit: 30,
region
},
fetchPolicy: 'cache-and-network',
nextFetchPolicy: 'cache-first',
});

在其中一个线程中,apollo团队提到这个问题在新版本3.6.0中得到了解决。所以,我也将我的apollo版本从3.2.0更新为3.6.9,但我仍然有同样的问题。

如果你有任何进一步的建议,请告诉我

[1] :https://github.com/apollographql/apollo-client/issues/7307[2]:https://github.com/apollographql/apollo-client/issues/7939[3]:https://github.com/apollographql/apollo-client/issues/9668

尝试将@apolo/client包更新到最新版本。现在是@3.7.14。它解决了我的问题。

最新更新