I know it is possible to use apollo-link-ws
to handle subscriptions in client side. Also i created a client which uses standard http
for running Queris
and Mutations
, and uses WebSocket
for subscriptions
. But i need to Pub
( emit ) some data to server through websocket too. So a way is using only apollo-link-ws
for all actions ( Query, Mutation ). But is it possible to make decision which Query uses Websocket which one uses standard http client?
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(),
});
And i pass the client to ApolloProvider
.