github.com/jepp2078/gqlgen@v0.7.2/example/chat/src/index.js (about)

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