-
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
Implement InMemoryCache#modify for surgically transforming fields. #5909
Merged
Commits on Feb 10, 2020
-
Configuration menu - View commit details
-
Copy full SHA for 59d973e - Browse repository at this point
Copy the full SHA 59d973eView commit details -
Configuration menu - View commit details
-
Copy full SHA for 012f7cc - Browse repository at this point
Copy the full SHA 012f7ccView commit details -
Support delivering initial cache.watch results immediately, if reques…
…ted. I needed this functionality for some tests of cache.modify, but I think I can expand it into a version of cache.watch that returns an async iterator if you do not supply a callback.
Configuration menu - View commit details
-
Copy full SHA for 7287039 - Browse repository at this point
Copy the full SHA 7287039View commit details -
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.
Configuration menu - View commit details
-
Copy full SHA for a4cf135 - Browse repository at this point
Copy the full SHA a4cf135View commit details -
Configuration menu - View commit details
-
Copy full SHA for f168aea - Browse repository at this point
Copy the full SHA f168aeaView commit details -
Simplify StoreWriter#writeSelectionSetToStore using store.get.
Configuration menu - View commit details
-
Copy full SHA for 7c5d4fd - Browse repository at this point
Copy the full SHA 7c5d4fdView commit details -
Configuration menu - View commit details
-
Copy full SHA for 6fdab9d - Browse repository at this point
Copy the full SHA 6fdab9dView commit details -
Configuration menu - View commit details
-
Copy full SHA for bd25886 - Browse repository at this point
Copy the full SHA bd25886View commit details
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.