-
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
[Data masking] Warn when passing object to useFragment
/watchFragment
from
that is not identifiable
#12004
Conversation
|
src/cache/core/cache.ts
Outdated
|
||
if (!id) { | ||
invariant.warn( | ||
"Could not identify object passed to `from` either because the object is non-normalized or the key fields are missing. If you are masking this object, please ensure the key fields are requested by the parent object." |
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.
Looking at this, this might be a bit too generic and be hard to find what is triggering this warning. Perhaps we add the fragment name in here as well?
Should we also try and differentiate between watchFragment
and useFragment
, or would adding in the fragment name be enough?
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.
Fragment name added in fd66d42
src/cache/core/cache.ts
Outdated
@@ -245,10 +245,17 @@ export abstract class ApolloCache<TSerialized> implements DataProxy { | |||
): Observable<WatchFragmentResult<TData>> { | |||
const { fragment, fragmentName, from, optimistic = true } = options; | |||
const query = this.getFragmentDoc(fragment, fragmentName); | |||
const id = typeof from === "string" ? from : this.identify(from); | |||
|
|||
if (!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.
This might make sense as a dev-only warning
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.
Dev-only warning added in 885be47
expect(console.warn).toHaveBeenCalledWith( | ||
"Could not identify object passed to `from` for '%s' fragment, either because the object is non-normalized or the key fields are missing. If you are masking this object, please ensure the key fields are requested by the parent object.", | ||
"UserFields" | ||
); |
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.
Interesting question: do we want to auto-unmask key fields?
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'd like to avoid that since it might be surprising behavior and instead just recommend that someone selects from the parent entity instead. Definitely something we can keep an eye on and consider down the road!
…nt` `from` that is not identifiable (#12004)
…nt` `from` that is not identifiable (#12004)
…nt` `from` that is not identifiable (#12004)
…nt` `from` that is not identifiable (#12004)
Closes #11675
Adds a warning when passing an object to the
from
property that is not identifiable from the cache (i.e.cache.identify
returnsundefined
). This also adds some tests to check that objects returned from masked queries can be read fromwatchFragment
anduseFragment
.