Empty object instead of undefined when using cache redirect and cache-only fetch policy
See original GitHub issueHello! 👋
In our app we have cache redirects for fields using typePolicies
and toReference
. Since version 3.4.8 the values the cache returns for these fields has changed during loading or if the reference is not found. The issue happens only when we use a query with fetchPolicy
set to cache-only
.
Intended outcome:
The data
from the userQuery
hook should return undefined
during loading or if the reference cannot be found.
Actual outcome:
The data
from the userQuery
hook returns and empty object {}
during loading or if the reference cannot be found.
How to reproduce the issue: I’ve recreated the issue on CodeSandbox: https://codesandbox.io/s/eager-mahavira-ie04m?file=/src/index.jsx
I’ve modified your template by:
- Adding a new
Pet
type - Added a nested
pets
field to thePerson
type - Changed the
ALL_PEOPLE
query to includepets
- Added a new
SINGLE_PET
query that allows you to get aPet
item by id - Used the new
SINGLE_PET
query with thefetchPolicy
set tocache-only
- Added a cache redirect for
pet
usingtypePolicies
andtoReference
Versions 3.4.8 and newer
Similar closed issue https://github.com/apollographql/apollo-client/issues/6368
Is this an intentional change or a regression?
Thank you for all the amazing work you’ve done with Apollo!
Žiga
Issue Analytics
- State:
- Created 2 years ago
- Reactions:9
- Comments:5
Top GitHub Comments
If it help this is an issue for us as well.
@Antarases A possible workaround: In our app, adding an if statement inside the type policy read function to check if the reference has an
id
field solved the issue.For example: