This repository was archived by the owner on Apr 13, 2023. It is now read-only.
Invoke onCompleted/onError even if Mutiation unmounts #2710
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.
/related #2293 (Query onCompleted not invoked)
See #2293 (comment), #2293 (comment), #2293 (comment), and #2293 (comment)
The issue
When passing both
onCompleted
andrefetchQueries
to aMutation
that un-mounts as a result ofrefetchQueries
,onCompleted
will never be called.Consider the following example:
A logout button that invokes a mutation, which re-fetches a query to propagate this change to the rest of the app (e.g. removing references to the currently logged-in user).
On that mutation, we have an
onCompleted
to redirect to the login page after both a successful logout mutation AND a successful refetch .With the current implementation,
onCompleted
will never be called because as soon asrefetchQueries
is finished, the logout mutation will unmount and the redirect will never happen.This might not be the best example, but hopefully it can demonstrate the real issue the PR is trying to address.
Replicating the Issue
I was able to replicate this issue easily when I tried to pass an
onCompleted
callback to the Mutation in this test and assert that it was called:The Cause
It appears that this is intentional in order to avoid updating the Mutation internal state after it un-mounts.
react-apollo/src/Mutation.tsx
Lines 244 to 247 in cce02a3
Unless I'm missing something here or there is a reason not do so, this doesn't necessarily mean that an
onCompleted
callback should never be called when a mutation is resolved and has been unmounted.While it can be hard to think that an unmounted component would have callbacks called, it is important to keep in mind that consumers of an unmounted Mutation have no indication to when the said mutation is resolved successfully or when its refetched queries are finished (same for errors).
Possible Fix
By changing the logic of
onMutationCompleted
a little bit, it looks like it's possible to invoke the user-providedonComplete
even when theMutation
unmounts.I also went ahead and applied the same changes to
onError
/onMutationError
for consistency.