Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added headers options for ApolloClient instance for server-side requests #2332

Merged
merged 5 commits into from
Feb 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions demo-projects/meetup/site/lib/initApollo.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const {

let apolloClient = null;

function create(initialState) {
function create(initialState, req) {
// Check out https://github.com/zeit/next.js/pull/4611 if you want to use the AWSAppSyncClient
return new ApolloClient({
connectToDevTools: process.browser,
Expand All @@ -18,16 +18,17 @@ function create(initialState) {
credentials: 'same-origin', // Additional fetch() options like `credentials` or `headers`
// Use fetch() polyfill on the server
fetch: !process.browser && fetch,
headers: req && req.headers,
}),
cache: new InMemoryCache().restore(initialState || {}),
});
}

export default function initApollo(initialState) {
export default function initApollo(initialState, req) {
// 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);
return create(initialState, req);
}

// Reuse client on the client-side
Expand Down
4 changes: 2 additions & 2 deletions demo-projects/meetup/site/lib/withApollo.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ export default App => {
const {
Component,
router,
ctx: { res },
ctx: { res, req },
} = ctx;

// Run all GraphQL queries in the component tree
// and extract the resulting data
const apollo = initApollo();
const apollo = initApollo(null, req);

ctx.ctx.apolloClient = apollo;

Expand Down