relay: commitMutation should return errors in onCompleted
Hi, I’m trying to propagate errors to the users that occurred in a commitMutation
The following documentation does not really provide any info other that an array is returned in the onCompleted
callback #commitMutation.
Giving the code example, errors
are undefind if an error occur on the server. I’m using a wrong id
on purpose (for testing) and the graphql server throws an error.
const mutation = ...
const deleteStuff = (environment, { stuffId }) => {
const variables = {
input: { stuffId }
};
commitMutation(environment, {
mutation,
variables,
onCompleted: (response, errors) => {
console.log("response", response)
console.log("errors", errors)
},
onError: err => console.error("ERR", err)
});
};
Ok, so onError
shows on errors, thats fine. But errors
are undefind
in onCompled
. Is this expected behaviour?
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Comments: 18 (8 by maintainers)
Just to be clear, Relay does conform to the GraphQL spec and best practices - but I can understand confusion here, as the best practices for error handling in GraphQL have emerged over time. The GraphQL
errors
field is intended for truly exceptional errors such as an invalid query or variable value: things that are not reasonably expected to occur in an application. Invalid user input or a backend service being unavailable are reasonable errors that are expected to occur (hopefully infrequently for the service down case!). If information about errors is required by the application to function, I suggest following @leebyron’s advice:link
link
There’s a great writeup by @alloy on this approach as the final suggestion in http://artsy.github.io/blog/2018/10/19/where-art-thou-my-error/#Make.exceptions.first-class.citizens.of.your.schema.