【问题标题】:How to forward extension from one server to middleware server如何将扩展从一台服务器转发到中间件服务器
【发布时间】:2018-06-04 20:42:29
【问题描述】:

我在中间件服务器上使用远程模式拼接。我可以在中间件服务器上远程获取架构,在中间件服务器上定义我的路由。

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

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

{
    "data": {
        "records": {
            "record": [{
                    "id": 1,
                },
                {
                    "id": 2,
                }
            ],
        },
        "additionalInfo": {}
    },
    "extensions": {
        "info": {}
    }
}

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

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

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

【问题讨论】:

    标签: node.js express graphql graphql-js express-graphql


    【解决方案1】:

    你有 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
        };
    }
    

    【讨论】:

    • :=> 不是必需的。原始示例代码使用对象字面量简写方法定义。
    • @aaronjkrause 我刚刚在文档中遇到了这个问题,感谢您指出这一点。
    • @RickyM 我看到了请求,但apiHeaders: [], additionalInfo: {}, 那里是空的。
    猜你喜欢
    • 2014-04-20
    • 2019-01-24
    • 2021-07-07
    • 2013-10-01
    • 1970-01-01
    • 2018-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多