Express 将获取响应头转发到Apollo graphql响应
我在apollo/Graphql上设置了apollo数据源rest数据源 Expressjs服务器。我想将一些头从fetch响应转发到/graphql响应 以下是流程:Express 将获取响应头转发到Apollo graphql响应,express,graphql,apollo,apollo-server,Express,Graphql,Apollo,Apollo Server,我在apollo/Graphql上设置了apollo数据源rest数据源 Expressjs服务器。我想将一些头从fetch响应转发到/graphql响应 以下是流程: POST /graphql Graphql makes fetch request using this.get('http://example.com/api') Response from this fetch contains the header "cache-status" Response from /graph
POST /graphql
Graphql makes fetch request using this.get('http://example.com/api')
Response from this fetch contains the header "cache-status"
Response from /graphql
I'd like to include the "cache-status" header from the example.com/api response here in the /graphql response
我在rest数据源类的didReceiveResponse()方法中看到了头。我不确定这是否是访问和存储它的正确位置。如何在POST/graphql响应中包含“缓存状态”标题?假设您来自,您可以覆盖didReceiveResponse
以拦截响应并返回自定义返回值
这里是Typescript中的代码片段,如果需要,可以通过删除参数/返回类型和访问修饰符轻松地将其转换为Javascript
class MyRestDataSource extends RESTDataSource {
public constructor(baseUrl: string) {
super();
this.baseURL = baseUrl;
}
public async getSomething(): Promise<any & { headers?: { [key: string]: string } }> {
// make the get request
return this.get('path/to/something');
}
// intercept response after receiving it
protected async didReceiveResponse(response: Response, _request: Request) {
// get the value that is returned by default, by calling didReceiveResponse from the base class
const defaultReturnValue = await super.didReceiveResponse(response, _request);
// check if it makes sense to replace return value for this type of request
if (_request.url.endsWith('path/to/something')) {
// if yes get the headers from response headers and add it to the returned value
return {
...defaultReturnValue,
headers: { headerValue: response.headers.get('header_name') },
};
}
return defaultReturnValue;
}
}
MyRestDataSource类扩展了RESTDataSource{
公共构造函数(baseUrl:string){
超级();
this.baseURL=baseURL;
}
公共异步getSomething():承诺{
//发出get请求
返回这个.get('path/to/something');
}
//接收到响应后拦截响应
受保护的异步didReceiveResponse(响应:响应,\u请求:请求){
//通过从基类调用didReceiveResponse,获取默认情况下返回的值
const defaultReturnValue=wait super.didReceiveResponse(响应,_请求);
//检查替换此类请求的返回值是否合理
if(_request.url.endsWith('path/to/something')){
//如果是,则从响应头中获取头并将其添加到返回值中
返回{
…默认返回值,
headers:{headerValue:response.headers.get('header\u name')},
};
}
返回默认返回值;
}
}
您是否知道如何获取响应标题等?