-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Invalidate by ID in react-query #29
Comments
Before I answer, why do you need invalidation instead of in place data update? Do you need adding items to/removing from arrays perhaps? |
Anyway, this might be related to #22 in order to do this, we would need to expose https://github.com/klis87/normy/blob/master/packages/normy/src/create-normalizer.ts#L167C26-L167C55 as a separate method, so only to get list of dependent queries, without manual denormalization Anyway, it is necessary to do this only for operation on arrays, like pushing, removing, sorting. If you want to invalidate only to update properties of existing objects, you are covered automatically, no matter where those objects are and how deep. In the future I hope to also cover arrays wit #1 |
Four main use cases for me: 1: adding and removing to lists, many layers deep. I typically can get the IDs of all the parents very trivially but would also be great if these were tracked by Normy. Add/remove list helpers could be great here also! 2: Failed mutations. I often find it easy to optimistically update mutations and refetch on failure. Saves some complexity of saving the old state to revert back to.
4: peer to peer realtime updates. To save having to build a fully authenticated realtime server, it easy to just broadcast what each client changed to all other clients listening in a channel. Then the clients could just invalidate all "ids" they receive. |
|
Re 4, I'm using react-query specifically to avoid having to build some kind of sync engine/ proper realtime service, so data over the wire is exactly what I'm trying to avoid, however it might not be as bad as I was thinking. I was imagining a lightweight server, possibly with no authorization, that lets any client subscribe to any channel, but only receives IDs that it then needs to decide itself what to refetch. Latency is no issue here just keeping things relatively in sync within a few seconds. I currently handle this via polling. This could definitely be a horrible approach 😂 |
Re 4, just remember, that over the wire you would need to only pass objects with ids and attrs only which were actually changed, like imagine an action like updated articles with id 1 and 2, all you need to pass is: [{ id: 1, name: 'Updated name 1' }, { id: 2, name: 'Updated name 2' }] then on push notification, all your queries will be updated immediately. On the contrary, if your strategy is to refetch everything, you could end up with refetching dozens of requests simultaneously, which could cause perf issues. Anyway, I would really like to have this feature built-in, as it is important to give a choice, sometimes invalidation might be just better |
Agree that refetching is dangerous... invalidating is the correct thing to do here as then only active queries are refetched and other views are refetched in the background when navigating to pages that use them. |
Ran across another good use case: When doing optimistic updates you going to want to cancel all in-flight queries to avoid having your update rolled back before the mutation completes. You can probably cover 99% of cases via something along the lies of: queryClient.cancelQueries({
queryKey: getQueryKeysById(
event.mutation.state.variables.id,
),
}); |
This is actually very interesting case, I never thought about that one. |
It would be awesome if there was a way to invalidate and/or refetch all queries based on an ID. I often find myself 3-4 layers deep in a nested object and I need to invalidate every single list view in all of its parents, but I don't want to invalidate pages of those lists if they don't contain the child. This would be especially useful paired with paginated/infinite query helpers and is also a great onError default for failed mutations.
I would love to help implement this, @klis87 could you point me in right direction if this is possible and what methods from core need to be wrapped.
The text was updated successfully, but these errors were encountered: