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.

Empty object instead of undefined when using cache redirect and cache-only fetch policy

See original GitHub issue

Hello! 👋

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 the Person type
  • Changed the ALL_PEOPLE query to include pets
  • Added a new SINGLE_PET query that allows you to get a Pet item by id
  • Used the new SINGLE_PET query with the fetchPolicy set to cache-only
  • Added a cache redirect for pet using typePolicies and toReference

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:open
  • Created 2 years ago
  • Reactions:9
  • Comments:5

github_iconTop GitHub Comments

4reactions
lxcidcommented, Nov 23, 2021

If it help this is an issue for us as well.

1reaction
ziga-mikliccommented, Jan 3, 2022

@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:

typePolicies: {
  Query: {
    fields: {
      user(existingData, { args, toReference, readField }) {
        // Search and return data if it already exists.
        // https://www.apollographql.com/docs/react/caching/advanced-topics/#cache-redirects-using-field-policy-read-functions
        const reference = toReference({ __typename: 'User', id: args?.id });
        // Check if reference is not empty (regression from Apollo client since 3.4.8)
        const referenceData = reference && readField('id', reference) ? reference : undefined;

        return existingData || referenceData;
      }
    }
  }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Reading and writing data to the cache - Apollo GraphQL Docs
If the cache is missing data for any of the query's fields, readQuery returns null . It does not attempt to fetch data...
Read more >
javascript - How to programmatically empty browser cache?
I am doing this because the application caches confidential data and I'd like to remove those when you press "log out". This would...
Read more >
fetch() - Web APIs | MDN
The promise resolves to the Response object representing the response to your request. A fetch() promise only rejects when a network error is ......
Read more >
Delete Multiple Records and Reports - Salesforce Help
You can delete multiple reports or records at the same time.Required Editions and User Permissions Available in: both Salesforce Classic (not available in....
Read more >
RFC 2616: Hypertext Transfer Protocol -- HTTP/1.1
1. If the Request-URI is encoded using the "% HEX HEX" encoding [42], the origin server MUST decode the Request-URI in order to...
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