我知道可以使用apollo-link-ws
在客户端处理订阅。我还创建了一个客户端,该客户端使用标准的http
来运行Queris
和Mutations
,并为WebSocket
使用subscriptions
。但是我也需要Pub
(通过Websocket将一些数据发送到服务器)。因此,一种方法是对所有操作(Query,Mutation)仅使用apollo-link-ws
。但是可以确定哪个查询使用标准HTTP客户端的Websocket吗?
import { ApolloClient } from 'apollo-client';
import { getMainDefinition } from 'apollo-utilities';
import { split } from 'apollo-link';
import { createUploadLink } from 'apollo-upload-client';
import { WebSocketLink } from 'apollo-link-ws';
import { InMemoryCache } from 'apollo-cache-inmemory';
import { ApolloProvider } from 'react-apollo';
const uploadLink = createUploadLink({
uri:`http://localhost:8899/graphql`,
credentials: 'include'
});
const wsLink = new WebSocketLink({
uri: `ws://localhost:8899/graphql`,
options: {
reconnect: true,
timeout: 120000,
}
})
const client = new ApolloClient({
link: split(
// split based on operation type
({ query }) => {
const definition = getMainDefinition(query);
console.table(definition);
return (
definition.kind === 'OperationDefinition' &&
definition.operation === 'subscription'
);
},
wsLink,
uploadLink),
cache : new InMemoryCache(),
});
然后我将客户端传递给ApolloProvider
。