adamsoffer / next-apollo

React higher-order component for integrating Apollo Client with Next.js
MIT License
481 stars 64 forks source link

Pass ctx to apolloClient #65

Closed alyavasilyeva closed 4 years ago

alyavasilyeva commented 4 years ago

Related to #60 If withData receives function, pass ctx to ApolloClient

adamsoffer commented 4 years ago

@alyavasilyeva - I actually just tested these changes with the next-apollo-example and it seems these changes breaks ssr. Getting the following warnings: index.js:1 Warning: Expected server HTML to contain a matching <div> in <div>.

adamsoffer commented 4 years ago

Here's how I'm passing in the client in the example:

const config = {
  link: new HttpLink({
    uri: "https://api.graph.cool/simple/v1/cixmkt2ul01q00122mksg82pn"
  })
};

const client = new ApolloClient(config)

export default withData(client);
alyavasilyeva commented 4 years ago

@adamsoffer oh, that's my mistake! I wrote a misleading description, at the moment it works like this:

const config = ctx => ({
  link: new HttpLink({
    uri: "https://api.graph.cool/simple/v1/cixmkt2ul01q00122mksg82pn",
    headers: {
      Cookie: !process.browser && ctx && ctx.req && ctx.req.headers['cookie'],
    },
  }),
});

export default withData(client);

This solution was enough for authentication in our project, and I didn't check it with ApolloClient, but if we still need to be able to use ApolloClient in withData, let me check it again!