-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Fix obscure warning in useFragment
when cache.identify
returns undefined
#12052
Conversation
🦋 Changeset detectedLatest commit: e712cc9 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
size-limit report 📦
|
/release:pr |
A new release has been made for this PR. You can install it with:
|
✅ Deploy Preview for apollo-client-docs ready!
To edit notification comments on pull requests, go to your Netlify site configuration. |
✅ Deploy Preview for apollo-client-docs ready!
To edit notification comments on pull requests, go to your Netlify site configuration. |
2bc7646
to
74674ec
Compare
src/react/hooks/useFragment.ts
Outdated
const stableOptions = useDeepMemo( | ||
// We default the `id` to a falsey string value to ensure `undefined` does | ||
// not get passed to cache.identify in cache.watchFragment. If that happens, | ||
// an obscure warning is displayed to the user | ||
// (https://github.com/apollographql/apollo-client/pull/12052). | ||
// | ||
// `ROOT_QUERY` will be used as the default `id` in `cache.diff` if `id` | ||
// is a falsey value. | ||
() => ({ ...rest, from: id || "" }), | ||
[rest, id] | ||
); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have to admit, I really don't like the empty string workaround.
Given that watchFragment
is still a very young api, could we maybe handle that over there, having it pass on an undefined
value?
typeof from === "undefined" ? from : typeof from === "string" ? from : this.identify(from)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Welp, I should have just kept my original changes 🤣. I moved it in 74674ec for a couple reasons:
- Our TypeScript types don't allow for
undefined
(technically) so this wasn't a supported use case. I believe you would have seen this already if you usedcache.watchFragment
directly and passedundefined
tofrom
(assuming you were using plain JS or ignored the TS errors) - We were using
id!
inuseFragment
which meant we were trying to circumvent what TS was already trying to warn us about (undefined
not being supported) so it felt like the fix needed to be on the side ofuseFragment
.
I'm fully willing to admit I'm overthinking this, but felt it a bit odd that our implementation was at odds with our supported types, hence this latest change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see the reasoning - but, tbh., I'd rather add a little comment to the !
- the "special" handling of ""
seems more like an accident to me and we might handle that differently at some point in the future, missing that we have to change this code path again.
That might be caused by something as trivial as replacing a ||
with a (logically much more fitting) ??
and trickle down in very weird ways.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds reasonable enough. Will revert the last commit and add an additional comment for the !
74674ec
to
b4e5568
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me now!
Fixes #12051
#12020 did some refactoring to
useFragment
to be more compatible with Rules of React (specifically not writing toref
in render). In the process,useFragment
now callscache.identify
directly to ensure that whole data objects passed tofrom
don't trigger rewatches when non key fields change to that object. This change however introduced a regression where invalid identifiers (i.e.cache.identify
returnsundefined
) would show a warning such as:This was a result of taking that
undefined
and passing it as the value tocache.watchFragment
, which itself calledcache.identify(undefined)
and this triggered the warning.This change ensures that
cache.watchFragment
forwardsundefined
tocache.watch
asid
since it usesROOT_QUERY
as a fallback in that case.