-
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.
Implement InMemoryCache#modify for surgically transforming fields.
The cache.writeQuery and cache.writeFragment methods do a great job of adding data to the cache, but their behavior can be frustrating when you're trying to remove specific data from a field. The typical cycle of reading data, modifying it, and writing it back into the cache does not always simply replace the old data, because it may trigger custom merge functions which attempt to combine incoming data with existing data, leading to confusion. For cases when you want to apply a specific transformation to an existing field value in the cache, we are introducing a new API, cache.modify(id, modifiers), which takes an entity ID and an object mapping field names to modifier functions. Each modifier function will be called with the current value of the field, and should return a new field value, without modifying the existing value (which will be frozen in development). For example, here is how you might remove a particular Comment from a paginated Thread.comments array: cache.modify(cache.identify(thread), { comments(comments: Reference[], { readField }) { return comments.filter(comment => idToRemove !== readField("id", comment)); }, }); In addition to the field value, modifier functions receive a details object that contains various helpers familiar from read/merge functions: fieldName, storeFieldName, isReference, toReference, and readField; plus a sentinel object (details.DELETE) that can be returned to delete the field from the entity object: cache.modify(id, { fieldNameToDelete(_, { DELETE }) { return DELETE; }, }); As always, modifications are applied to the cache in a non-destructive fashion, without altering any data previously returned by cache.extract(). Any fields whose values change as a result of calling cache.modify invalidate cached queries that previously consumed those fields. As evidence of the usefulness and generality of this API, I was able to reimplement cache.delete almost entirely in terms of cache.modify. Next, I plan to eliminate the foot-seeking missile known as cache.writeData, and show that cache.modify can handle all of its use cases, too.
- Loading branch information
Showing
3 changed files
with
101 additions
and
69 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
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
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