Skip to content
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

Merged
merged 6 commits into from
Jan 31, 2020

Conversation

benjamn
Copy link
Member

@benjamn benjamn commented Jan 31, 2020

One of the benefits of #5880 is that any custom merge function gets to see its incoming data before any nested merge functions have been run, so merge 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 custom merge 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 the existing and incoming objects might have totally different __typenames, risks combining { __ref } objects with non-reference objects, and does not invoke any nested merge functions. A rat's nest of edge cases!

This PR introduces a general-purpose options.mergeObjects helper function to enable easy and correct merges of existing and incoming values. It also comes in handy when merging specific elements from two arrays, which is something the cache cannot do automatically.

@codecov

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 }) {
Copy link
Member Author

@benjamn benjamn Jan 31, 2020

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

Copy link
Member

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.

@benjamn
Copy link
Member Author

benjamn commented Jan 31, 2020

@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!

@benjamn benjamn added this to the Release 3.0 milestone Jan 31, 2020
@benjamn

This comment has been minimized.

@benjamn benjamn changed the title Provide options.merge for more correct recursive merging. Provide options.mergeObjects for easy & correct recursive merging. Jan 31, 2020
@benjamn benjamn changed the title Provide options.mergeObjects for easy & correct recursive merging. Provide options.mergeObjects for easy and correct recursive merging. Jan 31, 2020
Copy link
Member

@hwillson hwillson left a 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!

@benjamn benjamn merged commit 7024720 into master Jan 31, 2020
@benjamn benjamn deleted the field-policy-options.merge-helper branch January 31, 2020 16:40
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 16, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants