Skip to content

Commit a6d24cb

Browse files
alyavasilyevaadamsoffer
authored andcommitted
Add headers to apolloClient
If withData receives ApolloClient instance, pass headers to ApolloClient
1 parent 8923907 commit a6d24cb

File tree

3 files changed

+16
-10
lines changed

3 files changed

+16
-10
lines changed

package-lock.json

+3-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@
3939
"apollo-boost": "^0.4.4",
4040
"graphql": "^14.3.1",
4141
"next": "^9.0.6",
42-
"react-dom": "^16.8.6",
43-
"react": "^16.8.6"
42+
"react": "^16.8.6",
43+
"react-dom": "^16.8.6"
4444
},
4545
"devDependencies": {
4646
"@apollo/react-hooks": "^3.0.1",
@@ -65,6 +65,7 @@
6565
"dependencies": {
6666
"@babel/runtime": "^7.4.5",
6767
"isomorphic-unfetch": "^3.0.0",
68+
"lodash": "^4.17.15",
6869
"prop-types": "15.7.2",
6970
"prop-types-exact": "1.2.0",
7071
"url": "0.11.0"

src/withData.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import Head from 'next/head'
33
import { ApolloProvider } from '@apollo/react-hooks'
44
import { ApolloClient, InMemoryCache, HttpLink } from 'apollo-boost'
55
import fetch from 'isomorphic-unfetch'
6+
import { isFunction } from 'lodash'
67

78
let apolloClient = null
89

@@ -40,7 +41,11 @@ export default apolloConfig => {
4041

4142
// Initialize ApolloClient, add it to the ctx object so
4243
// we can use it in `PageComponent.getInitialProp`.
43-
const apolloClient = (ctx.apolloClient = initApolloClient(apolloConfig, null))
44+
const apolloClient = (ctx.apolloClient = initApolloClient(
45+
apolloConfig,
46+
null,
47+
ctx
48+
))
4449

4550
// Run wrapped getInitialProps methods
4651
let pageProps = {}
@@ -101,7 +106,10 @@ export default apolloConfig => {
101106
* Creates or reuses apollo client in the browser.
102107
* @param {Object} initialState
103108
*/
104-
function initApolloClient(apolloConfig, initialState = {}) {
109+
function initApolloClient(apolloConfig, initialState = {}, ctx) {
110+
if (isFunction(apolloConfig)) {
111+
apolloConfig = apolloConfig(ctx)
112+
}
105113
// Make sure to create a new client for every server-side request so that data
106114
// isn't shared between connections (which would be bad)
107115
if (typeof window === 'undefined') {

0 commit comments

Comments
 (0)