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

Apollo client requires react ?!? #8958

Open
Stradivario opened this issue Oct 23, 2021 · 8 comments
Open

Apollo client requires react ?!? #8958

Stradivario opened this issue Oct 23, 2021 · 8 comments

Comments

@Stradivario
Copy link

Stradivario commented Oct 23, 2021

Hi guys!

Thank you for the great project it opens up many possibilities using graphql!

I am an early bird user from the moment that apollo introduced apollo-client!

I have created a package called @rxdi/graphql-client which helps me to wrap all of the `apollo-client logic and initialize it inside a dependency injection called @rxdi.

So far so good but i decided to upgrade apollo-client to a new 3.*.* version using @apollo/client package and i got a bundle error that says Cannot resolve dependency 'react'

  1 | import { invariant } from "../../utilities/globals/index.js";
> 2 | import * as React from 'react';
    |                       ^
  3 | import { getApolloContext } from "./ApolloContext.js";
  4 | export var ApolloProvider = function (_a) {
  5 |     var client = _a.client, children = _a.children;

So i have investigated a little bit and i found out that inside the barrel export 'index.ts' there is a line export * from './react'; i was shocked to see this so i follow it up and i found this file https://github.com/apollographql/apollo-client/blob/main/src/react/context/ApolloProvider.tsx

  1. So i am confused why react doesn't present as a dependency but it is required inside the code ?
  2. Why even there is a code related to a framework which tight couples the @apollo/client to react ?
  3. I now need to fork and remove all of this code in order to get the new version of @apollo/client does that sounds right ?
  4. Does now @apollo/client can be called as react framework for graphql so i can skip next time when i want to install something which was framework agnostic ?

I am not a React user i have my own framework build using Web Components if you want you can check it out using this starter https://github.com/rxdi/starter-client-side-lit-html

By the way this starter is using also apollo-client for interacting with Elon Musk Space X API

One way to mitigate this issue is to try to setup the @apollo/client not using the barrel export but instead a specific one by path. I have found that @apollo/client/core suits me well so wish me luck ! :)

Please don't get me wrong i like every framework but i decided to invest time and build my own stack and i am relying to a framework agnostic packages.

Cheers,
Kristiyan Tachev

@brainkim
Copy link
Contributor

brainkim commented Oct 28, 2021

This is something we’re trying to fix, but will have to wait til a major version release, as it would be a breaking change. For now, you will have to continue importing the client from the @apollo/client/core entry point. At some point, we do hope to isolate the different entry points so that other framework users do not have to import or download the React layer.

@jpvajda
Copy link
Contributor

jpvajda commented Oct 26, 2022

This could be an interesting consideration for AC 4.0, So i'll add it to our milestone for consideration. Cc @bignimbus

@milesj
Copy link

milesj commented Dec 22, 2022

Just ran into this while trying to use Apollo with Solid.

@gengns
Copy link

gengns commented Feb 16, 2023

Same here when using Apollo with Svelte.

@andreasonny83
Copy link

It is very annoying to have a peerDependency to React. Importing from @apollo/client/core does not really solve the problem since that module is still living under the @apollo/client package which has a React dependency.
The correct way to solve this problem will be to separate the @apollo/client/core under a dedicated @apollo/client-core library React agnostic.

@phryneas
Copy link
Member

@andreasonny83 react is only an optional peer dependency - your bundler should not force you to install it, only install it correctly in your node_modules folder structure in a way that is discoverable from Apollo Client if you already have react in your dependencies.

@andreasonny83
Copy link

@phryneas that is true, however the peerDependenciesMeta was only introduced by NPM v7 meaning that using older versions on NPM will result on an error when trying to consume a library with a dependency on Apollo Client.
Can I ask what is the reason for Apollo to keep the Core package under @apollo/client rather than exporting it to a separate npm package?

@phryneas
Copy link
Member

phryneas commented Jul 18, 2023

@andreasonny83 npm v7 shipped with node 15, which went End of Life 2 years ago. Node 16 (the oldest officially supported node version, which will receive support for one more month at this point) already ships with npm v8.
In one month, the oldest officially supported node version still receiving security updates will be node 18, which ships with npm 9.5.
I know things can't always be super up-to-date, but there has to be a limit how far back we can support things :/
Also, note that the two worst case scenarios here are that you either get a warning message that doesn't stop you from doing anything or install a package of a few kilobytes that will not make it into your bundle in the end.

Can I ask what is the reason for Apollo to keep the Core package under @apollo/client rather than exporting it to a separate npm package?

I'm sorry, but that decision predates me.
I would guess it was mainly to help users keep their packages up-to-date, since before @apollo/client you had to install about 10 different packages and had to keep all of them in sync, which was very cumbersome and lead to a lot of bugs and frustration.

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

7 participants