-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgatsby-ssr.js
55 lines (47 loc) · 1.42 KB
/
gatsby-ssr.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
import React from 'react'
import { Provider } from 'react-redux'
import { createStore, combineReducers, applyMiddleware, compose } from 'redux'
import exampleReducer from 'src/reducers/example'
import { GRAPHQL_API_BASE_URL } from 'src/config'
import {
ApolloClient,
ApolloProvider,
createNetworkInterface,
IntrospectionFragmentMatcher
} from 'react-apollo'
const networkInterface = createNetworkInterface({
uri: `${GRAPHQL_API_BASE_URL}/`
})
const token =
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI1ODdjNmUwMTUyODI1YTAwMWY5ZjIxYjAiLCJyb2xlIjpbImFkbWluIl0sImV4cCI6MTQ5NTc3NjQ2MywiaWF0IjoxNDk1MTcxNjYzfQ.2RwVORaIwLmpkxfPbzdvbUyK-Jytl4tMLDNAv-Y632s'
networkInterface.use([
{
applyMiddleware(req, next) {
if (!req.options.headers) {
req.options.headers = {} // Create the header object if needed.
}
//req.options.headers.authorization = token ? `Bearer ${token}` : null
next()
}
}
])
const client = new ApolloClient({
networkInterface: networkInterface
})
const store = createStore(
combineReducers({
exampleReducer: exampleReducer,
apollo: client.reducer()
}),
// initial state
{},
compose(applyMiddleware(client.middleware()))
)
// This gets returned to Gatsby which then renders the Root component as normal.
exports.wrapRootComponent = Root => {
return () => (
<ApolloProvider client={client} store={store}>
<Root />
</ApolloProvider>
)
}