2017-08-02 179 views
1

在IOS上,應用程序運行正常。但在Android上我得到這個錯誤。這是我在客戶端和服務器上的配置。請幫忙!React Native Apollo錯誤:「網絡錯誤:網絡請求失敗」

錯誤: Error image

下面是關於客戶端的配置:

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', 
    }); 
}); 

我使用的反應 - 阿波羅:1.4.10, apollo-client:1.9.0-0

回答

0

我有同樣的問題,我發現我的解決方案在這裏https://github.com/apollographql/apollo-client/issues/1757

基本上你需要在createNetworkInterface功能使用您的計算機的IP地址,所以更改此:

const networkInterface = createNetworkInterface({ uri: 'http://localhost:3000/graphql' }); 

這樣:

const networkInterface = createNetworkInterface({ uri: 'http://YOURIPADDRESS:3000/graphql' }); 
+0

我試圖設置IP地址它正在使用真實設備。但在Android模擬器上,它不起作用。 – linhnh

+0

啊,是的,抱歉,我忘了提及我使用的是真實設備。 – Yellowhill