Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Native Apollo error: "Network error: Network request failed"

On IOS, the application runs correctly. But on Android I get this error. Here's my config in client and server. Please help!

Error: Error image

Here's the config on client:

import ApolloClient, { createNetworkInterface } from 'apollo-client';
import { SubscriptionClient, addGraphQLSubscriptions } from 'subscriptions-transport-ws';

const networkInterface = createNetworkInterface({ uri: 'http://localhost:3000/graphql' });

const wsClient = new SubscriptionClient('ws://localhost:3000/subscriptions', {
  reconnect: true,
});

const networkInterfaceWithSubscriptions = addGraphQLSubscriptions(
  networkInterface,
  wsClient,
);

export const client = new ApolloClient({
  networkInterface: networkInterfaceWithSubscriptions,
});

Here's the config on server:

import express from 'express';
import {
  graphqlExpress,
  graphiqlExpress,
} from 'graphql-server-express';
import bodyParser from 'body-parser';
import cors from 'cors';
import { execute, subscribe } from 'graphql';
import { createServer } from 'http';
import { SubscriptionServer } from 'subscriptions-transport-ws';
import { schema } from './schema';

const PORT = 3000;
const server = express();

server.use('*', cors({ origin: 'http://localhost:8081' }));
server.use('/graphql', bodyParser.json(), graphqlExpress({ schema }));
server.use('/graphiql', graphiqlExpress({
  endpointURL: '/graphql',
  subscriptionsEndpoint: 'ws://localhost:3000/subscriptions',
}));

// We wrap the express server so that we can attach the WebSocket for subscriptions
const ws = createServer(server);
ws.listen(PORT, () => {
  console.log('GraphQL Server is running');
  // Set up the WebSocket for handling GraphQL subscriptions
  new SubscriptionServer({
    execute,
    subscribe,
    schema
  }, {
    server: ws,
    path: '/subscriptions',
  });
});

I'm using react-apollo: 1.4.10, apollo-client: 1.9.0-0

like image 251
linhnh Avatar asked Aug 02 '17 08:08

linhnh


People also ask

Why is React Native not working on my Android app?

When making an HTTP Request with React Native, there is some kind of conflict because your Android app is run on an emulator which uses localhost too, and instead of sending the request to the localhost on your computer, it sends the request to the phone itself but with a different port and that is why you are getting this error.

How to solve network request failed error?

Start your app as usual but don’t forget to give an IP address and a port, this will help you solve the Network Request Failed error. NOTE: Replace 10.10.10.2 with your own IP Address, this is just an example. And on your mobile app, make sure to use the correct URL in your request.

How do I handle network errors in Apollo client?

When a network error occurs, Apollo Client adds it to the error.networkError field returned by your useQuery call (or whichever operation hook you used). You can add retry logic and other advanced network error handling to your application with Apollo Link.

Why can't I call HTTP URL from React Native fetch?

you have to do below changes on your info.plist to allow unsecure url You are trying to call HTTP URL from react-native fetch but getting Network request failed to issue or in ios working fine then below the solution for you only. This has to do with Android not trusting my SSL certificate.


2 Answers

Basically, you have to replace lochalhost with your machine's IP address. The issue was reporeted here, https://github.com/apollographql/apollo-client/issues/1757

To check your IP address, refer to this video https://www.youtube.com/watch?v=TRFtQzx0Y0M

like image 70
Luc Abayo Avatar answered Sep 24 '22 02:09

Luc Abayo


I had the same problem and I found my solution here https://github.com/apollographql/apollo-client/issues/1757

Basically you need to use your computer's ip-address in the createNetworkInterface function, so change this:

const networkInterface = createNetworkInterface({ uri: 'http://localhost:3000/graphql' });

to this:

const networkInterface = createNetworkInterface({ uri: 'http://YOURIPADDRESS:3000/graphql' });
like image 33
Yellowhill Avatar answered Sep 25 '22 02:09

Yellowhill