Skip to content

Commit 1fbb1d6

Browse files
Docs: Clarify use case and give examples for merge: false (#12815)
Co-authored-by: Jerel Miller <[email protected]>
1 parent 5433669 commit 1fbb1d6

File tree

1 file changed

+32
-10
lines changed

1 file changed

+32
-10
lines changed

docs/source/caching/cache-field-behavior.mdx

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -570,22 +570,44 @@ const cache = new InMemoryCache({
570570
});
571571
```
572572

573-
### Disabling `merge` functions
573+
### Opting in to the default behavior for non-normalized fields
574574

575-
In some cases, you might want to completely disable merge functions for certain fields. To do so, pass `merge: false` like so:
575+
Apollo Client replaces existing data with incoming data for non-normalized fields by default. When this happens, you will encounter console warnings like "Cache data may be lost when...", even when the default behavior is desirable. You can tell Apollo Client you want this behavior by passing `merge: false` to a field's `FieldPolicy`. By opting into this behavior, the console warning is no longer emitted:
576576

577-
```ts {13}
577+
```ts {6}
578578
const cache = new InMemoryCache({
579579
typePolicies: {
580-
Book: {
580+
Author: {
581581
fields: {
582-
// No longer necessary!
583-
// author: {
584-
// merge: true,
585-
// },
586-
},
587-
},
582+
books: {
583+
merge: false,
584+
}
585+
}
586+
}
587+
},
588+
});
589+
```
590+
591+
In some cases, you might want the same behavior for all occurrences of a particular type. To do so, pass `merge: false` to the type policy for that type like so:
588592

593+
```ts {19}
594+
const cache = new InMemoryCache({
595+
typePolicies: {
596+
// No longer necessary!
597+
// Article: {
598+
// fields: {
599+
// author: {
600+
// merge: false,
601+
// },
602+
// },
603+
// },
604+
// Book: {
605+
// fields: {
606+
// author: {
607+
// merge: false,
608+
// },
609+
// },
610+
// },
589611
Author: {
590612
merge: false,
591613
},

0 commit comments

Comments
 (0)