react-native-fullstack-graphql icon indicating copy to clipboard operation
react-native-fullstack-graphql copied to clipboard

Conditionally set HttpLink URI

Open DevanB opened this issue 6 years ago • 6 comments

There is an issue with fetch in React Native that prevents the usage of localhost.

A proposed solution is to use Platform.select to set the ApolloClient's HttpLink uri to one of several options:

const SERVER_URL = __DEV__
  ? Platform.select({
      ios: 'GET_USERS_IP_ADDRESS_AND_APPEND_PORT_4000',
      android: 'http://10.0.2.2:4000'
    })
  : 'USE_PRODUCTION_GRAPHQL_SERVER_ENV_VAR';

Android apparently allows 10.0.2.2 to equal localhost, but I haven't confirmed this myself.

DevanB avatar Feb 14 '18 17:02 DevanB

Alright will handle this asap

juicycleff avatar Feb 26 '18 15:02 juicycleff

Well android does not really, except you are using an emulator. For real device its different, it is based on the ip of the wifi connection which changes every time it restarts. But I will have a fix for that

juicycleff avatar Feb 26 '18 15:02 juicycleff

Is 127.0.0.1 supported instead, @DevanB?

marktani avatar Feb 26 '18 16:02 marktani

@marktani - apparently, no. It has to do with proxying through with Expo.

DevanB avatar Feb 27 '18 03:02 DevanB

@DevanB did you end up finding a solution to this?

devomacdee avatar Mar 27 '18 15:03 devomacdee

@devomacdee unfortunately, no. I haven’t necessarily worked on it much too. The crux of the solution I proposed is figuring out the developer’s IP address when the env is development (don’t worry about production since that should be a separate url for a server).

DevanB avatar Mar 27 '18 16:03 DevanB