I have the following client code and wish to be able to catch server offline and online events in order to display user notifications.
import { ApolloClient, InMemoryCache } from '@apollo/client';
import { getMainDefinition } from 'apollo-utilities';
import { ApolloLink, split } from 'apollo-link';
import { WebSocketLink } from 'apollo-client-graphql-ws';
import { HttpLink } from 'apollo-link-http';
const httpServer = 'http://localhost:3030/graphql';
const wsServer = 'ws://localhost:3030/graphql';
const httpLink = new HttpLink({
uri: httpServer,
// credentials: 'include'
});
const wsLink = new WebSocketLink({
url: wsServer,
options: {
reconnect: true,
connectionCallback: (err, result) => {
if (err) {
console.log(`Error connecting to Websocket ${JSON.stringify(err)}`);
} else {
console.log(`Connected to WebSocket : ${result}`);
}
}
}
});
const terminatingLink = split(
({ query }) => {
const { kind, operation } = getMainDefinition(query);
return (
kind === 'OperationDefinition' && operation === 'subscription'
);
},
wsLink,
httpLink,
);
const link = ApolloLink.from([terminatingLink]);
const cache = new InMemoryCache();
const client = new ApolloClient({
link,
cache
});
export default client;