-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
146 lines (124 loc) · 3.87 KB
/
index.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import { ApolloClient, HttpLink, InMemoryCache } from 'apollo-boost'
import fetch from 'isomorphic-unfetch'
import Head from 'next/head'
import { getDataFromTree } from 'react-apollo'
import propTypes from 'prop-types'
import { withClientState } from 'apollo-link-state'
import defaults from './local/defaults'
import resolvers from './local/resolvers'
import typeDefs from './local/typeDefs'
import { setContext } from 'apollo-link-context'
import { ApolloLink } from 'apollo-link'
let apolloClient = null
// Polyfill fetch() on the server (used by apollo-client)
if (!process.browser) {
global.fetch = fetch
}
function create(initialState) {
const httpLink = new HttpLink({
uri: process.env.API_URL
})
const cache = new InMemoryCache().restore(initialState || {})
const stateLink = withClientState({
resolvers,
defaults,
typeDefs,
cache
})
const authLink = setContext((req, prevCtx) => {
let authHeaders = {}
if (process.browser) {
let idToken = localStorage.getItem('idToken')
let userId = localStorage.getItem('userId')
if (idToken && userId) {
Object.assign(authHeaders, {
authorization: `Bearer ${idToken}`,
userid: userId
})
}
}
return {
headers: {
...prevCtx.headers,
...authHeaders
}
}
})
const link = ApolloLink.from([authLink, stateLink, httpLink])
return new ApolloClient({
connectToDevTools: process.browser,
ssrMode: !process.browser, // Disables forceFetch on the server (so queries are only run once)
link,
cache
})
}
function initApollo(initialState) {
// Make sure to create a new client for every server-side request so that data
// isn't shared between connections (which would be bad)
if (!process.browser) {
return create(initialState)
}
// Reuse client on the client-side
if (!apolloClient) {
apolloClient = create(initialState)
}
return apolloClient
}
export default App => {
return class Apollo extends React.Component {
static displayName = 'withApollo(App)'
static async getInitialProps(ctx) {
try {
const { Component, router } = ctx
// Run all GraphQL queries in the component tree
// and extract the resulting data
const apollo = initApollo()
let appProps = {}
if (App.getInitialProps) {
appProps = await App.getInitialProps(ctx, apollo)
}
const apolloState = {}
try {
// Run all GraphQL queries
await getDataFromTree(
<App
{...appProps}
Component={Component}
router={router}
apolloState={apolloState}
apolloClient={apollo}
/>
)
} catch (error) {
// Prevent Apollo Client GraphQL errors from crashing SSR.
// Handle them in components via the data.error prop:
// http://dev.apollodata.com/react/api-queries.html#graphql-query-data-error
console.error('Error while running `getDataFromTree`', error)
}
if (!process.browser) {
// getDataFromTree does not call componentWillUnmount
// head side effect therefore need to be cleared manually
Head.rewind()
}
// Extract query data from the Apollo store
apolloState.data = apollo.cache.extract()
return {
...appProps,
apolloState
}
} catch (ex) {
console.error(ex)
}
}
constructor(props) {
super(props)
// `getDataFromTree` renders the component first, the client is passed off as a property.
// After that rendering is done using Next's normal rendering pipeline
this.apolloClient =
props.apolloClient || initApollo(props.apolloState.data)
}
render() {
return <App {...this.props} apolloClient={this.apolloClient} />
}
}
}