如何使用例如lusca在graphql和graphiql的node express上启用csrf?

tob*_*kle 6 csrf node.js express graphql

有一个节点表达服务器与csrf lusca,我想为graphql和graphiql启用csrf,但如何做到这一点?

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',
}))
Run Code Online (Sandbox Code Playgroud)