React Native Apollo 错误:“网络错误:网络请求失败";
在 IOS 上,应用程序运行正常.但在 Android 上,我收到此错误.这是我在客户端和服务器中的配置.请帮忙!
On IOS, the application runs correctly. But on Android I get this error. Here's my config in client and server. Please help!
错误:错误图片
这是客户端上的配置:
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,
});
这是服务器上的配置:
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
I'm using react-apollo: 1.4.10, apollo-client: 1.9.0-0
基本上,您必须将 localhost 替换为您机器的 IP 地址.该问题已在此处重新报告,https://github.com/apollographql/apollo-client/issues/1757
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
要检查您的 IP 地址,请参阅此视频 https://www.youtube.com/看?v=TRFtQzx0Y0M
To check your IP address, refer to this video https://www.youtube.com/watch?v=TRFtQzx0Y0M