Adding event-based invalidation support #8618
Unanswered
jimmycallin
asked this question in
Ideas
Replies: 2 comments 2 replies
-
@TkDodo We've brushed on this topic in the past. Where you you stand on stuff like this now? |
Beta Was this translation helpful? Give feedback.
2 replies
-
I just read about the tRPC query integration, which looks really cool! But they mention there is no primitive from them to integrate their subscription hook https://trpc.io/docs/client/tanstack-react-query/usage#subscriptionOptions This feels to me like a related use case, that could be worth exploring alongside this particular idea. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hello!
We have quite a few
useQuery
anduseInfiniteQuery
wrappers, that we over the years have managed to move more and more logic into react-query'squeryOptions
andinfiniteQueryOptions
, which has been great since we have been able to reuse these for both dynamic useQueries, as well as prefetching from the router's loader.There is one part of these wrappers we haven't been able to integrate, and that is our query invalidation logic. We listen to webhook events for update events to various entities, and in case an update matches the rendered entity, we invalidate the query.
Essentially, our hooks look like this now:
This works overall pretty well for us - we can fetch entities in a common way, and automatically have them invalidated as update events matching the entity are passed.
What I would love to have is to have the event invalidation more tightly integrated into react-query. There are a few reasons for me wanting this:
useEntity
wrapper, and have each component callgetEntityQuery
directly, and passing it into useQuery, useSuspenseQuery, useQueries, useInfiniteQuery, queryClient.ensureQueryData, etc, without having to create new hooks for each scenario. As long as I care about data invalidation on webhook events, I can't come up with a good way to do this.The way I imagine supporting this is through a new
invalidationManager
, that works close to how the other managers work. Something like:which then enables us to for each query define the same match fn as I'm doing in my useUpdateEvent hook, but as part of the
queryOptions
:I can also see this being useful to add to the QueryClient, to have a centralized place to add invalidation logic.
Thanks for reading, curious to hear what you think!
Beta Was this translation helpful? Give feedback.
All reactions