Description
Issue Description
This one took quite a while to get a minimal reproduction for since a lot of things have to be true for it to happen so I'll try to explain it all step by step as well as attaching a Codesandbox. If you:
- have a query that uses
@include
with a variable that is set through a query-level default - and which also uses
@nonreactive
somewhere in it - and you're calling it with
useQuery
- and you change the variables to something that doesn't include the variable that is used in the
@include
- and you then evict a field of the query using
cache.evict
- and you then call
cache.gc
- then Apollo errors with a "Invalid variable referenced in @include directive" error, even though the variable has a valid default
This bubbles up as an unhandled error that can't be caught by either try/catch
around the cache.gc()
call or React error boundaries, which made it completely crash out our React Native app.
I know a little bit of what is happening here, observable.reobserve() in useResubscribeIfNecessary sets new variables on the observable but doesn't apply query defaults to them because that logic is in the QueryManager rather than the observable. This normally doesn't cause any problems AFAICT but if the query has a nonreactive directive then the Observable has to process the selection sets with the new variables, which are missing their defaults. I'm not super sure why this only happens if cache.gc
is called, though, and I don't immediately know how I'd refactor things to fix this in a PR so I'm just reporting it for now.
Link to Reproduction
https://codesandbox.io/p/devbox/upbeat-bush-mwmqx6?workspaceId=03104bf9-16b8-430d-a030-08b693449298
Reproduction Steps
Click "Change variables" and then "Evict cache" in the CodeSandbox, see the uncaught error in the console
@apollo/client
version
3.10.5