Context returned in `onMutate` is not available in `onError` hook (and `onMutate` has no errors)
ymansurozer opened this issue · 3 comments
I am implementing a basic optimistic to-do inspired by @posva's PR on Atidone. But I have an issue where the context I return in onMutate
is not being available in onError
. And I am certain onMutate
is not erroring.
Reproduction: https://github.com/ymansurozer/optimistic/blob/main/pages/basic.vue
Reproduction code (quite simple so I am adding it here too):
const { mutate } = useMutation({
mutation: (data: Omit<Todo, 'id'>) => $fetch('/api/non-existent', { method: 'POST', body: { todo: data } }),
onMutate: () => {
try {
const context = { test: 'data' }
console.log('>> Context from onMutate', context)
return context
}
catch (error) {
console.error('>> Error in onMutate', error)
return { test: 'data' }
}
},
onError: (error, variables, { test }) => {
console.log('>> Context from onError', test)
console.log('>> Variables', variables)
},
})
And the logs:
>> Context from onMutate {test: 'data'}
404 (Page not found: /api/non-existent)
>> Context from onError undefined
>> Variables Proxy(Object) {text: 'test', completed: false}
P.S.: I am working on a generic useOptimisticList
composable to simplify optimistic updates for all CRUD operations for a basic list. The composable is here and a sample usage is here. basic.vue
is a stripped version to show the bug. I was hoping we could include the composable in the cookbooks in the docs, if you're open to it @posva. Happy to create a PR. ❤️
Thanks! I fixed the bug locally, I will push it and release later, there are a few changes coming up
Regarding the optimistic updates: of course we can add a cookbook entry in the future. I was planning on releasing a plugin to handle optimistic updates for classic scenarios (list + detail calls). The plugin API is still experimental but I would love to have feedback around it! But maybe a composable is indeed an easier choice
Thank you Eduardo!
To be perfectly honest with you, I haven't looked at the plugin API so haven't thought about it. But I'll do so because it does look promising.