Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
ziga-miklic opened this issue Nov 17, 2021 · 5 comments
Assignees

Comments

@ziga-miklic
Copy link

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
#6368

Is this an intentional change or a regression?

Thank you for all the amazing work you've done with Apollo!

Žiga

@lxcid
Copy link

lxcid commented Nov 23, 2021

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

@Antarases
Copy link

Hi!

Is there any update concerning this issue?
I've noticed that it happens in all versions between 3.4.8 and 3.5.6.

Now I rolled back to 3.4.7.
Maybe somebody has other solution? Would like to know about it. 🙂

@ziga-miklic
Copy link
Author

@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;
      }
    }
  }
}

@lxcid
Copy link

lxcid commented May 7, 2022

Bumping this up for visibility, making it hard for us to upgrade to latest.

@lxcid
Copy link

lxcid commented Aug 19, 2022

Are we gonna address this issue or should we expect empty object when dealing with cached situation now?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants