-
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
Provide options.mergeObjects for easy and correct recursive merging. #5885
Conversation
This comment has been minimized.
This comment has been minimized.
const merged: any[] = []; | ||
const authors = new Map<string, any>(); | ||
function add(author: any) { | ||
merge(existing: any[], incoming: any[], { readField, mergeObjects }) { |
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.
Is there any way to easily share complicated examples like this between the documentation and the tests? @trevorblades @hwillson
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.
@benjamn Great question - I know when @trevorblades added MDX support to the docs, he also opened up the possibility of requiring/importing shared docs. I'm not sure if we can reach outside of the docs project to pull from the AC codebase though, so I'll defer to Trevor for an answer.
@hwillson Any idea what's up with that Codecov report? #5885 (comment) @StephenBarlow Tagged you for review because this involved additional changes to the most recent docs about merging non-normalized data. Happy to answer questions tomorrow / next week! |
This comment has been minimized.
This comment has been minimized.
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.
Looks great @benjamn - thanks!
One of the benefits of #5880 is that any custom
merge
function gets to see itsincoming
data before any nestedmerge
functions have been run, somerge
functions have full control over how the contents of their input arguments get combined, in principle.In practice, it's actually pretty tricky to implement a correct custom
merge
function that might encounter any non-normalized data, because even non-normalized objects can have custommerge
functions for their fields, which must be called.We've been (briefly) recommending that people ignore this edge case and simply return
{ ...existing, ...incoming }
when combining non-normalized objects, so that no fields are lost. However, this style ignores the possibility that theexisting
andincoming
objects might have totally different__typename
s, risks combining{ __ref }
objects with non-reference objects, and does not invoke any nestedmerge
functions. A rat's nest of edge cases!This PR introduces a general-purpose
options.mergeObjects
helper function to enable easy and correct merges ofexisting
andincoming
values. It also comes in handy when merging specific elements from two arrays, which is something the cache cannot do automatically.