-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added 'Customizing Type Policies' Segment in cache-configuration.mdx #…
…10933 (#10967) Co-authored-by: Jerel Miller <[email protected]> Co-authored-by: Lenz Weber-Tronic <[email protected]>
- Loading branch information
1 parent
dc4baed
commit ae2651a
Showing
1 changed file
with
41 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -234,6 +234,47 @@ This code also has the following drawbacks: | |
* It does nothing to protect against undefined object properties. | ||
* Accidentally using different key fields at different times can cause inconsistencies in the cache. | ||
|
||
### Customizing Type Policies | ||
|
||
After creating an `InMemoryCache` instance, you can use the `addTypePolicies` method to add or modify type policies. | ||
|
||
Here is an example of how to use the `addTypePolicies` method: | ||
|
||
```ts | ||
const cache = new InMemoryCache({ | ||
typePolicies: { | ||
Person: { | ||
fields: { | ||
name: { | ||
read(name = "UNKNOWN NAME") { | ||
return name.toUpperCase();; | ||
} | ||
}, | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
// Add a type policy to the cache. | ||
cache.policies.addTypePolicies({ | ||
Person: { | ||
fields: { | ||
email: { | ||
read(email = "[email protected]") { | ||
return email; | ||
}, | ||
}, | ||
}, | ||
}, | ||
}); | ||
``` | ||
|
||
The code creates an `InMemoryCache` with a custom type policy for the `Person` type. The type policy specifies that if the `name` field is not available in the cache, it should return a default value of "UNKNOWN NAME" and converts it to uppercase. | ||
|
||
Then, the code adds an additional type policy to the cache using `cache.policies.addTypePolicies`. This new type policy is related to the `Person` type and its `email` field. The custom read function specifies that if the email field is not available in the cache, it should return a default value of "[email protected]". | ||
|
||
Overall, the code sets up caching behaviors for the `Person` type, ensuring that default values are provided for the `name` and `email` fields if they are not present in the cache. | ||
|
||
### Disabling normalization | ||
|
||
You can instruct the `InMemoryCache` _not_ to normalize objects of a particular type. This can be useful for metrics and other transient data that's identified by a timestamp and never receives updates. | ||
|