createOptimisticAction rollback too returning in the mutationFn does nothing
and onUpdate handlers doesnt fire
18 Replies
wise-white•3mo ago
onUpdate only calls when you do mutations outside of an explicit transaction like this. The documentation is poor still for this but you also need to await a call to either invalidate tags or collection.utils.refetch()
Nothing needs returned from the mutationFn either
like-goldOP•3mo ago
So how do I avoid the rollback ?
wise-white•3mo ago
It's rolling back because the optimistic state is dropped when you return from the mutationFn
like-goldOP•3mo ago
The example in the doc has a return I was thinking that it would validate if the transaction match the servers data
wise-white•3mo ago
If you've refetched your collection then your optimistic state will be replaced with the new server state
Yeah the doc needs updated...
like-goldOP•3mo ago
There’s no other way to do it than writeUpdate ?
That would be great to do something that add the data without refetching and without calling writeUpdate I think
wise-white•3mo ago
How would that work?
Just by applying the return value?
like-goldOP•3mo ago
Transaction are only for optimistic update ?
wise-white•3mo ago
Yes
like-goldOP•3mo ago
If I do a writeUpdate in the mutationFn that fine ?
wise-white•3mo ago
A transaction is tracking the syncing of the mutation to the server and the sync back of the new server data
Yup! That or just refetch everything!
like-goldOP•3mo ago
But refetch can be extensive for network sometimes
wise-white•3mo ago
Sure, that's why the direct write APIs exist
like-goldOP•3mo ago
Yea but that a bit redundant
wise-white•3mo ago
Redundancy is good 😂
like-goldOP•3mo ago
I’m gonna do a writeUpdate in the mutationFn so
Not saying is not
wise-white•3mo ago
Refetch is the default as it's automatic and works great for most use cases. Direct writes is for everything else
like-goldOP•3mo ago
Ok ok
Thx