denvned/isomorphic-relay

Question: Parse session in graphql

saltas888 opened this issue · 1 comments

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?

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 } },
);