使用 express-gateway 修改 graphql 查询变量

Modifying graphql query variable using express-gateway

我正在尝试使用 express-gateway 修改 graphql 查询变量。

网关上的代码如下,

const axios = require("axios");
const jsonParser = require("express").json();
const { PassThrough } = require("stream");

module.exports = {
  name: 'gql-transform',
  schema: {
    ... // removed for brevity sakes
  },
  policy: (actionParams) => {
    return (req, res, next) => {
      req.egContext.requestStream = new PassThrough();
      req.pipe(req.egContext.requestStream);
  
      return jsonParser(req, res, () => {
        req.body = JSON.stringify({
          ...req.body,
          variables: {
            ...req.body.variables,
            clientID: '1234'
          }
        });

        console.log(req.body); // "clientID": "1234" is logged in the body.variables successfully here
        return next();
      });
    };
  }
};

现在,当我从 POSTMAN 发出请求时,请求通过并且 returns 只有当我包含 clientID 时才返回 200OK,否则,它会抛出错误

"message": "Variable "$clientID" of required type "ID!" was not provided."

知道这里可能出了什么问题吗?

我能让它工作的唯一方法是使用 node-fetch,然后从我的中间件向 graphql-server 发出 fetch 请求,而不是执行 return next() 并遵循中间件链。

我的设置如下所示,

Client (vue.js w/ apollo-client) ---> Gateway (express-gateway) ---> Graphql (apollo-server) ---> Backend REST API (*)

当我的客户端向我的网关发出 graphql 请求时,我修改了我的中间件以执行以下操作(与问题中的内容相反),

const jsonParser = require("express").json();
const fetch = require('node-fetch');

module.exports = {
  name: 'gql-transform',
  schema: {
    ... // removed for brevity sakes
  },
  policy: () => {
    return (req, res) => {
      jsonParser(req, res, async () => {
        try {
          const response = await fetch(`${host}/graphql`, {...}) // removed config from fetch for brevity
          res.send(response);
        } catch (error) {
          res.send({ error });
        }
      });
    };
  }
};