如何在单个 graphql 请求中禁用多个操作

问题描述

Graphql 支持单个请求中的多个操作。这意味着可以在单个 HTTP 请求中提交无限数量的 graphql 查询,这将导致一些安全问题。有没有办法只允许单个 graphql 请求中的单个操作?

解决方法

您可以限制查询次数,如下所示

app.use('*',(req,res,next) => {
  const query = req.query.query || req.body.query || '';
  if (query.length > 1) {
    throw new Error('Only One Query allowed');
  }
  next();
});