react-apollo icon indicating copy to clipboard operation
react-apollo copied to clipboard

useMutation mutate function does not call `onCompleted`

Open dminkovsky opened this issue 4 years ago • 9 comments

Intended outcome: onCompleted passed to mutate function should be called.

Actual outcome:

onCompleted passed to mutate function is not called. onCompleted must be passed directly to useMutation (second arg).

How to reproduce the issue:

Version

System: OS: macOS Mojave 10.14.6 Binaries: Node: 13.4.0 - /usr/local/bin/node Yarn: 1.21.1 - /usr/local/bin/yarn npm: 6.13.4 - /usr/local/bin/npm Browsers: Chrome: 79.0.3945.88 Firefox: 71.0 Safari: 13.0.3 npmPackages: @apollo/react-hooks: ^3.1.3 => 3.1.3 @apollo/react-ssr: ^3.1.3 => 3.1.3 apollo-cache-inmemory: ^1.6.5 => 1.6.5 apollo-client: ^2.6.8 => 2.6.8 apollo-link: ^1.2.13 => 1.2.13 apollo-link-context: ^1.0.19 => 1.0.19 apollo-link-error: ^1.1.12 => 1.1.12 apollo-link-http: ^1.5.16 => 1.5.16

dminkovsky avatar Dec 31 '19 01:12 dminkovsky

Don't know what's the issue here, but I'm using following workaround:

useMutation returns a promise, so you can pass the callback after calling the mutate function:

const [insertItem] = useMutation(insertItemQuery);

insertItem({
  variables: {}
}).then(
  res => handleSuccess(res),
  err => handleError(err)
);

beerose avatar Jan 05 '20 15:01 beerose

UPDATE Sorry, I've made a huge mistake, we have 2 very similarly named mutations and I was testing the wrong one for 30mins. 🤦‍♂ Everything is working as expected for me, maybe OP is doing something similar?


Im having the same issue ([email protected])

const [myMutation] = useMutation(MY_MUTATION, {
  onError: () => console.log('error!'), // never gets called
  onCompleted: () => console.log('completed!'), // never gets called
});

In the network tab, the correct mutation gets fired and returns correctly with

{
  errors: [{ ... }],
  data: { ... }, 
  extensions:  { ... },
}

orrybaram avatar Jan 07 '20 17:01 orrybaram

Having the same issue

SallyHabib97 avatar Mar 22 '20 16:03 SallyHabib97

+1

nratter avatar Apr 28 '20 19:04 nratter

+1

mufeez-amjad avatar Apr 29 '20 07:04 mufeez-amjad

@orrybaram in your example, you're passing the completion handler directly to useMutation. That works fine, I think the OP is referring to passing it to the mutate function (returned from useMutation)

I'm also having the same issue when onCompleted is passed to the mutate function, according to this onCompleted is a mutation option, but it's not being called:

https://github.com/apollographql/apollo-client/blob/master/docs/shared/mutation-options.mdx

mattgabor avatar May 04 '20 19:05 mattgabor

Don't know what's the issue here, but I'm using following workaround:

useMutation returns a promise, so you can pass the callback after calling the mutate function:

const [insertItem] = useMutation(insertItemQuery);

insertItem({
  variables: {}
}).then(
  res => handleSuccess(res),
  err => handleError(err)
);

I was unable to call useState functions in the onCompleted function that can be passed in the useMutation hook, but I was able to call them using then on the returned mutate object.

3dfoster avatar May 20 '20 20:05 3dfoster

+1

jebax avatar Jun 24 '20 10:06 jebax

+1

Aiden-Brine avatar Jul 06 '20 18:07 Aiden-Brine