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: () => {}
});
A function that performs an asynchronous task and returns a promise.
Apollo React Mutation Update We pass the update function as an option to useMutation . - const [addTodo] = useMutation(ADD_TODO); + const [addTodo] = useMutation(ADD_TODO, {update: updateCache}); We need to fetch the current list of todos from the cache.
UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch (). (rejection id: 4) To say the least, I have created a helper function which looks something like this
If you do not provide a rejection handler callback to the promise, and it fails, this is where the Javascript runtime gets upset and throws the “Unhandled Promise Rejection” error. If you don’t tell the promise how to handle the error if it rejects, the error doesn’t know where to go. The solution.
The answer is similar to when the promise is resolved; you access the error by registering a handler/callback by passing it to the .catch method on the promise. Below is an example of how you can access the error when a promise is rejected.
To recap, To register a rejection handler in a promise chain, you need to call the .catch method on the promise and pass it to your rejection handler. Finally, the Unhandled Promise Rejection error! Now that we have an idea of the different states of a promise, can you think of why the “Unhandled Promise Rejection” error may be occurring?
Your createUser
mutation is a promise you should handle error inside try catch
block, or in upper scope inside apollo-link-error onError
method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With