Merging Objects

How can we merge data (two objects that contain an array)? I can be able to achieve simple caching and merging the existing and incoming. However, if I am merging them and there are changes with incoming, it is not replacing the data.

For example, here is my existing data

{
“__typename”: “Result”,
“data”: {
“__typename”: “Items”,
“id”: “1”,
“label”: “Test Label”,
“items”: [
{
“__typename”: “Product”,
“productid”: “1”,
“description”: “Main Description”,
“color”: “green”
}
]
}
}

And this is the incoming data

{
“__typename”: “Result”,
“data”: {
“__typename”: “Items”,
“id”: “1”,
“label”: “Test Label”,
“items”: [
{
“__typename”: “Product”,
“productid”: “1”,
“description”: “__Main Description”,
“color”: “blue”
}
]
}
}

And this is how I am handling the merging:

Is there any proper way to do this? Because there are other keys that might change aside from description and color.

Thanks

@hodormiso have you seen the Merging arrays section of our docs? In your case I would consider creating a new field policy just for the items field, to handle the array merging in a separate merge function. The docs show an example of this.