question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Updating the cache by update property still gives warning about Cache may be lost.

See original GitHub issue

Intended 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:closed
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
GustavoFenillicommented, Sep 14, 2021

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 with cache.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.

1reaction
benjamncommented, Sep 13, 2021

One small note: if you have both a merge function and use overwrite: true, the merge function will still be called, but with undefined existing data, so the merge function behaves as if you’re writing the data for the first time (more details in #7810). This can be surprising, but it gives the merge function a chance to store the incoming data in whatever internal format it wants. If you don’t have a merge 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 calling cache.writeQuery manually anyway.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found