I am trying to figure out how to make a new websocket connection for each subscription and I thought maybe ApolloLink would be the answer. I’m having trouble making it work. I know my code is not correct but I’m a bit lost on the direction I should take. Any advice is greatly appreciated.
I’m trying to do something like this.
const httpLink = new HttpLink({
uri: process.env.REACT_APP_LIVE_SERVICE_API,
credentials: 'include'
});
const subscriptionLinks = new ApolloLink((operation, forward) => {
let cars;
let people;
let books;
if (operation.operationName === 'CarsSubscription') {
cars = new WebSocketLink({
uri: `wss://${process.env.REACT_APP_LIVE_API.replace('https://', '')}/subscriptions`,
credentials: 'include',
options: {
reconnect: true,
lazy: true,
wsOptionArguments: []
}
});
forward(cars);
}
if (operation.operationName === 'PeopleSubscription') {
people = new WebSocketLink({
uri: `wss://${process.env.REACT_APP_LIVE_API.replace('https://', '')}/subscriptions`,
credentials: 'include',
options: {
reconnect: true,
lazy: true,
wsOptionArguments: []
}
});
forward(people);
}
if (operation.operationName === 'BooksSubscription') {
books = new WebSocketLink({
uri: `wss://${process.env.REACT_APP_LIVE_API.replace('https://', '')}/subscriptions`,
credentials: 'include',
options: {
reconnect: true,
lazy: true,
wsOptionArguments: []
}
});
forward(books);
}
return ApolloLink.from([cars, people, books]);
});
const splitLink = split(
({ query }) => {
const definition = getMainDefinition(query);
return (
definition.kind === 'OperationDefinition' &&
definition.operation === 'subscription'
);
},
subscriptionLinks,
httpLink
);
const client = new ApolloClient({
link: splitLink,
cache
});