apollo-client icon indicating copy to clipboard operation
apollo-client copied to clipboard

`mergeOptions` in `useLazyQuery` not merging more complex variables properly

Open selrond opened this issue 5 months ago • 4 comments

Issue Description

mergeOptions is implemented in such a way, that more complex variables are not merged properly:

consider:

vars1 (defined at the hook level)

{
  "filter": {
    "field": {
      "eq": "whatever"
    }
  }
}

vars2 (defined at the useLazyQuery execute function call)

{
  "filter": {
    "otherField": {
      "eq": "something"
    }
  }
}

vars2 always overrides vars1, because it’s spread over it:

      variables: compact({
        ...(defaults && defaults.variables),
        ...options.variables,
      })

Link to Reproduction

Reproduction Steps

No response

@apollo/client version

3.13.8

selrond avatar Jun 17 '25 12:06 selrond

Hi @selrond,

variable merging for useLazyQuery will be completely removed in the upcoming Apollo Client 4.0 release(alphas are out, RC will be coming this week), so I'm sorry, but this is probably not something that we change fix in 3.x. (That said, it's quite intentional that this is not deep merging, that would cause a lot of unintended effects)

I would recommend that you change the code in preparation to 4.0 and pass the full merged variables into execute.

phryneas avatar Jun 17 '25 12:06 phryneas

Thanks for the heads-up!

selrond avatar Jun 17 '25 13:06 selrond

@phryneas hust curious - what kind of unintended side effects are we talking about?

Based on the docs, I'd (and I assume others as well) assume, that by "merging" the variables, it's actually merging them, not just shallowly spreading one after another (no notion of "shallow" mentioned in the docs).

It's quite confusing tbh

selrond avatar Jun 17 '25 15:06 selrond

what kind of unintended side effects are we talking about?

It becomes very hard to remove properties from defaults if you don't 100% know which ones will be there - you need that knowledge to explicitly override every single one of them with undefined, which is also a lot of work.

This has always been a shallow merge for sanity reasons.

phryneas avatar Jun 17 '25 15:06 phryneas

Hey @selrond 👋

We've released Apollo Client 4.0 with the upgraded behavior to useLazyQuery which removes the variable merging in favor of passing variables directly to the execute function. Please see the migration guide for more information on upgrading.

As such, I'm going to go ahead and close this issue. Thanks!

jerelmiller avatar Aug 26 '25 23:08 jerelmiller

Do you have any feedback for the maintainers? Please tell us by taking a one-minute survey. Your responses will help us understand Apollo Client usage and allow us to serve you better.

github-actions[bot] avatar Aug 26 '25 23:08 github-actions[bot]

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. For general questions, we recommend using our Community Forum or Stack Overflow.

github-actions[bot] avatar Sep 26 '25 00:09 github-actions[bot]