apollographql / apollo-client

:rocket:  A fully-featured, production ready caching GraphQL client for every UI framework and GraphQL server.
https://apollographql.com/client
MIT License
19.32k stars 2.65k forks source link

Multiple apollo-client instances will not reuse the same Apollo Context in React #8790

Closed ashubham closed 2 years ago

ashubham commented 2 years ago

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/c46e02f580ea5bf4b7f7367f8bf23123a230827a

Intended outcome:

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.

Code here: https://github.com/apollographql/apollo-client/blob/main/src/react/context/ApolloContext.ts#L17

image

How to reproduce the issue:

Versions

3.4.0

benjamn commented 2 years ago

@ashubham You make a fair point about the pointlessness of the WeakMap.

Please have a look at #8798 when you have a chance!