Hey there,
I've been working on a React Native app for a while now with great success using Apollo Client. I do most of my debugging during development using the iOS simulator, and check the app on the Android emulator now and again to make sure things are working as intended.
I have configured Apollo Client per the docs (basically just setting the networkInterface
and wrapping my app in the ApolloProvider
) and it's been working great on iOS. But when I booted up my Android emulator for my weekly check-up on the android side of things, all of my queries and mutations are resulting in:
Network error: Network request failed
My networkInterface is being pointed at: http://localhost:3000/graphql
This works on iOS, but Android can't seem to communicate with it. After changing my network interface to a hosted endpoint (i.e. https://staging.app.com/graphql
), it worked. It seems Android can't communicate with localhost
by default.
So, two things
1) What can I do to enable communication with localhost
during development? Is there something I can add to AndroidManifest.xml
?
2) Once we figure it out, this would be a great addition to the docs.
Pretty sure just using the ip address of your computer instead of localhost will fix it. This has nothing to do with react-apollo btw :)
@zackify thank you! This thread also had relevant information: https://github.com/facebook/react-native/issues/10404
OMG! I could just kick myself...repeatedly! I know this! I know this! I know this! So whyyyyyyy didn't this occur to me hours ago?! Ug!
Thanks for posting this question so I can get back to work.
const api = new Frisbee({
baseURI: 'http://10.0.2.15:9000',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
}
});
help me with this ,,getting network request failed
So if you are going to use the android emulator, through android studio, using this uri...
http://10.0.2.2:3000/
works for me.. running on expo and through a physical device, need to track down your local ip address
http://${ip.address}:3000/
@matthew-gordon - Tried your solution still does not work.
I had the same issue.
Below is what worked for me
I used this link to get my local IP address https://lifehacker.com/how-to-find-your-local-and-external-ip-address-5833108
import React from 'react';
import RootComponent from './src/RootComponent';
import {ApolloProvider} from '@apollo/react-hooks';
import {ApolloClient} from 'apollo-client';
import {InMemoryCache} from 'apollo-cache-inmemory';
import {createHttpLink} from 'apollo-link-http';
const LOCAL_SYSTEM_IP_ADDRESS = '192.168.178.241';
const PORT = '5000';
const client = new ApolloClient({
//note: this is the local IP address of the system
link: createHttpLink({
uri: `http://${LOCAL_SYSTEM_IP_ADDRESS}:${PORT}/graphql`,
}),
cache: new InMemoryCache(),
});
const App = () => {
return (
<ApolloProvider client={client}>
<RootComponent />
</ApolloProvider>
);
};
export default App;
@azeezat in your local API just replace your IP address with 10.0.0.0/your port number...
@azeezat in your local API just replace your IP address with 10.0.0.0/your port number...
This didn't work. The solution I posted above is what worked for me
Hi guys, I'm having the same exact problem with Expo + React Native + Apollo Client + graphql-yoga-server. I read this issue so I managed to make my graphql endpoint reachable from "the external world" with nat and no-ip. Anyways, from expo server, if I click "Run in Browser", everything works correctly, if I run the app in android emulator or via the expo app on my smartphone I get a network error, with the following error stack:
`Network error: Network request failed
Most helpful comment
So if you are going to use the android emulator, through android studio, using this uri...
http://10.0.2.2:3000/
works for me.. running on expo and through a physical device, need to track down your local ip address
http://${ip.address}:3000/