-
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
Add data masking to fragments read by useFragment
/watchFragment
#12018
Conversation
|
f60b539
to
c7eb755
Compare
useFragment
/watchFragment
useFragment
/watchFragment
useFragment
/watchFragment
useFragment
/watchFragment
Moving to draft until I've moved this to work with the |
c7eb755
to
e10e358
Compare
e10e358
to
dcb805a
Compare
9569f52
to
b54c22f
Compare
b54c22f
to
51c0110
Compare
@@ -394,6 +419,27 @@ export abstract class ApolloCache<TSerialized> implements DataProxy { | |||
return maskOperation(data, document, this.fragmentMatches.bind(this)); | |||
} | |||
|
|||
public maskFragment<TData = unknown>(options: MaskFragmentOptions<TData>) { |
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.
Apologies ahead of time. This has been moved in #12029, but wanted to do that together with maskOperation
, so its in the other PR.
const { fragment, fragmentName } = options; | ||
|
||
const observable = this.cache.watchFragment(options); | ||
let latestResult: WatchFragmentResult<TFragmentData> | undefined; | ||
|
||
return new Observable((observer) => { | ||
const subscription = observable.subscribe({ | ||
next: (result) => { | ||
result.data = this.queryManager.maskFragment({ | ||
fragment, | ||
fragmentName, | ||
data: result.data, | ||
}); |
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.
Hmm. This works, too, I guess.
My assumption while we were discussing this was more along the lines of "masking happens in the cache, but is not enabled by default there".
So, when calling cache.watchFragment
, you can pass along a dataMasking
option, but it's false
by default - and coming from ApolloClient
, it would be this.queryManager.dataMasking
by default.
So the implementation here would be more along the lines of
const { dataMasking = this.queryManager.dataMasking, ...rest } = options
return cache.watchFragment({...rest, dataMasking})
Of course, implementing it like this works, too. But if someone were to use a cache without a fragment masking implementation, this would bloat it a little 🤔.
Up to you to make the final call here :)
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 initially had something like this, but #12029 moves these methods out of the base cache class. I left it like this to prepare for that PR, but I'm realizing now how I can achieve essentially what you're suggesting here but without the class methods.
I'd prefer to keep this option private if possible, so I might bury this option in a symbol so that cache.watchFragment
doesn't have a public option for data masking (we can always add as a public option later, but I'm trying to be strict right now about where/when its added until we finalize the rules on that).
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'll actually go implement this refactor in #12029 since I've moved these methods out of Cache
in that PR and can better address along with those changes.
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.
Closes #12022
Adds data masking to
watchFragment
anduseFragment
.