我正在尝试使用端点和订阅端点连接服务器和客户端。 我正在关注appolo文档。我不知道哪里错了。
import React, { Component } from 'react';
import { Platform, StyleSheet, Text, View } from 'react-native';
import ApolloClient from "apollo-boost";
import gql from 'graphql-tag';
import { ApolloProvider } from "react-apollo";
import { WebSocketLink } from 'apollo-link-ws';
import { SubscriptionClient } from 'subscriptions-transport-ws';
import { HttpLink } from 'apollo-link-http';
import Navigator from './app/Navigator'
import { createStackNavigator, createAppContainer } from 'react-navigation';
const httpLink = new HttpLink({
uri: 'https://api.graph.cool/simple/v1/cjrt2a37a5y4d0151eizuzi50'
})
const WsLink = new WebSocketLink({
uri: 'wss://subscriptions.us-west-2.graph.cool/v1/cjrt2a37a5y4d0151eizuzi50',
options: {
reconnect: true
}
})
const client = new ApolloClient(
{
httpLink: WsLink,
}
)
export default class App extends Component {
render() {
return (
<ApolloProvider client={client}>
<Navigator />
</ApolloProvider>
)
;
}
}
答案 0 :(得分:0)
您需要使用订阅设置网络接口。
尝试一下:
import React, { Component } from 'react';
import { Platform, StyleSheet, Text, View } from 'react-native';
import ApolloClient, {createNetworkInterface} from "apollo-boost";
import gql from 'graphql-tag';
import { ApolloProvider } from "react-apollo";
import { SubscriptionClient } from 'subscriptions-transport-ws';
import Navigator from './app/Navigator'
import { createStackNavigator, createAppContainer } from 'react-navigation';
// Create regular NetworkInterface by using apollo-client's API:
const networkInterface = createNetworkInterface({
uri: 'https://api.graph.cool/simple/v1/cjrt2a37a5y4d0151eizuzi50' // Your GraphQL endpoint
});
// Create WebSocket client
const wsClient = new SubscriptionClient(`wss://subscriptions.us-west-2.graph.cool/v1/cjrt2a37a5y4d0151eizuzi50`, {
reconnect: true
});
// Extend the network interface with the WebSocket
const networkInterfaceWithSubscriptions = addGraphQLSubscriptions(
networkInterface,
wsClient
);
const client = new ApolloClient(
{
networkInterface: networkInterfaceWithSubscriptions
}
)
export default class App extends Component {
render() {
return (
<ApolloProvider client={client}>
<Navigator />
</ApolloProvider>
)
;
}
}