setQueryDefaults
more granularity with predicate function
#7826
Unanswered
paulobmarcos
asked this question in
Ideas
Replies: 1 comment 3 replies
-
My suggestion in these cases would be to re-structure your query keys to be one object, because objects have no order:
because then you can do:
and it'll match all ids. You can then do the same with |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hey TanStack team 👋
Problem
There are times when developers may need more granularity when setting query defaults.
Disclaimer: The examples bellow are just for demonstration purposes.
Just as an example, let's say there are a few endpoints:
/post/{id}/likes
/post/{id}/comments
/post/{id}/views
Let's also say cache keys are defined like this:
["post", {id}, "likes"]
["post", {id}, "comments"]
["post", {id}, "views"]
If there is a need to invalidate all caches for a specific post, it can easily be done with:
If there is a need to invalidate only the
comments
cache for all posts it's still easily managed with apredicate
:However, if we want to set a query default for all
comments
query keys, it's not really possible sincesetQueryDefaults
does not provide a similar predicate.If we rearranged cache keys from most generic to most specific, it could mitigate this but it would introduce a tradeoff when we want to invalidate all caches for a specific post:
There are also other scenarios where rearranging the keys from most generic to most specific could introduce challenges.
Currently, we have an issue where cache key factories are auto generated for hundreds of endpoints and rearranging them can lead to collisions or mismatches.
Idea
To make React Query even more flexible and adaptable to many different scenarios, it would be great to introduce a similar "match function" to
setQueryDefaults
.Example:
Or:
Even if anyone wanted to encapsulate completely different query keys with the same query defaults it would provide an easier/single way of doing so.
This would not be a breaking change, just an extension of
setQueryDefaults
.Would this be something worth pursuing? If so, I will gladly contribute with the change.
Beta Was this translation helpful? Give feedback.
All reactions