Skip to content

Commit 57e1c1a

Browse files
committed
docs: add instructions for enabling data masking types in Apollo Client
1 parent adafd87 commit 57e1c1a

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

packages/docs/data/data-masking.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Data masking prevents components from accessing GraphQL fields they didn't explicitly request. This creates loosely coupled components that are more resistant to breaking changes.
44

55
::: tip Recommended: Use with GraphQL Codegen
6-
Data masking works best with [GraphQL Codegen](https://the-guild.dev/graphql/codegen) for type-safe masked types. See the [TypeScript page](/data/typescript) for setup instructions.
6+
Data masking works best with [GraphQL Codegen](https://the-guild.dev/graphql/codegen) for type-safe masked types. See the [TypeScript page](/data/typescript) for setup instructions, including the required [type augmentation](/data/typescript#enabling-data-masking-types) to make masked types work correctly.
77
:::
88

99
## The Problem
@@ -208,7 +208,14 @@ const client = new ApolloClient({
208208
})
209209
```
210210

211-
### 3. Refactor Components
211+
### 3. Configure TypeScript (if applicable)
212+
213+
If you're using TypeScript with GraphQL Codegen, you need to:
214+
215+
1. Update your codegen config to generate masked types (see [TypeScript setup](/data/typescript#configuration))
216+
2. Create the type augmentation file (see [Enabling Data Masking Types](/data/typescript#enabling-data-masking-types))
217+
218+
### 4. Refactor Components
212219

213220
Gradually refactor components to use `useFragment` and remove `@unmask` directives:
214221

packages/docs/data/typescript.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,32 @@ The `graphql()` function:
179179
- Returns a `TypedDocumentNode` with full type inference
180180
- Automatically includes fragment definitions
181181

182+
### Enabling Data Masking Types
183+
184+
By default, Apollo Client doesn't modify operation types regardless of whether they are masked or unmasked. To use GraphQL Codegen's masking format with your operation types, you need to tell Apollo Client to use the associated GraphQL Codegen masking types.
185+
186+
Create a TypeScript declaration file (e.g., `apollo-client.d.ts`) in your project:
187+
188+
```ts
189+
// This import is necessary to ensure all Apollo Client imports
190+
// are still available to the rest of the application.
191+
import '@apollo/client'
192+
import type { GraphQLCodegenDataMasking } from '@apollo/client/masking'
193+
194+
declare module '@apollo/client' {
195+
interface TypeOverrides extends GraphQLCodegenDataMasking.TypeOverrides {}
196+
}
197+
```
198+
199+
This extends Apollo Client's `TypeOverrides` interface with the GraphQL Codegen data masking types, ensuring that:
200+
- Masked types don't include fields from fragment spreads
201+
- The `@unmask` directive properly unmasks types
202+
- `FragmentType` works correctly for type-safe fragment props
203+
204+
::: tip
205+
Make sure TypeScript can find this declaration file. It should be in a location covered by your `tsconfig.json`'s `include` patterns.
206+
:::
207+
182208
### Type-Safe Fragments
183209

184210
Use `FragmentType` from `@apollo/client` to type component props that receive fragment data:

0 commit comments

Comments
 (0)