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

Session cookie is not passed #437

Closed
benoitguigal opened this issue Oct 8, 2015 · 6 comments
Closed

Session cookie is not passed #437

benoitguigal opened this issue Oct 8, 2015 · 6 comments

Comments

@benoitguigal
Copy link

I am using Relay with express-graphql and express-session. I would like to use the session in my GraphQL schema but unfortunately the connect.sid cookie is not passed in graphql requests.
Should I create my own network layer for that or is this supposed to be built-in ?

@fson
Copy link
Contributor

fson commented Oct 8, 2015

The default network layer uses fetch which does not pass cookies by default, but can enable them by passing an init object for fetch when you're creating the network layer:

Relay.injectNetworkLayer(
  new Relay.DefaultNetworkLayer('http://example.com/graphql', {
    credentials: 'same-origin',
  })
);

@benoitguigal
Copy link
Author

Great ! Reading the documentation for injecting a network layer, it is not clear to me where this should be done. Just before declaring the RootContainer ? I am using react-router-relay so I wonder if I have control over the network layer or if I should look inside react-router-relay.

@fson
Copy link
Contributor

fson commented Oct 8, 2015

@benoitguigal It shouldn't matter as long as you do it before rendering your app with ReactDOM.render(). You probably have some sort of entry point for your app where you set up the router and call ReactDOM.render() – do it there.

@benoitguigal
Copy link
Author

Works like a charm, thanks a lot !

@cpotemski
Copy link

@benoitguigal Can you give me an example how you pass the connect.sid to the requests? I am at the same point and don't know how to set the cookie.

@benoitguigal
Copy link
Author

@dasarma, just add:

Relay.injectNetworkLayer(
  new Relay.DefaultNetworkLayer('http://example.com/graphql', {
    credentials: 'same-origin',
  })
);

an then cookies will be passed.
Then just follow documentation from express-graphql:

var session = require('express-session');
var graphqlHTTP = require('express-graphql');

var app = express();

app.use(session({ secret: 'keyboard cat', cookie: { maxAge: 60000 }}));

app.use('/graphql', graphqlHTTP(request => ({
  schema: MySessionAwareGraphQLSchema,
  rootValue: { session: request.session },
  graphiql: true
})));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants