Skip to content
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

Context returned in onMutate is not available in onError hook (and onMutate has no errors) #95

Closed
ymansurozer opened this issue Nov 4, 2024 · 3 comments
Assignees
Labels
🐞 bug this isn't working as expected

Comments

@ymansurozer
Copy link

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. ❤️

@github-project-automation github-project-automation bot moved this to 🆕 Triaging in Pinia Colada Roadmap Nov 4, 2024
@posva posva added the 🐞 bug this isn't working as expected label Nov 4, 2024 — with Volta.net
Copy link
Owner

posva commented Nov 4, 2024

Thanks! I fixed the bug locally, I will push it and release later, there are a few changes coming up

Copy link
Owner

posva commented Nov 4, 2024

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

@posva posva self-assigned this Nov 4, 2024
@ymansurozer
Copy link
Author

ymansurozer commented Nov 4, 2024

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.

posva added a commit that referenced this issue Nov 5, 2024
@posva posva closed this as completed in 618312b Nov 5, 2024
@github-project-automation github-project-automation bot moved this from 🆕 Triaging to Done in Pinia Colada Roadmap Nov 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐞 bug this isn't working as expected
Projects
Archived in project
Development

No branches or pull requests

2 participants