Markus Hayner
Markus Hayner

Reputation: 2959

React Native [Network error]: TypeError: Network request failed using Apollo Client

I just want to get connected on my Apollo Server on localhost using React Native with Expo.

If I try to use the following api link https://q815p14lp.lp.gql.zone/graphql its working fine. But if I try to use http://127.0.0.1:8080/graphql or http://localhost:8080/graphql I get the following error: [Network error]: TypeError: Network request failed

This is my code where I try to get connected.

const httpLink = new HttpLink({ uri: 'http://127.0.0.1:8080/graphql' });

const authLink = setContext(async (_, { headers }) => {
  const token = await getToken();

  return {
    headers: {
      ...headers,
      authorization: token ? `Bearer ${token}` : null,
    }
  };
});

const errorLink = onError(({ networkError, graphQLErrors }) => {
  if (graphQLErrors)
    graphQLErrors.map(({ message, locations, path }) =>
      console.log(
        `[GraphQL error]: Message: ${message}, Location: ${locations}, Path: ${path}`
      )
    );
  if (networkError) console.log(`[Network error]: ${networkError}`);
})

const client = new ApolloClient({
  link: ApolloLink.from([errorLink, authLink, httpLink]),
  cache: new InMemoryCache()
});

"apollo-boost": "^0.1.18",
"apollo-link-context": "^1.0.9",
"apollo-link-error": "^1.1.1",
"expo": "^30.0.1",
"react-apollo": "^2.2.4",

Any idea what am I doing wrong?

Upvotes: 2

Views: 3551

Answers (1)

Jesper We
Jesper We

Reputation: 6097

The host *.lp.gql.zone is a Launchpad graphql server. So you have your server running on Launchpad, not on your local machine.

Also connecting to your local machine using Expo does not make a lot of sense. The "localhost" in this case is the phone device that runs the app.

I guess what you really want is for the Expo app on the device to connect to your development graphql server on your mac/pc? In this case you should use the IP address you see in the debug view:

enter image description here

Your connect url in this case becomes: http://10.10.9.70:8080/graphql

Upvotes: 5

Related Questions