apollo-client: Data masking with fragment that should never be masked

Moved from https://github.com/apollographql/apollo-client/issues/11666#issuecomment-2518099996

What happens if a fragment is expected to NOT be masked, always? I have some fragment on types that don’t have clear ids, so I can’t useFragment on it.

So far I’ve been using useFragment from GraphQL Codegen client preset (which I renamed unmaskFragmentData) in the component using the fragment In that case it is expected that, while the typings are masked, the data is available at runtime to be unmasked.

I can go put @unmask everywhere that fragment is spread, but that’s error-prone. I’m fiddling with a custom directive @disableMasking on the FragmentDefinition to ensure all places using that fragment are using @unmask and are not using @nonreactive Now I’m hitting a typings issue because it unmasks the fragment, effectively making all the data available top-level, but I really only want it to be available on the component.

How can I make sure that the data will be available at runtime, but is masked in the typings? I basically want @unmask at runtime, but it shouldn’t be there for typings codegen

Right now, my approach is to enforce the @unmask directive for those fragment spreads and during codegen I strip them before running typescript-operations and leave them for the runtime (at some point because I’m not ready to try apollo data masking)

Note that I’m working with a heavily customized fork GraphQL Codegen client preset to make it work with Apollo client more natively

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 16 (16 by maintainers)

Most upvoted comments

That would be amazing! Hope all goes well when you do start using it! Appreciate the conversation!

Phew 🎉. For a second I thought 3.12.1 might have to be on its way 😆

Urgg I just saw the setIsSubmitting in my screenshot and I got so sad… please ignore that I’m fixing it to use const [revertAuditEntry, { loading: isSubmitting} ] = useRevertAuditEntryMutation() hahaha

To make sure I’m understanding this case, is it safe to assume you’re passing data as a prop to a child component here? And what you’re asking for is to use @unmask because the data is non-normalized, but you want to make sure to enforce that only the child component is the only one that can access the fragment fields by ensuring the unmasked data in the parent is not available in that type. Does that sound right?

Correct

Perhaps a small code example would be helpful. I’m not quite sure I’m following the structure here between the fragments and your components.

Yup! Give me a bit of time to get good examples in a readable format and I’ll post it here