大佬教程收集整理的这篇文章主要介绍了使用 express-gateway 修改 graphql 查询变量,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试使用 express-gateway
修改 graphql 查询变量。
网关上的代码如下,
const axios = require("axios");
const JsonParser = require("express").Json();
const { Passthrough } = require("stream");
module.exports = {
name: 'gql-transform',scheR_912_11845@a: {
... // removed for brevity sakes
},policy: (actionParams) => {
return (req,res,next) => {
req.egContext.requestStream = new Passthrough();
req.pipe(req.egContext.requestStream);
return JsonParser(req,() => {
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 的请求时,只有当我包含 clIEntID
时,请求才会通过并返回 200OK,否则,它会作为错误抛出
"message": "未提供所需类型 "ID!" 的变量 "$clIEntID"。"
知道这里出了什么问题吗?
我可以让这个工作的唯一方法是使用 node-fetch
然后从我的中间件向 graphql-sever 发出 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',scheR_912_11845@a: {
... // 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 });
}
});
};
}
};
以上是大佬教程为你收集整理的使用 express-gateway 修改 graphql 查询变量全部内容,希望文章能够帮你解决使用 express-gateway 修改 graphql 查询变量所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。