简体   繁体   English

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

[英]Modifying graphql query variable using express-gateway

I'm trying to modify a graphql query variable using express-gateway .我正在尝试使用express-gateway修改 graphql 查询变量。

The code on the gateway is as below,网关上的代码如下,

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();
      });
    };
  }
};

Now, when I hit the request from POSTMAN, the request goes through and returns a 200OK only when I include clientID , otherwise, it throws as error现在,当我从 POSTMAN 发出请求时,只有当我包含clientID时,请求才会通过并返回 200OK ,否则会抛出错误

"message": "Variable "$clientID" of required type "ID." was not provided." “消息”:“未提供所需类型“ID”的变量“$clientID”。”

Any idea what could be going wrong here?知道这里可能出了什么问题吗?

The only way I could get this working was by using node-fetch and then making a fetch request to the graphql-sever from my middleware instead of doing a return next() and following the middleware chain.我可以让这个工作的唯一方法是使用node-fetch然后从我的中间件向 graphql-sever 发出fetch请求,而不是执行return next()并遵循中间件链。

My setup is something like the following,我的设置如下所示,

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

When my client makes a graphql request to my gateway, I've modified my middleware to do the following (as opposed to what's in the question),当我的客户向我的网关发出 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 });
        }
      });
    };
  }
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 express-gateway 中运行自定义函数 - Run custom functions in express-gateway Express-gateway 为快速微服务应用程序创建新的插件/策略 - Express-gateway create new plugin / policy for express microservices application 使用 Express-Gateway 访问 Api 和使用 JWT 进行用户身份验证的建议 - Suggestion on Api Access with Express-Gateway, and User authentication with JWT 如何正确使用express-gateway对Web应用程序进行身份验证? - How to properly use express-gateway for authentication of a web app? 设置授权后,Express-Gateway响应错误 - Express-Gateway response error after setting authorization 我无法在 Express-Gateway 上访问我想要的数据 - I cannot access the data I want on Express-Gateway 使用 Ingress 服务类型将“express-gateway”部署到 Google Kubernetes 引擎时出现问题 - Problem deploying “express-gateway” to Google Kubernetes Engine with Ingress service type 使用express和graphql查询mongodb - query mongodb with express and graphql GraphQL + React-> Express PROXY(网关)中间件 - GraphQL + React -> Express PROXY(Gateway) Middleware 缺少 GET 查询:在 Express 服务器上使用 Apollo 实现 GraphQL - GET query missing: Implementing GraphQL Using Apollo On an Express Server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM