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 require react ?? #7318

Closed
yaquawa opened this issue Nov 12, 2020 · 18 comments
Closed

apollo-client require react ?? #7318

yaquawa opened this issue Nov 12, 2020 · 18 comments

Comments

@yaquawa
Copy link

yaquawa commented Nov 12, 2020

I got this error when compiling with WebPack 5.
I'm using vue, does this mean the apollo-client require react as a dependency?

 error  in ./node_modules/@apollo/client/react/context/ApolloConsumer.js

Module not found: Error: Can't resolve 'react' in '/Users/x/node_modules/@apollo/client/react/context'

 error  in ./node_modules/@apollo/client/react/hooks/useApolloClient.js

Module not found: Error: Can't resolve 'react' in '/Users/x/node_modules/@apollo/client/react/hooks'

 error  in ./node_modules/@apollo/client/react/hooks/utils/useBaseQuery.js

Module not found: Error: Can't resolve 'react' in '/Users/x/node_modules/@apollo/client/react/hooks/utils'
@baruchvlz
Copy link

It's a peer dependency.

@skourismanolis
Copy link

@yaquawa did you find a way to fix this without adding react as an (unnecessary) dependency?

@yaquawa
Copy link
Author

yaquawa commented Nov 17, 2020

@skourismanolis No, it requires react even if you don't use react at all... it really shouldn't force users to use react.

@baruchvlz
Copy link

baruchvlz commented Nov 17, 2020

@yaquawa Have you tried using VueApollo ?

@yaquawa
Copy link
Author

yaquawa commented Nov 17, 2020

@baruchvlz I'm using it now. it requires apollo-client.

@victorgarciaesgi
Copy link

Is there a way to remove react from the dependencies if we are not using it?

@skourismanolis
Copy link

@victorgarciaesgi I suppose you could fork the repo and remove the dependency from package.json but that's now a good long-term solution

What really must happen is since the apollo-client package is not supposed to be react-specific, any such functionality should be moved to a new package as a breaking change of a major update. But that's up to the apollo team and how they want to handle it.

@yaquawa
Copy link
Author

yaquawa commented Nov 25, 2020

This takes extra 298 kB if you don't use react. which is nonsense for non-react users.
The team really should program on interface but not the solid implementation of react, just give the user a chance to select the driver to be used.

@victorgarciaesgi
Copy link

@yaquawa I completly agree. I use Vue and I just updated to v3 and I want to switch back. Even for Node users this is nonsense

@cmd-johnson
Copy link

cmd-johnson commented Nov 26, 2020

I just stumbled across that same problem, but I got the client to work without having to install react.
Just import everything you need from @apollo/client/core instead of @apollo/client and you'll be good to go.

It'd be nice to have that documented somewhere, though.

@victorgarciaesgi
Copy link

@cmd-johnson I followed the docs here and imported everything from @apollo/client/core too and I still got the missing react error.

import { ApolloClient, InMemoryCache, ApolloLink } from '@apollo/client/core';
import { setContext } from '@apollo/client/link/context';
import apolloLogger from 'apollo-link-logger';
import { onError } from '@apollo/client/link/error';
import { createUploadLink } from 'apollo-upload-client';

Is there something i'm missing?

@victorgarciaesgi
Copy link

Ok I found it. For anyone having this issue the error comes from apollo-link-logger which required the entire @apollo/client

// node_modules/apollo-link-logger/es/index.js
var _client = require('@apollo/client');

I will open a PR

@yaquawa
Copy link
Author

yaquawa commented Nov 27, 2020

@victorgarciaesgi Thanks that helps!

@ryrych
Copy link

ryrych commented Dec 2, 2020

These changes made it run:

-import ApolloClient from 'apollo-client'
-import { ApolloLink, concat } from 'apollo-link'
-import { HttpLink } from 'apollo-link-http'
-import { split } from 'apollo-link'
-import { onError } from 'apollo-link-error'
-import { InMemoryCache, defaultDataIdFromObject } from 'apollo-cache-inmemory'
+import { ApolloClient } from '@apollo/client/core'
+import { ApolloLink, HttpLink, split, concat } from '@apollo/client/core'
+import { onError } from '@apollo/client/link/error'
+import { InMemoryCache, defaultDataIdFromObject } from '@apollo/client/cache'
-import gql from "graphql-tag"
+import { gql } from "@apollo/client/core"

package.json

-"apollo-boost": "^0.4.9",
-"apollo-cache-inmemory": "^1.6.6",
-"apollo-client": "^2.6.10",
-"apollo-link": "^1.2.14",
-"apollo-link-context": "^1.0.20",
-"apollo-link-http": "^1.5.17",
-"apollo-link-ws": "^1.0.20",
-"apollo-utilities": "^1.3.4",
-"graphql": "^15.3.0",
-"graphql-tag": "^2.11.0",
+"@apollo/client": "^3.3.2",
+"graphql": "^15.4.0",

@Yohandah
Copy link

Yohandah commented Dec 21, 2020

I'm having this issue as well using @apollo/client in my Angular project.

Importing from @apollo/client/core fixes the problem but I'm unsure why ... I think Apollo shouldn't rely on react since it's an optional peer dependency

@fenilli
Copy link

fenilli commented Mar 12, 2021

The problem is that @apollo/client exports from ./react and ./core.

It makes sense if the package is react friendly and as bonus works for other frameworks, but if the package intentions are to be framework free, it should only export from ./core and make react users import from @apollo/client/react instead.

@fenilli
Copy link

fenilli commented Mar 15, 2021

The problem is that @apollo/client exports from ./react and ./core.

It makes sense if the package is react friendly and as bonus works for other frameworks, but if the package intentions are to be framework free, it should only export from ./core and make react users import from @apollo/client/react instead.

After trying to find how to run a query and mutation using the docs, I found out that everything is based on react, so probably this package is meant to be used only with react, and as bonus it works for other frameworks too.

Sadly the docs are lacking on how to use the ApolloClient without using react hooks, also lacking in how to use the package without react at all ( where you get that you need to use @apollo/client/core is in the migration from 2.x, that makes no sense ).

So I would suggest to improve the documentation a little bit with a section on how to use this package without react and also on how to use queries, mutations and so on ( doesn't need to be focused on frontend frameworks, just on which method to use and its properties ).

Edit: There is a part of documentation on how to use the core apollo/core but it does need improvements, some examples would be helpful, actually good typescript support and not "any" everything and a better explaining about the types "TVariables" what is that? how it works?

I will be studying on how to use it better inside my project so I do undertand it more and will make a PR with hopefully better docs, making the types obvious.

@hwillson
Copy link
Member

Apollo Client 4 is going to remove React from the default @apollo/client entry point. We're tracking this here: #8190 - thanks!

storjBuildBot pushed a commit to storj/storj that referenced this issue Dec 13, 2022
This is insane but the problem was caused by the old apollo-client dependency which is 3 years old.
The error was coming from ts-invariant code here
https://github.com/apollographql/invariant-packages/blob/master/packages/ts-invariant/src/invariant.ts#L64

By this change I updated our app to use new @apollo/client dependency but there is one pretty big concern.
This lib has react as a peer dependency (what?!) so to avoid adding react to our vue project I had to import needed functionality from @apollo/client/core instead of just @apollo/client.

There is a discussion here apollographql/apollo-client#7318
According to this ^ apollo team are going to remove react from their peer dependecies list when apollo V4 will be launched.
See apollographql/apollo-client#8190

That's another point why we should move away from using graphql entirely or at least stop using apollo.

Issue:
#5371

Change-Id: Ifd484efbcd9e99d8c2e21d0dab93670c9358e25c
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 15, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants