-
Notifications
You must be signed in to change notification settings - Fork 0
/
file23.js
72 lines (60 loc) · 1.73 KB
/
file23.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import React from 'react'
import ReactDOM from 'react-dom'
import './index.css'
import App from './App'
import * as serviceWorker from './serviceWorker'
// NEW IMPORTS ref: https://www.youtube.com/watch?v=iNStN6q_5As
import { ApolloClient, InMemoryCache } from 'apollo-boost'
import { ApolloProvider } from '@apollo/react-hooks'
import { createHttpLink, HttpLink } from 'apollo-link-http'
import { BrowserRouter } from 'react-router-dom'
import { setContext } from 'apollo-link-context'
import { AUTH_TOKEN } from './constants'
// Subscriptions
import { split } from 'apollo-link'
import { WebSocketLink } from 'apollo-link-ws'
import { getMainDefinition } from 'apollo-utilities'
const httpLink = new HttpLink({
uri: 'https://ciao-be-2.herokuapp.com/'
})
const authLink = setContext((_, {headers}) => {
const token = localStorage.getItem(AUTH_TOKEN)
return{
headers: {
...headers,
authorization: token ? `Bearer ${token}` : ''
}
}
})
const wsLink = new WebSocketLink({
uri: `wss://ciao-be-2.herokuapp.com/`,
options: {
reconnect: true,
connectionParams: {
token: localStorage.getItem("d65d2435915eab95eea7abfed9359381619dafa9e5bd44389285ebde40cb18fd10ba8b11133caf339ade087075fb2a06"),
},
}
})
const link = split(
({ query }) => {
const { kind, operation } = getMainDefinition(query)
return kind === 'OperationDefinition' && operation === 'subscription'
},
wsLink,
authLink.concat(httpLink)
)
const cache = new InMemoryCache()
const client = new ApolloClient({
link,
cache,
resolvers: {},
})
ReactDOM.render(
<BrowserRouter>
<ApolloProvider client={client}>
<App />
</ApolloProvider>
</BrowserRouter>,
document.getElementById('root')
)
serviceWorker.unregister()