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

coops22
  • 401
  • 7
  • 15

2 Answers2

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.

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.

vipin goyal
  • 671
  • 11
  • 17