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 doesn't change refetch function when the apollo client instance of the ApolloProvider changes #11096

Open
SimonMueller opened this issue Jul 25, 2023 · 4 comments

Comments

@SimonMueller
Copy link

Issue Description

When changing the apollo client instance of the ApolloProvider context the refetch function provided by the useLazyQuery hook does not change and therefore an outdated version of the apollo client is used to make further requests.

It works as expected and the refetch function changes when using useQuery in the same way.

In our case the problem popped up when refreshing the auth tokens which we send in an http link for each request. After refreshing and using useLazyQuery we still see the old tokens as the refetch function is using the old apollo client and therefore the old auth link.

Versions:

  • @apollo/client: 3.7.17
  • graphql: 15.5.0

Link to Reproduction

https://github.com/SimonMueller/apollo-client-context-change-lazy-query-bug-reproduction

Reproduction Steps

  1. Run npm ci to install
  2. Run npm start to start the app
  3. Open the app in the browser
  4. Open the console tab of the dev tools in the browser
  5. Click the "Make request" button and observe the client id logged to the console
  6. Click the "Change client"
  7. Click the "Make request" button again and observe the client id which has not changed but should have
@SimonMueller SimonMueller changed the title useLazyQuery doesn't change refetch function when the apollo client of the ApolloProvider changes useLazyQuery doesn't change refetch function when the apollo client instance of the ApolloProvider changes Jul 25, 2023
@phryneas
Copy link
Member

Good point, I can see that in our code - we should take a look at that.

Independently of that, I would really recommend against creating a new ApolloClient instance just because you changed an authentication token.
You should keep the same ApolloClient instance over the lifetime of your application.
Depending on how you created the client in the first place, you can usually share a variable in scope with the setContext Link and just modify that - so the link always uses the latest token available without having to recreate everything.

@SimonMueller
Copy link
Author

wow that was a quick response! 😄

and thanks for your note on the instance creation. makes total sense not only to minimize rerenders as the ApolloProvider is usually on the top of the hierarchy. Thats exactly how we fixed our problem. By using a reference to the latest token and not creating a new apollo client instance everytime.

I thought it would still be good to report it. 👍

@phryneas
Copy link
Member

Absolutely, thanks a ton for the bug report!

@dyst5422
Copy link
Contributor

dyst5422 commented Oct 2, 2023

Can confirm, using a reference is the way to go here - wildly simple and effective

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants