如何将扩展从一台服务器转发到中间件服务器

N S*_*rma 11 node.js express graphql graphql-js express-graphql

我在我的middlware服务器上使用远程架构拼接.我能够在中间件服务器上远程获取架构,在中间件服务器上定义我的路由.

app.use('/graphql', graphqlHTTP((request,res) => {
 const startTime = Date.now();
 return {
   schema: remoteSchema
   graphiql: false,
   extensions({ document, variables, operationName, result }) {
     return {
       // here I am not getting extensions which I have on my another server as below.
       console.log(res); // this does not have additional info and response headers
       console.log(result); // this only has response against the query
     }
   };
})); 
Run Code Online (Sandbox Code Playgroud)

我在结果中得到查询的结果,但没有得到响应标题和附加信息,这是我在其他解析器所在的其他服务器上添加的扩展的一部分.

{
    "data": {
        "records": {
            "record": [{
                    "id": 1,
                },
                {
                    "id": 2,
                }
            ],
        },
        "additionalInfo": {}
    },
    "extensions": {
        "info": {}
    }
}
Run Code Online (Sandbox Code Playgroud)

可能是什么问题?这就是我在扩展程序中的另一台服务器上添加响应标头和其他信息的方法.我调试下面的扩展数据可用的代码.这不会传递给中间件服务器.

extensions({ document, variables, operationName, result }) {
   result.data.additionalInfo = res.additionalInfo;
   // extension to write api headers in response
   var headerObj = {};
   res.apiHeaders.forEach(element => {
     merge(headerObj, element);
   });
   result.headerObj = headerObj;
   return {
      information: headerObj
   };
}
Run Code Online (Sandbox Code Playgroud)

我的应用程序流程是我使用远程架构拼接调用中间件路由然后另一个服务器路由.我希望我在另一台服务器上添加的扩展应该转发到响应中的中间件服务器.

Ric*_*kyM 2

你有 console.log() 请求吗?我很确定您在扩展函数中获得的有关您想要输出的标头的任何内容都将在请求中,因为它是服务器上的中间件,响应是您在将其发送到下一个函数之前要修改的内容或返回给客户端。

extensions({ document, variables, operationName, result }) {
    // console.log the request object to check the header information from the request.
    console.log(request);
    return {
        // This will fill the information key with all the headers in the request.
        information: reaquest.header
    };
}
Run Code Online (Sandbox Code Playgroud)