apollo-client: Could not find "client" in the context of Apollo(MyComponent). Wrap the root component in an
Hello, Using expo/react-native, I get this error when I try make a graphql-query from a modal. I.E the component only gets mounted when you click on a link [the modal slides in]. The graphql-query seems to work in any other component.
my root component is wrapped in a ApolloProvider
<ApolloProvider client={client}>
<View style={styles.container}>
<Navigator navigatorViews={navigatorViews} routeConfig={routeConfig} />
</View>
</ApolloProvider>
I’m not quoting my query code as it works fine outside the modal, is there some gotchas for components that are mounted dynamically ?
Intended outcome: Execute graphql query from a component that is mounted dynamically
Actual outcome: see error above
How to reproduce the issue: Make graphql query from a component that is mounted in dynamically
Version
- apollo-client@^1.9.1
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 13
- Comments: 43
I got this error when trying to use
@apollo/react-hooks
The solution as suggested by @sampathsris is to wrap your app with both
ApolloProvider
from both the new hooks lib and the originalreact-apollo
libEdit: This does not seem to work anymore. Move along. But sure, go visit the link if you’re curious.
In case you got this error while using
react-apollo-hooks
, you may want to take a look at this sandbox: https://codesandbox.io/s/vnr2lqvrm0.@ahmedbrandver I fixed my issue, what was happening to me was that I using the hooks from both
@apollo/client
and@apollo/react-hooks
. What I did to fix it was remove the@apollo/react-hooks
library and just import the hooks or anything GraphQL related from@apollo/client
.Why does this solution work? it feels wrong to have to import two different providers? can someone explain please.
Had the same issue, aparently you cant use tags and hooks at the same time. If you use hooks, dont use tags. Also, ApolloProvider should be imported from @apollo/react-hooks instead of “react-apollo”
Source: https://spectrum.chat/apollo/react-apollo/error-using-hooks-beta-could-not-find-client~777fcc0e-d590-44f1-99e1-d709a7df5c35?m=MTU2MDMzNjkyNTA3NQ==
Wrapping the test in with
<MockedProvider>
solved it for me using:import { MockedProvider } from "@apollo/client/testing";
I am using the newer version ApolloV3
same here. i’m on version
3.3.2
.it actually only happens on certain components for me. I have the whole app wrapped with the
ApolloProvider
though.Following these instructions: https://www.apollographql.com/docs/react/get-started/ I was dismayed when the error “Uncaught ReferenceError: client is not defined” showed up – and I had exactly copied their code…
It took a while before I realized that they left out the client definition in their ApolloProvider example…
It works once you bring the client definition in:
cheers, Al;
I am using react-apollo-hooks and had the components set up just like the sandbox (Root, not BuggyRoot), but still got the error.
Reverting back to 2.5.2 ‘fixed’ it for now. https://github.com/apollographql/react-apollo/issues/2900
I had this issue when I introduced server side rendering to an app. Here’s how the problematic code was setup:
I got the error that is in the title of the issue. I fixed it by updating my code to this:
Hopefully that helps someone.
Is anyone else running into this when trying to run the official docs demo for hooks? From what I understand, everything has been now bundled into
@apollo/client
. Is that correct? I’m running"@apollo/client": "^3.0.2""
Tried with react-apollo too, no luck 😦 @ptimson
Used all the version combinations from 3.0.0 to 3.1.x same error msg " Unhandled Rejection (Invariant Violation): Could not find “client” in the context or passed in as an option. Wrap the root component in an <ApolloProvider>, or pass an ApolloClient instance in via options."
Doc says we should install @apollo/react-hooks and import ApolloProvider, none of this are working,
I am just trying to setup hello world project for apollo client 😦 any one have boilerplate or working version combinations, Please share it.
Thanks!
In my use case, this issue was caused by duplicate copies of
@apollo/client
in my bundle. The fix was to force webpack to resolve a single copy of the module:I have used the latest version following the official docs with “@apollo/client”: “^3.2.3”, and I went through the same problem.
@Kallirroi I am also currently attempting this with
"@apollo/client": "^3.0.2",
and after going through this thread, I’m more confused than ever. I got to this error when attempting to use the useLazyQuery hook, and after attempting the recipe offered by @ptimson I had no luck. Please report back if you find anything as this appears to still cause some issues.This might work
rm -rf node_modules package-lock.json
npm install
Maybe there is some issue with npm cache, if it still doesn’t work try
npm cache clean --force
Note: this will clean all your your node dependencies, however the package.json file is not deleted. Use these commands at own risk.
2.5.2 worked for me too. But I didn’t used react-apollo, solved it by creating HOC on hooks for classes)).
And then use it:
i guess you can now just use
<ApolloConsumer>
@oakmad I know this isn’t helpful in any way, but I just started running into this issue today and I have no idea why. I’ll report back if I figure it out, but it’s definitely nothing to do with conditional rendering.
EDIT: It was after lunch and my brain wasn’t working, all I had to do was mock the provider because it was failing tests but running correctly in the app.
@thierryskoda @oakmad Sorry guys, i’ve not revisited this yet. But a work around would be to either import the client again https://stackoverflow.com/questions/40634872/do-i-need-to-bind-to-a-component-to-use-the-apollo-react-client-even-if-it-has-n
or use HOC from apollo http://dev.apollodata.com/react/higher-order-components.html#withApollo