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

useLazyQuery executes query twice the first time on 3.6.x #9655

Closed
Tracked by #9666 ...
dairyisscary opened this issue May 2, 2022 · 8 comments · Fixed by #9666 or #9684
Closed
Tracked by #9666 ...

useLazyQuery executes query twice the first time on 3.6.x #9655

dairyisscary opened this issue May 2, 2022 · 8 comments · Fixed by #9666 or #9684

Comments

@dairyisscary
Copy link

dairyisscary commented May 2, 2022

I have some code like this:

const [execute, { data, loading }] = useLazyQuery(query, {
  fetchPolicy: "network-only",
  nextFetchPolicy: "cache-first",
  notifyOnNetworkStatusChange: true,
});
const handleClick = () => execute({ variables: { something } });
return <button onClick={handleClick}>do it</button>;

I click the button (once) but see two queries in the network tab. If i click the button a second time, with new something, the query appears to only fire one more time, as expected

In my debugging, I found that this line is executing the query for the same variables and same options passed to useLazyQuery:

if (!equal(watchQueryOptions, currentWatchQueryOptions)) {

for me, the second extra query has this:

currentWatchQueryOptions === {
  fetchPolicy: "standby"
  initialFetchPolicy: "network-only"
  nextFetchPolicy: "cache-first"
  notifyOnNetworkStatusChange: true
  query: {}
  variables: { something }
};
// and
watchQueryOptions === {
  fetchPolicy: "network-only"
  nextFetchPolicy: "cache-first"
  notifyOnNetworkStatusChange: true
  query: {}
  variables: { something }
}

Hopefully this is helpful and not just a red herring :)

benjamn added a commit that referenced this issue May 3, 2022
This test started out as a regression test for PR #9665, but it revealed
a few other problems that I will fix in the next commit, potentially
solving issue #9655 as well.
brainkim pushed a commit that referenced this issue May 3, 2022
This test started out as a regression test for PR #9665, but it revealed
a few other problems that I will fix in the next commit, potentially
solving issue #9655 as well.
@benjamn benjamn self-assigned this May 3, 2022
@benjamn benjamn added this to the v3.6.x patch releases milestone May 3, 2022
@benjamn
Copy link
Member

benjamn commented May 3, 2022

@dairyisscary Can you try npm i @apollo/client@beta to install version 3.7.0-alpha.2? That version includes #9666, which I think may help with this issue. If that solves your problem, then we will release 3.6.3 with the same changes.

@benjamn benjamn linked a pull request May 3, 2022 that will close this issue
1 task
@dairyisscary
Copy link
Author

@benjamn thanks for getting back so quickly!

No, unfortunately, I can still reproduce on 3.7.0-alpha.2. Again, not sure if this is helpful at all but that !equal() check is not protecting the reobserve call in this case on the first re-render post-click.

@benjamn
Copy link
Member

benjamn commented May 4, 2022

@dairyisscary Can you try putting the relevant code into a runnable reproduction? I'll keep trying things on my side, but a reproduction helps a ton.

@dairyisscary
Copy link
Author

dairyisscary commented May 4, 2022

@benjamn https://github.com/dairyisscary/duplicate-lazy-query main branch has it

I struggled at first to reproduce it so I kept adding more and more of my configuration until I got it. it was the queryDeduplication: false on the Client constructor. sorry homie; completely forgot about this setting since its fire and forget. :)

I'm less certain this is a bug now but i will say on apollo 3.4.17 this does not duplicate the network request twice even with queryDeduplication turned off (I'm two releases behind, I never got to 3.5). if it is not a bug, what would you recommend benjamn? there doesn't appear to be a way to turn this on and off for an individual query -- I would turn it off for this particular query if it were possible.

@benjamn
Copy link
Member

benjamn commented May 4, 2022

This is definitely something we (still) want to fix @dairyisscary! Thanks for the reproduction. 🙌

@benjamn
Copy link
Member

benjamn commented May 5, 2022

@benjamn
Copy link
Member

benjamn commented May 5, 2022

@dairyisscary This should be fixed if you run npm i @apollo/client@next (to get version 3.6.3)!

@dairyisscary
Copy link
Author

amazing, seems to fix the issue in real app. thanks @benjamn!

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 15, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.