question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

onCompleted and onError not working with mutation

See original GitHub issue

Hey, great library.

I’m trying to catch mutation errors but it seems none of the callbacks are called. The only way I was able to catch errors is to use errorPolicy: “ignore” and then checking the result in refetchQueries callback.

Is it an error or am I doing something wrong?

addUser = useMutation(mutation, {
    update: (proxy, mutationResult) => {
       /* NOT CALLED */
    },
    refetchQueries: (result) => {
      /* CALLED ON errorPolicy: "ignore" */
    },
    onCompleted: (data) => {
       /* NOT CALLED */
    },
    onError: (data) => {
       /* NOT CALLED */
    }
  });

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:14 (2 by maintainers)

github_iconTop GitHub Comments

66reactions
zebapycommented, Jan 30, 2019

I needed this and loading so I made a wrapper hook. To those who may find it useful:

import { useState } from 'react';
import { useMutation as useHookMutation } from 'react-apollo-hooks';

export function useMutation(
  mutation,
  { onCompleted, onError, ...options } = {}
) {
  const [loading, setLoading] = useState(false);
  const [called, setCalled] = useState(false);
  const [error, setError] = useState(null);
  const [data, setData] = useState(null);

  const mutate = useHookMutation(mutation, options);

  const handler = async (...args) => {
    setLoading(true);
    setCalled(true);
    setError(null);
    setData(null);

    try {
      const { data } = await mutate(...args);

      setData(data);

      setLoading(false);

      if (onCompleted) {
        onCompleted(data);
      }

      return { data };
    } catch (e) {
      setLoading(false);
      setError(e);

      if (onError) {
        onError(e);
      }
    }
  };

  return [
    handler,
    {
      loading,
      called,
      error,
      data
    }
  ];
}

22reactions
trojanowskicommented, Nov 19, 2018

Hi @shlomokraus

Right now you can use only the mutation options supported directly by apollo-client. onCompleted and onError are react-apollo specific. You can have a similar result with something like that:

const addUser = useMutation(mutation);

const myHandler = () => {
  addUser().then(
    result => {
      // success callback
    },
    error => {
      // error callback
   }
  );
}

Or with async/await syntax:

const addUser = useMutation(mutation);

async function myHandler() {
  let result;
  try {
    result = await addUser();
  } catch (error) {
    // error handler
  }
}

And then:

<button onClick={myHandler}>Add user</button>
Read more comments on GitHub >

github_iconTop Results From Across the Web

`onCompleted` and `onError` not working in `useMutation ...
Given the following code: const [createMedicalDocument, { loading }] = useMutation( CREATE_MEDICAL_DOCUMENT_MUTATION, { onCompleted: (data) ...
Read more >
Mutations in Apollo Client - Apollo GraphQL Docs
A callback function that's called when your mutation successfully completes with zero errors (or if errorPolicy is ignore and partial data is returned)....
Read more >
onCompleted of useMutation get executed even on error
This is the reason that the GraphQL errors are ignored. They will only be triggered when using the default policy of none ....
Read more >
commitMutation | Relay
The value passed to onCompleted is the the mutation fragment, as read out from the store, after updaters and declarative mutation directives are ......
Read more >
How to use the react-relay.commitMutation function in ... - Snyk
function commit({ environment, onCompleted, onError }) { const variables = { input: {} } commitMutation(environment, { mutation, variables, onCompleted, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found