首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >React Native Apollo错误:"Network error: Network request failed“

React Native Apollo错误:"Network error: Network request failed“
EN

Stack Overflow用户
提问于 2017-08-02 16:46:51
回答 4查看 10.7K关注 0票数 7

在IOS上,应用程序运行正常。但在Android上,我得到了这个错误。这是我在客户端和服务器端的配置。请帮帮我!

错误:Error image

以下是客户端的配置:

代码语言:javascript
复制
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,
});

下面是服务器上的配置:

代码语言:javascript
复制
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',
  });
});

我使用的是react-apollo: 1.4.10,apollo-client: 1.9.0-0

EN

回答 4

Stack Overflow用户

发布于 2018-03-15 19:07:05

基本上,您必须将lochalhost替换为您计算机的IP地址。此问题已在此处报告,https://github.com/apollographql/apollo-client/issues/1757

要检查您的IP地址,请参阅此视频https://www.youtube.com/watch?v=TRFtQzx0Y0M

票数 7
EN

Stack Overflow用户

发布于 2017-09-03 01:42:51

我也有同样的问题,我在这里找到了我的解决方案https://github.com/apollographql/apollo-client/issues/1757

基本上,您需要在createNetworkInterface函数中使用计算机的ip地址,因此请更改以下内容:

代码语言:javascript
复制
const networkInterface = createNetworkInterface({ uri: 'http://localhost:3000/graphql' });

要这样做:

代码语言:javascript
复制
const networkInterface = createNetworkInterface({ uri: 'http://YOURIPADDRESS:3000/graphql' });
票数 4
EN

Stack Overflow用户

发布于 2018-06-07 17:24:00

由于我是在模拟器上工作,因此将localhost更改为127.0.0.1对我很有效:

代码语言:javascript
复制
const link = new HttpLink({ uri: 'http://127.0.0.1:3500/graphql' });

const client = new ApolloClient({
  link,
  cache
})
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45455877

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档