How do I access `error` field from `onComplete` or `data` field from `onError` in Apollo client

We have a graphql mutation call that can possibly return a response with both the errors field and data field:

{
 "data": // data,
 "errors": // errors,
}

without setting errorPolicy: all to the useMutation hook, onCompleted will fire only if there is no errors present in the response and onError will fire only if there is errors present in the response. However, the issue I am facing is that, onComplete handler doesn’t have access to the errors field when it is present in the response and onError handler doesn’t have access to the data field in the response either.

Once I set errorPolicy: all to the useMutation hook, onComplete will always fire even when the errors field is present in the response, but still it doesn’t have access to errors from the onComplete handler. The onError handler will never fire even when the errors field is present in the response. We can only get the errors from the result object returned by useMutation.

I wonder if there is a better, more elegant way to handle this within the respective handlers (i.e. onCompleted and onError)?

Hi @joji_miller :wave: welcome to the Apollo GraphQL Forum! The client callbacks can indeed be confusing and the team is definitely interested in simplifying the API. Until then, two things: first, I’ll bring this to the team to discuss, this seems like something that might be workable. Second, might I ask more about your use case?

You can destructure data, error variables from mutation or query. they will be available on both callback since they are at top level.