You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
Open the console tab of the dev tools in the browser
Click the "Make request" button and observe the client id logged to the console
Click the "Change client"
Click the "Make request" button again and observe the client id which has not changed but should have
The text was updated successfully, but these errors were encountered:
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
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.
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.
Issue Description
When changing the apollo client instance of the
ApolloProvider
context the refetch function provided by theuseLazyQuery
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:
Link to Reproduction
https://github.com/SimonMueller/apollo-client-context-change-lazy-query-bug-reproduction
Reproduction Steps
npm ci
to installnpm start
to start the appThe text was updated successfully, but these errors were encountered: