首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用快速网关修改graphql查询变量

使用快速网关修改graphql查询变量
EN

Stack Overflow用户
提问于 2021-02-15 12:27:22
回答 1查看 135关注 0票数 0

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

网关上的代码如下,

代码语言:javascript
复制
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();
      });
    };
  }
};

现在,当我按下邮递员的请求时,请求只在包含clientID时才返回200 as,否则它将抛出为错误。

"message":“未提供所需类型的"ID!”变量“"$clientID”。

知道这里会出什么问题吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-02-16 12:18:33

我能做到这一点的唯一方法是使用node-fetch,然后从我的中间件中向graphql服务器发出一个fetch请求,而不是执行return next()并遵循中间件链。

我的设置如下所示,

代码语言:javascript
复制
Client (vue.js w/ apollo-client) ---> Gateway (express-gateway) ---> Graphql (apollo-server) ---> Backend REST API (*)

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

代码语言:javascript
复制
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 });
        }
      });
    };
  }
};
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66208010

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档