apollographql / apollo-client

:rocket:  A fully-featured, production ready caching GraphQL client for every UI framework and GraphQL server.
https://apollographql.com/client
MIT License
19.38k stars 2.66k forks source link

[bug?] refetch() resolves to undefined after component is unmounted #11752

Open AlexLMeow opened 7 months ago

AlexLMeow commented 7 months ago

Issue Description

Async functions might end up calling refetch() after the component containing useQuery is unmounted. When this happens, await refetch() resolves to undefined, breaking any downstream code that works on the refetch result.

The typescript return value is Promise<ApolloQueryResult<TData>> so undefined is unexpected.

Seems to only occur when ApolloClient is set up with assumeImmutableResults: true.

I also have some evidence that this can occur even when the component is still mounted, but I couldn't pin down the exact scenario. Let me know if you find another trigger for this bug so i can go back and validate my application behavior accordingly.

Link to Reproduction

https://codesandbox.io/p/devbox/affectionate-pike-gx8ww7

Reproduction Steps

Click the button in the repro

@apollo/client version

My app is on ~3.4.17 Repro is on 3.9.9