-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Merged
jerelmiller
merged 9 commits into
apollographql:release-3.8
from
caylahamann:cayla/use-mutation-onError-bug
Jul 27, 2023
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
5252a6d
fix: useMutation calls onError if errorPolicy is all
33d4e17
chore: check that on complete isnt called for errorpolicy all
f4c146d
chore: add changeset for bug fix
3e2b3c5
Update .changeset/early-pumpkins-type.md
caylahamann f8ce693
Update src/react/hooks/__tests__/useMutation.test.tsx
caylahamann 7d1c977
Update src/react/hooks/__tests__/useMutation.test.tsx
caylahamann a397c46
Merge branch 'release-3.8' into cayla/use-mutation-onError-bug
jerelmiller 2221651
Update size-limit
jerelmiller b383d17
Remove unneeded check for errorPolicy when calling onError
jerelmiller File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@apollo/client": patch | ||
--- | ||
|
||
Fixes a bug in `useMutation` so that `onError` is called when an error is returned from the request with `errorPolicy` set to 'all' . |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -437,6 +437,50 @@ describe('useMutation Hook', () => { | |
expect(fetchResult.errors[0].message).toEqual(CREATE_TODO_ERROR); | ||
}); | ||
|
||
it(`should call onError when errorPolicy is 'all'`, async () => { | ||
const variables = { | ||
description: 'Get milk!' | ||
}; | ||
|
||
const mocks = [ | ||
{ | ||
request: { | ||
query: CREATE_TODO_MUTATION, | ||
variables | ||
}, | ||
result: { | ||
data: CREATE_TODO_RESULT, | ||
errors: [new GraphQLError(CREATE_TODO_ERROR)], | ||
}, | ||
} | ||
]; | ||
|
||
const onError = jest.fn(); | ||
const onCompleted = jest.fn(); | ||
|
||
const { result } = renderHook( | ||
() => useMutation(CREATE_TODO_MUTATION, { errorPolicy: 'all', onError, onCompleted }), | ||
{ wrapper: ({ children }) => ( | ||
<MockedProvider mocks={mocks}> | ||
{children} | ||
</MockedProvider> | ||
)}, | ||
); | ||
|
||
const createTodo = result.current[0]; | ||
|
||
let fetchResult: any; | ||
await act(async () => { | ||
fetchResult = await createTodo({ variables }); | ||
}); | ||
|
||
expect(fetchResult.data).toEqual(CREATE_TODO_RESULT); | ||
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(); | ||
}); | ||
|
||
it(`should ignore errors when errorPolicy is 'ignore'`, async () => { | ||
const errorMock = jest.spyOn(console, "error") | ||
.mockImplementation(() => {}); | ||
|
@@ -476,6 +520,44 @@ describe('useMutation Hook', () => { | |
expect(errorMock.mock.calls[0][0]).toMatch("Missing field"); | ||
errorMock.mockRestore(); | ||
}); | ||
|
||
it(`should not call onError when errorPolicy is 'ignore'`, async () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks so much for adding this test! |
||
const variables = { | ||
description: 'Get milk!' | ||
}; | ||
|
||
const mocks = [ | ||
{ | ||
request: { | ||
query: CREATE_TODO_MUTATION, | ||
variables, | ||
}, | ||
result: { | ||
errors: [new GraphQLError(CREATE_TODO_ERROR)], | ||
}, | ||
} | ||
]; | ||
|
||
const onError = jest.fn(); | ||
|
||
const { result } = renderHook( | ||
() => useMutation(CREATE_TODO_MUTATION, { errorPolicy: "ignore", onError }), | ||
{ wrapper: ({ children }) => ( | ||
<MockedProvider mocks={mocks}> | ||
{children} | ||
</MockedProvider> | ||
)}, | ||
); | ||
|
||
const createTodo = result.current[0]; | ||
let fetchResult: any; | ||
await act(async () => { | ||
fetchResult = await createTodo({ variables }); | ||
}); | ||
|
||
expect(fetchResult).toEqual({}); | ||
expect(onError).not.toHaveBeenCalled(); | ||
}); | ||
}); | ||
|
||
it('should return the current client instance in the result object', async () => { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎉