Updating the cache by update property still gives warning about Cache may be lost.
See original GitHub issueIntended outcome:
If I update the cache manually using the update, why would I need to to the same inside the type policies, It should not give a warning about it.
Actual outcome: I get a warning that the “Cache may be lost, use a merge function on type policies”, even tho I updated the cache manually using the update property on mutate.
How to reproduce the issue: Its hard to make a reproduction because I’m using Vue+Apollo Client, to get database information.
const cache = new InMemoryCache();
const GET_BUSINESS_GROUPS = gql`
query getBusinessGroups {
businessGroups: getBusinessGroups {
id
name
code
}
}
`;
const MUTATION = gql`
mutation deleteBusinessGroup ($id: ID!) {
businessGroup: deleteBusinessGroup (id: $id) {
id
name
code
}
}
`;
const updateCache = (cache, { data }) => {
const cachedQuery = cache.readQuery({ query: GET_BUSINESS_GROUPS });
if (cachedQuery && data) cache.writeQuery({ query: GET_BUSINESS_GROUPS, data: { businessGroups: cachedQuery.businessGroups.filter(({ id }) => id !== data.businessGroup.id) } });
cache.evict({ id: data.businessGroup.id, field: 'BusinessGroup' });
cache.gc();
}
Versions
System: OS: Linux 5.4 Debian GNU/Linux 10 (buster) 10 (buster) Binaries: Node: 16.5.0 - ~/.nvm/versions/node/v16.5.0/bin/node npm: 7.20.0 - ~/.nvm/versions/node/v16.5.0/bin/npm npmPackages: @apollo/client: ^3.3.19 => 3.4.5
Issue Analytics
- State:
- Created 2 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
cache.write warns "Cache data may be lost" when removing ...
Cache data may be lost when replacing the notifications field of a Query object. To address this problem (which is not a bug...
Read more >Refetching queries in Apollo Client - Apollo GraphQL Docs
The InMemoryCache helps you determine which active queries might have been invalidated by recent cache updates. Local cache updates and refetching work ...
Read more >Cache data may be lost when replacing the my field of a ...
Cache data may be lost when replacing the my field of a Query object. To address this problem (which is not a bug...
Read more >Fix: Cache data may be lost when replacing the getAllPosts ...
Fix: Cache data may be lost when replacing the getAllPosts field of a Query object in Apollo client. To fix this issue, simply...
Read more >Normalized Caching | urql Documentation
A query document may still ask the GraphQL API about what entity it's dealing ... In other words, normalized caches can build up...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Yes, but I just found out about the downside, when calling an update I’m not manually
cache.writeQuery
, and if it has a nested mutation change, It still needs to merge safely that nested information that was changed too, and if I was to do that withcache.writeQuery
I would need to manually cache the nested type on updates, and I can imagine cases where I would need to manually cache two queries at the same update, and that wouldn’t be the most viable experience, so type policies are a way of writing a “manual” cache that will be called when needed.One small note: if you have both a
merge
function and useoverwrite: true
, themerge
function will still be called, but with undefinedexisting
data, so themerge
function behaves as if you’re writing the data for the first time (more details in #7810). This can be surprising, but it gives themerge
function a chance to store the incoming data in whatever internal format it wants. If you don’t have amerge
function for the field, you shouldn’t have to worry about this.The downside of
overwrite: true
is that it only works for manual cache writes (and refetches), but that seems fine here because you’re callingcache.writeQuery
manually anyway.