-
Notifications
You must be signed in to change notification settings - Fork 787
Refactor ApolloProvider/ApolloConsumer implementation to use React 16.3 context API #2540
Refactor ApolloProvider/ApolloConsumer implementation to use React 16.3 context API #2540
Conversation
@wzrdzl: Thank you for submitting a pull request! Before we can merge it, you'll need to sign the Meteor Contributor Agreement here: https://contribute.meteor.com/ |
CI for Preact will fail until developit/preact#963 is resolved |
….3 context API The old way of passing the context still exists side-by-side with the new way to allow gradual migration of <Query>, <Mutation> and other modules that access the context in the "old" way. This should unlock "user space" experiments with react hooks and react-apollo because if yoou want to access the context(client) in a hook, the context has to be exposed using the "new" context API closes apollographql#1937
b3e70c7
to
02ba729
Compare
@jbaxleyiii what shall we do about Preact? Wait until developit/preact/pull/963 is merged or add a some handling for the case when createContext is not a function? |
@janek26 @jbaxleyiii I don't think this whole PR should be postponed just because auf preact-compatability. The blocking PR you mentioned preactjs/preact#963 has had no activity since July.. Preact and React versions will probably diverge even more in the future so we might need a different solution here doesn't block the other one :/ |
Would love to see this merged as well. Right now it's basically necessary to have a custom Provider in the tree if I want to consume the ApolloClient eg. by hooks. I agree that waiting for Preact is not feasible in a long run with all that sweetness of Suspense and Hooks, it's unlikely they will be able to catch up soon enough. Perhaps |
maybe creating a different branch and publishing the alpha-version of this package supporting react-hooks and suspense will satisfy all the stakeholders, and consequently save the time in the main release when preact supports new context api? We can't wait every time on two upstream repos especially when other projects including react actively publish alpha packages. Moreover, the preact merge is dependent on reactjs/rfcs#2 to be approved. And, we all have past experiences of how slow such processes can be. Even in case of new context API, react implemented it in 16.3 while the proposal is still in process, and react 16.3 was released on March 30 2018 and it is November now. |
Why not just use the create-react-context polyfill? It works well and the preact tests pass when using it. |
@arianon That could be also a way, but only if it can be tree shaken well enough. It's not a good idea to bloat build just to support some React clone :) |
@FredyC It's small enough (1.78kB min+gzip) to keep react-apollo under its current |
Maybe bloating on alpha version will be an acceptable tradeoff? 😬 |
To me, an acceptable tradeoff is not to support Preact in alpha version if they do not care about it (assuming from 4 months old issue). |
@FredyC i agree with you. But, i guess it's more of business issue. As big enterprise client might be using preact in production and supporting them is essential priority for Apollo. Apollo revenue strategy seems to be focused on high traffic enterprise client. But, sacrificing DX which brings enterprise customers is somewhat awkward. Although, i am a fan of and use Apollo server and react Apollo for the quality DX that Apollo stack provides. But, may be sending this feature request from our company email ids' can be helpful 🤐 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for working on this @wzrdzl!
The meat of this PR is solid, but it would be easier to accept if it didn't involve adding a new npm dependency (memoize-one
). If it's really important to avoid creating more than one child context object per client, perhaps there's a more direct way to implement that behavior, like sticking a special property on the client, which is something we're already doing with the operations
map? Alternatively, I think I would be fine with this PR without any memoization.
As for Preact, perhaps there's a way for this code to detect if Preact is being used (e.g. |
Is there an update about the current state of the PR @wzrdzl regarding @benjamn concerns to avoid the additional dependency? Otherwise I would like to take a look to ensure context migration works in StrictMode 😄 |
A note on https://github.com/alexreardon/memoize-one/blob/master/src/index.js (@benjamn @wzrdzl) |
Until Preact X is released, the current non-alpha version of Preact does not support React's new Context API (https://reactjs.org/docs/context.html). To allow us to move forward with supporting the Context API, this commit leverages `create-react-context` to make Preact happy. The bundle size impact of adding this dependency in is negligible (about 50 bytes after gzip). This polyfill can be removed when (if) we update to using Preact X.
I agree with @benjamn regarding the removal of memoization, and until Preact X is launched (which does support These changes have been made, so we should be close to getting this merged. Let me know if anyone objects - thanks! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we're all set here - LGTM!
Wouldn’t we need to also export ApolloContext in |
@mattfwood The Edit: Made the PR ... #2872 |
Just a heads-up: after bumping to 2.5.3 which uses this, I started to get console errors about This seems to have come from Just an FYI for anyone else stuck on this. |
As it's not gonna be exposed, here's the workaround I came up with: #2872 (comment) |
The old way of passing the context still exists side-by-side with the new way to allow gradual migration of
<Query>
,<Mutation>
and other modules that access the context in the "old" way.So this change is completely backwards compatible
This should unlock experiments with React Hooks version of
react-apollo
API (#2539) because if you want to access the context(client) in a hook, the context has to be exposed using the "new" context APIcloses #1937