我只想使用React Native和Expo来连接本地主机上的Apollo服务器。
如果我尝试使用以下api链接https://q815p14lp.lp.gql.zone/graphql
可以正常工作。但是,如果我尝试使用http://127.0.0.1:8080/graphql
或http://localhost:8080/graphql
,则会出现以下错误:[Network error]: TypeError: Network request failed
这是我尝试建立连接的代码。
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",
知道我在做什么错吗?