denvned / isomorphic-relay

Adds server side rendering support to React Relay
BSD 2-Clause "Simplified" License
242 stars 53 forks source link

Question: Parse session in graphql #53

Closed saltas888 closed 8 years ago

saltas888 commented 8 years ago

Hello, I'm creating a basic user authentication with server side rendering...

My server looks like this

https://github.com/jkettmann/relay-authentication/blob/master/server/server.js

where graphqlServer: https://github.com/jkettmann/relay-authentication/blob/master/server/graphQlServer.js

i have also appended a render function in the server.js file:

const networkLayer = new Relay.DefaultNetworkLayer(GRAPHQL_URL,{
    credentials: 'same-origin',
    session: req.session
  });

  match({ routes:routes({req.context}), location: req.url }, async (error, redirectLocation, renderProps) => {
    if (error) {
      next(error);
    } else if (redirectLocation) {
      res.redirect(302, redirectLocation.pathname + redirectLocation.search);
    } else if (renderProps) {
      await fetchData(renderProps, req.context)
      IsomorphicRouter.prepareData(renderProps, networkLayer).then(render).catch(next);
    } else {
      res.status(404).send('Not Found');
    }

Now, when I refresh the page, I got in the graphql server an empty session and then it creates an anonymous token.

But, if I go in a page, that needs data from relay, without page reload then I get the session with the logged in user token.

It seems like that injecting the session in networkLayer doesn't work...

What is my fault?

denvned commented 8 years ago

WHAT-WG Fetch API that DefaultNetworkLayer uses internally doesn't understand what the session property means.

Instead, you should use it like this:

const networkLayer = new Relay.DefaultNetworkLayer(
  GRAPHQL_URL,
  { headers: { cookie: req.headers.cookie } },
);