Friday 21 April 2017

how to enable csrf on node express for graphql and graphiql with e.g. lusca?

Having a node express server with csrf lusca and I want to enable csrf also for graphql and graphiql, but how to do that?

const csrf = require('lusca').csrf()
const graphqlExpress = require('graphql-server-express').graphqlExpress
const graphiqlExpress  = require('graphql-server-express').graphiqlExpress

server.use((req, res, next) => {
     csrf(req, res, next)
  }
})

server.use('/graphql', (req, res, next) => {
  graphqlExpress( () => {
    const query = req.query.query || req.body.query;
    return {
      schema,
      context: Object.assign({ user }, req.context),
      debug: false,
      formatError(e) { console.log(e) },
    }
  })(req, res, next)
})

server.use('/graphiql', graphiqlExpress({
  endpointURL: '/graphql',
}))



via tobkle

No comments:

Post a Comment