coops22
coops22

Reputation: 481

Possible unhandled promise rejection warning with useMutation

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

Upvotes: 6

Views: 4060

Answers (2)

vipin goyal
vipin goyal

Reputation: 701

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

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

Upvotes: 0

Oleg Kupriianov
Oleg Kupriianov

Reputation: 178

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

Upvotes: 2

Related Questions