apollographql / apollo-client

:rocket:  A fully-featured, production ready caching GraphQL client for every UI framework and GraphQL server.
https://apollographql.com/client
MIT License
19.34k stars 2.66k forks source link

UseFragment producing a warning during store misses (TypeError: Cannot read properties of undefined) #12051

Closed alex-e-leon closed 3 weeks ago

alex-e-leon commented 4 weeks ago

Issue Description

Since @apollo/client 3.11.5 I've started receiving warnings in the console during store misses of useFragment in development: TypeError: Cannot read properties of undefined (reading '__typename')

A bit of testing suggests that this happens in all my uses of useFragment during store misses. Note that although I get the warning, everything continues to work fine. @apollo/client 3.11.4 did not have this issue.

In case it's relevant I'm using the following arguments on useFragment: fragment, fragmentName, from: {__typename: X, id: X}

The full stack trace is:

TypeError: Cannot read properties of undefined (reading '__typename')
    at Policies2.identify (policies.ts:360:14)
    at InMemoryCache2.identify (inMemoryCache.ts:350:28)
    at ApolloCache2.watchFragment (cache.ts:232:50)
    at useFragment.ts:105:36
    at subscribeToStore (react-dom.development.js:16139:10)
    at commitHookEffectListMount (react-dom.development.js:23189:26)
    at invokePassiveEffectMountInDEV (react-dom.development.js:25193:13)
    at invokeEffectsInDev (react-dom.development.js:27390:11)
    at commitDoubleInvokeEffectsInDEV (react-dom.development.js:27369:7)
    at flushPassiveEffectsImpl (react-dom.development.js:27095:5)

Link to Reproduction

None

Reproduction Steps

Call useFragment on any fragment which hasn't been loaded into the FE cache/store by typename + id.

@apollo/client version

3.11.5

jerelmiller commented 4 weeks ago

Hey @alex-e-leon!

You beat me to opening this 😆. I discovered this just recently working with the data masking feature. We did some rework with useFragment to ensure it follows the Rules of React (specifically not writing to ref in render), but I just found out that causes this warning. I know exactly where the issue is coming from so I'll get a PR out for this very soon (hopefully today). So sorry about the additional noise!

alex-e-leon commented 4 weeks ago

This is the punishment I get for staying up to date with all the latest deps : )

And thanks @jerelmiller - its great to get such fast feedback and see that you're already on top of it!

jerelmiller commented 4 weeks ago

Not on you 😆. We unfortunately had a missing test so we didn't see the regression on our end.

I just opened https://github.com/apollographql/apollo-client/pull/12052 which should fix the issue. Could you try this snapshot release to see if the warning goes away?

npm i @apollo/client@0.0.0-pr-12052-20240903200316
github-actions[bot] commented 3 weeks ago

Do you have any feedback for the maintainers? Please tell us by taking a one-minute survey. Your responses will help us understand Apollo Client usage and allow us to serve you better.

alex-e-leon commented 3 weeks ago

@jerelmiller - sorry, I've been super busy this week, so probably don't have time to test the branch right now. But looks like you guys are on top of it. Thanks for the quick response and looking forward to seeing it in the next release!

jerelmiller commented 3 weeks ago

You're welcome! I just got it released so you can to upgrade to 3.11.7 and the warning should disappear. Thanks again for reporting the issue!