11

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

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

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

4

1 回答 1

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
    };
}
于 2018-01-05T02:42:40.983 回答