question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

queryCache.refetchQueries(...) won't work as documented if staleTime is not reached (or at least if set to Infinity)

See original GitHub issue

After displaying a list (data coming from a query), a component use a mutation to post some new data that would go to this list. After mutation succeed, I invalidate the query cache (multiple techniques were tried, with different and most of the time expected results, see below) concerning the queries, then push back the list on to history.

The techniques I used to invalidate the cache are the following:

  1. refetchQueries (as the docs suggest).
await queryCache.refetchQueries(["items"]);
  1. refetchQueries with {force: true}
await queryCache.refetchQueries(["items"], {force: true});
  1. removeQueries
await queryCache.removeQueries(["items"]);
  1. set queries as stale so the next render will actually fetch the data
queryCache.getQueries(["items"]).forEach((query) => {
  query.state.isStale = true;
});

With default configuration, every method works (with slightly different but expected behaviors).

With react-query configured to use a non-default staleTime though, only 2, 3 and 4 works, as refetchQueries (and more specifically the fetch method on the query will ignore non-stale queries if not forced.

Although after reading the code I understand this is the expected behavior, I think there are a few defects :

  • Documentation states that after a mutation, you can refetch your data using refetchQueries and suggest to do so. It makes it hard to debug as there are no pointers to what can make the refetching not happen.
  • Although refetching is great, in some case it may be more correct to just set the queries as stale and let the app refetch the data itself whenever a component uses one of the queries (aka method 4). In some cases (ok, including mine), one mutation could invalidate a bunch of queries, while the immediate need for the next user view is probably only one of those queries. Making the query stale makes it easier for the developper which does not have to know the logic of “what next page will need”. We stale a bunch of queries, then mutate history, react-query does the rest (with old data shown while refetching). Refetching all queries related may be a bit too much, most of them may be never used again. Would it be possible to add an api method to queryCache to setQueriesAsStale (or whatever english describes this best) ? Then it could be shown as an alternative to refetch in documentation once mutation is done.

I can work on a patch (code, tests and doc) if this makes sense.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

5reactions
tannerlinsleycommented, Jun 2, 2020

I think this would be a good addition or fix to the library. I would expect this new function to be something like invalidateQueries. It would allow invalidating multiple queries. If those queries are currently in use on the page (they have open instances to them), they should get refetched, if they do not, then they should be marked as stale. Thoughts?

2reactions
tannerlinsleycommented, Jun 15, 2020

I’m still working on it. But now I’m thinking it will be more like this: refetchQueries is now gone and replaced by invalidateQueries. invalidateQueries(queryKey) will at the very least invalidate every query it matches no matter what and (by default) will also trigger a refetch for queries that are currently in use via useQuery (and friends). You can turn this off via invalidateQueries({ refetchActive: false })

Read more comments on GitHub >

github_iconTop Results From Across the Web

queryCache.refetchQueries(...) won't work as documented ...
queryCache.refetchQueries(...) won't work as documented if staleTime is not reached (or at least if set to Infinity) #521.
Read more >
What are staleTime and cacheTime in React-Query?
I want to store data for 2 mins in the cache after first-time API call. No matter if the component mounts or unmounts,...
Read more >
react-query - npm
Hooks for managing, caching and syncing asynchronous and remote data in React. Latest version: 3.39.2, last published: 2 months ago.
Read more >
QueryClient | TanStack Query Docs
If the query exists and the data is not invalidated or older than the given staleTime , then the data from the cache...
Read more >
Effective React Query Keys | TkDodo's blog
The answer is: You don't. That's not what refetch is for - it's for refetching with the same parameters. If you have some...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found