Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: useMutation calls onError if errorPolicy is all #11103

Conversation

caylahamann
Copy link
Contributor

@caylahamann caylahamann commented Jul 26, 2023

Fixes #10973

Fixes a bug in useMutation so that onError is called when an error is returned from the request with errorPolicy set to 'all'.

Checklist:

  • If this PR contains changes to the library itself (not necessary for e.g. docs updates), please include a changeset (see CONTRIBUTING.md)
  • If this PR is a new feature, please reference an issue where a consensus about the design was reached (not necessary for small changes)
  • Make sure all of the significant new logic is covered by tests

@apollo-cla
Copy link

@caylahamann: Thank you for submitting a pull request! Before we can merge it, you'll need to sign the Apollo Contributor License Agreement here: https://contribute.apollographql.com/

@netlify
Copy link

netlify bot commented Jul 26, 2023

‼️ Deploy request for apollo-client-docs rejected.

Name Link
🔨 Latest commit 33d4e17

@changeset-bot
Copy link

changeset-bot bot commented Jul 26, 2023

🦋 Changeset detected

Latest commit: b383d17

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@apollo/client Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@jerelmiller jerelmiller self-requested a review July 26, 2023 20:07
@jerelmiller jerelmiller self-assigned this Jul 26, 2023
@jerelmiller
Copy link
Member

We've tweaked the file size checks in our release-3.8 branch, so feel free to ignore that failed check for now. We can get that updated once we've merged that branch into main 🙂.

Copy link
Member

@jerelmiller jerelmiller left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you sooooo much for fixing this! Funny that we called this out so explicitly in our docs, but this had been a bug for so long 😆. I had some minor suggestions for you if you'd like to take a look.

As for releasing this, I'll check to see if we want to get anything else in for our 3.8.0 release, otherwise we'll make this a fast-follow in 3.8.1. Thanks so much for the contribution!

.changeset/early-pumpkins-type.md Outdated Show resolved Hide resolved
src/react/hooks/__tests__/useMutation.test.tsx Outdated Show resolved Hide resolved
src/react/hooks/__tests__/useMutation.test.tsx Outdated Show resolved Hide resolved
expect(fetchResult.errors[0].message).toEqual(CREATE_TODO_ERROR);
expect(onError).toHaveBeenCalledTimes(1);
expect(onError.mock.calls[0][0].message).toBe(CREATE_TODO_ERROR);
expect(onCompleted).not.toHaveBeenCalled();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉

@@ -479,6 +523,45 @@ describe('useMutation Hook', () => {
expect(errorMock.mock.calls[0][0]).toMatch("Missing field");
errorMock.mockRestore();
});

it(`should not call onError when errorPolicy is 'ignore'`, async () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks so much for adding this test!


const onError = executeOptions.onError || ref.current.options?.onError

if (error && errorPolicy === 'all' && onError) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (error && errorPolicy === 'all' && onError) {
if (error && onError) {

I believe some of the internals of Apollo Client (QueryManager in particular) will remove the error property from the result for you when errorPolicy is set to ignore, so the explicit check is not needed. Check to see if you're able to remove this check and maintain passing tests.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confirmed that removing this works as expected 🎉 . I'm going to go ahead and apply it :)

@jerelmiller
Copy link
Member

@caylahamann we will go ahead and get this released with 3.8.0 🙂. I'm going to repoint this PR to our release branch and merge into that. We should be going live with 3.8 very soon. Thanks so much for fixing this!

@jerelmiller jerelmiller changed the base branch from main to release-3.8 July 27, 2023 23:19
@wpride
Copy link

wpride commented Aug 11, 2023

This has been bugging us for ages. Thank you.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 11, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

useMutation onError handler is not fired if error policy is set to "all"
4 participants