简体   繁体   中英

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. 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. 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.

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 .

I dont think there is a solution with just a fetch policy, they are rather unflexible.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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