Possible unhandled promise rejection warning with useMutation
Asked Answered
I

2

6

I'm getting an unhandled promise rejection error when I use useMutation with react native. Here's the code producing the issue:

const [createUser, { error, loading }] = useMutation(CREATE_USER_MUTATION);

Anytime my graphql server returns an error to the client I get an unhandled promise rejection error (screenshot below). I'm able to make it go away by adding an error handler like so, but it seems like a hack. Any thoughts? Am I doing something wrong or is this something that should be addressed by the apollo folks?

const [createUser, { error, loading }] = useMutation(CREATE_USER_MUTATION, {
  onError: () => {}
});

enter image description here

Idioglossia answered 10/8, 2019 at 15:21 Comment(1)
It looks like this answer has a detail explanation of what's going on: https://mcmap.net/q/265455/-handling-errors-with-react-apollo-usemutation-hook.Dustcloth
L
2

Your createUser mutation is a promise you should handle error inside try catch block, or in upper scope inside apollo-link-error onError method.

Large answered 24/12, 2019 at 17:51 Comment(0)
A
0
const [createUser, { data, loading }] = useMutation(CREATE_USER_MUTATION, {
  onError: (err) => {
      setError(err);
  }
});

With this we can access data with loading and proper error handling.

Ation answered 30/4, 2021 at 8:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.