github.com/maeglindeveloper/gqlgen@v0.13.1-0.20210413081235-57808b12a0a0/example/chat/src/index.js (about) 1 import React from 'react'; 2 import ReactDOM from 'react-dom'; 3 import { 4 ApolloClient, 5 ApolloProvider, 6 HttpLink, 7 split, 8 } from '@apollo/client'; 9 import { InMemoryCache } from 'apollo-cache-inmemory'; 10 import { WebSocketLink } from 'apollo-link-ws'; 11 import { getMainDefinition } from 'apollo-utilities'; 12 import { App } from './App'; 13 14 const wsLink = new WebSocketLink({ 15 uri: `ws://localhost:8085/query`, 16 options: { 17 reconnect: true 18 } 19 }); 20 21 const httpLink = new HttpLink({ uri: 'http://localhost:8085/query' }); 22 23 24 // depending on what kind of operation is being sent 25 const link = split( 26 // split based on operation type 27 ({ query }) => { 28 const { kind, operation } = getMainDefinition(query); 29 return kind === 'OperationDefinition' && operation === 'subscription'; 30 }, 31 wsLink, 32 httpLink, 33 ); 34 35 const apolloClient = new ApolloClient({ 36 link: link, 37 cache: new InMemoryCache(), 38 }); 39 40 if (module.hot) { 41 module.hot.accept('./App', () => { 42 const NextApp = require('./App').default; 43 render(<NextApp/>); 44 }) 45 } 46 47 function render(component) { 48 ReactDOM.render(<ApolloProvider client={apolloClient}> 49 {component} 50 </ApolloProvider>, document.getElementById('root')); 51 } 52 53 render(<App />);