import { InMemoryCache , NormalizedCacheObject } from 'apollo-cache-inmemory' ; import { ApolloClient } from 'apollo-client' ; import { split } from 'apollo-link' ; import { HttpLink } from 'apollo-link-http' ; import { WebSocketLink } from 'apollo-link-ws' ; import { getMainDefinition } from 'apollo-utilities' ; import { OperationDefinitionNode } from 'graphql' ; import fetch from 'node-fetch' ; import ws from 'ws' ; export class TestApolloClient { public client : ApolloClient < NormalizedCacheObject > ; constructor ( host = 'localhost:8000' , authHeaders ? ) { this . client = createApolloClient ( host , authHeaders ) ; } } function createApolloClient ( host , authHeaders ? ) { const httpLinkConfig = { uri : `http://${ host }/graphql` , fetch , headers : authHeaders } ; const httpLink = new HttpLink ( httpLinkConfig as any ) ; const wsLink = new WebSocketLink ( { uri : `ws://${ host }/graphql` , options : { reconnect : true } , webSocketImpl : ws } ) ; const link = split ( ( { query } ) => { const mainDefinition = getMainDefinition ( query ) as OperationDefinitionNode ; const { kind , operation } = mainDefinition ; return kind === 'OperationDefinition' && operation === 'subscription' ; } , wsLink , httpLink ) ; return new ApolloClient ( { link , cache : new InMemoryCache ( ) , defaultOptions : { query : { fetchPolicy : 'no-cache' } , watchQuery : { fetchPolicy : 'no-cache' } } } ) ; }