apollographql/apollo-client

Merging array in DeepMerger

hartantothio opened this issue · 3 comments

Issue Description

I'm looking at this line and I think it's a problem when I have the following data for source and target:

// New data
source: []

// Previous data
target: [{...}]

My expectation is that the data will be replaced with source, which is the most recent data. But in this case, because Object.keys([]) returns an empty array, it will returns target instead.

Link to Reproduction

n/a

Reproduction Steps

No response

@apollo/client version

3.9.5

Hey @hartantothio 👋

I'm a bit unclear what you're looking for here. Are you seeing issues with DeepMerger currently that need fixing? Are you using DeepMerger directly in some code that isn't performing correctly? I'd love to get some more context on the outcome you're looking for.

Hi @jerelmiller -- sorry about that. Hopefully the following is helpful.

I have a GQL query using the defer directive (doesn't seem to happen if I remove the defer). But, the problematic field isn't part of the the fragment being deferred. It's just the issue seems to be only when I turned on the defer directive on the query.

Example query:

fragment ObjectFragment on Object {
  id
  randomKey {
    someData
  }
}

fragment ObjectSlowFields on Object {
  id
  slowField
}

query TestQuery {
  object {
    ...ObjectFragment
    ...ObjectSlowFields @defer
  }
}

Initially, the returned results might looked like this:

{
    "data": {
        "object": {
            "randomKey": {
                "someData": [1, 2, 3]
            }
        }
    }
}

However, on subsequent fetch, if the server returned an [] for someData, I'd expect the final "merged" data for someData to be [].

This sounds like it's related to the failing test in #11374 which I had started investigating. Will take a closer look today.