import { ApolloClient, InMemoryCache, HttpLink } from '@apollo/client';
import { Config } from '../config';
import { WebSocketLink } from "@apollo/client/link/ws";
import { RetryLink } from "@apollo/client/link/retry";
import { SubscriptionClient } from "subscriptions-transport-ws";


const subscriptionClient = new SubscriptionClient(Config.subscriptionEndpoint, {
  reconnect: true
});

const link = new RetryLink().split(
  (operation) => operation.operationName === 'subscription',
  new WebSocketLink(subscriptionClient),
  new HttpLink({ uri: Config.graphQLEndpoint, credentials: 'include' })
);

export const client = new ApolloClient<any>({
  cache: new InMemoryCache(),
  link: link,
});