0

When I'm using the useMutation hook and getting an error on the response the onCompleted method gets triggered. Is that normal behavior?

 const [createPerson] = useMutation(CreatePerson, {
   onCompleted: () => {
     console.log('even onError it`s executed')
   }
 })
Ar26
  • 350
  • 1
  • 11
  • Can you please also post the `createPerson` call? It is not normal behavior. Normal behavior would be to fire something like `onError`. – Gh05d Oct 12 '20 at 08:16
  • This is the response I'm getting from the server: {data: null, errors: [{message: ''already exist", path: ["/createPerson"], ....}],... } – Ar26 Oct 12 '20 at 08:32
  • Yeah, I am more interested in the full function call, as there could be a problem in the error handling. – Gh05d Oct 12 '20 at 08:36
  • await createPerson({ variables: { input: { name: user.name, email: user.email } }, }) – Ar26 Oct 12 '20 at 08:44
  • Did you set a specific error policy? – Gh05d Oct 12 '20 at 09:43
  • yes, on ApolloProvider. Do you think it's related? I did it because I want to handle the errors myself and to not have the apollo error page for default – Ar26 Oct 12 '20 at 09:46
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/222899/discussion-between-gh05d-and-arie5). – Gh05d Oct 12 '20 at 10:04

1 Answers1

2

From our discussion in the chat I take that you have set the error policy of the Apollo Provider to ignore:

ignore: Ignore allows you to read any data that is returned alongside GraphQL Errors, but doesn't save the errors or report them to your UI.

This is the reason that the GraphQL errors are ignored. They will only be triggered when using the default policy of none.

More info about the error policies here and in this thread.

Gh05d
  • 3,394
  • 1
  • 12
  • 31
  • 1
    I will change it to the default error policy, now I trying to catch all the API errors in the apolloProider because I want to prevent the default error page – Ar26 Oct 12 '20 at 10:41
  • 1
    If you want to keep your current structure, you could also check the status code on the data prop that you get from the `onCompleted` function and only do stuff when there is no error. But this is kind of a hack and would have to be implemented everywhere. – Gh05d Oct 12 '20 at 12:19