-
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
Added 'Customizing Type Policies' Segment in cache-configuration.mdx #10933 #10967
Merged
Merged
Changes from 3 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
7ee85e2
Update cache-configuration.mdx - Added 'Customizing Type Policies' S…
sahilmanchanda1999 0072d22
Merge branch 'apollographql:main' into bugfix/10933
sahilmanchanda1999 35f75e4
Updated 'Customizing Type Policies' Segment
sahilmanchanda1999 0184c6d
Add language to code block
jerelmiller edd3727
Adjust verbiage on intro paragraph
jerelmiller c5a8089
Update code comment for accuracy
jerelmiller e23c6ce
Add space after code block
jerelmiller d898f06
Merge branch 'main' into bugfix/10933
phryneas 23aac85
Merge branch 'main' into bugfix/10933
phryneas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
|
@@ -216,6 +216,46 @@ 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 Apollo client object, you can use the `addTypePolicy` method to add or modify type policies. | ||
|
||
Here is an example of how to use the `addTypePolicy` method: | ||
|
||
``` | ||
const cache = new InMemoryCache({ | ||
typePolicies: { | ||
Person: { | ||
fields: { | ||
name: { | ||
read(name = "UNKNOWN NAME") { | ||
return name.toUpperCase();; | ||
} | ||
}, | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
// Add a type policy to the client object. | ||
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. | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
@StephenBarlow would you mind taking a look at this section and provide any suggestions you may have?