Multiple @apollo/client modules must reuse the same React context to consume from the provider when using useApolloClient or other hooks.
Actual outcome:
Since Apollo now creates a WeakMap in the module scope of @apollo/client this weak map is recreated for each individual module instances, the earlier approach was caching it on the React object which worked. I do not see how creating this Weakmap helps compared to just creating the React context as a module scoped singleton variable.
We are using a Microfrontend(MFE) architecture, where Apollo client is packaged individually with each MFE. Until 3.2.0 Apollo context was created and stored on the
React
module object on a symbol, but the behavior was changed with this change https://github.com/apollographql/apollo-client/commit/c46e02f580ea5bf4b7f7367f8bf23123a230827aIntended outcome:
Multiple
@apollo/client
modules must reuse the same React context to consume from the provider when usinguseApolloClient
or other hooks.Actual outcome:
Since Apollo now creates a
WeakMap
in the module scope of@apollo/client
this weak map is recreated for each individual module instances, the earlier approach was caching it on theReact
object which worked. I do not see how creating thisWeakmap
helps compared to just creating the React context as a module scoped singleton variable.Code here: https://github.com/apollographql/apollo-client/blob/main/src/react/context/ApolloContext.ts#L17
How to reproduce the issue:
@apollo/client
in an app. Create aApolloContext
Provider using one of the copies, and provide aclient
instance.useApolloClient
hook from another copy, to see that theclient
is undefined.Versions
3.4.0