[英]How to set a time-based fetch policy in apollo client for graphql queries?
I am trying to use Apollo client to cache Grapql query results.我正在尝试使用 Apollo 客户端来缓存 Grapql 查询结果。 I reviewed the fetch policies from the docs but I could not find a way to use a cache based policy with expiration.
我查看了文档中的获取策略,但我找不到使用基于缓存的过期策略的方法。
In my code I have:在我的代码中,我有:
const client = new ApolloClient({
link: from([errorLink, httpLink]),
ssrMode: typeof window === "undefined",
errorPolicy: "all",
cache: new InMemoryCache(),
defaultOptions: {
watchQuery: {
fetchPolicy: "cache-first",
},
query: {
fetchPolicy: "cache-first",
},
},
});
The cache-first
policy does what I am looking for but it does not expire. cache-first
策略可以满足我的要求,但不会过期。 Is there a way to make the policy dynamic?有没有办法使政策动态化?
It is kind of hard to understand your use case, as a lot of details are missing.由于缺少很多细节,因此很难理解您的用例。
However, I think what your are looking for is the cache evict functionality.但是,我认为您正在寻找的是缓存驱逐功能。 Put that into a
setTimeout
to clear the cached data after a given timeout.将其放入
setTimeout
以在给定超时后清除缓存数据。
If you want to clear the cache only if the data has not been accessed for a while, you need to combine it with a clearTimeout
.如果您只想在一段时间未访问数据时清除缓存,则需要将其与
clearTimeout
结合使用。
I dont think there is a solution with just a fetch policy, they are rather unflexible.我不认为只有一个获取策略的解决方案,它们相当不灵活。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.