有什么我可以注入来了解我所处的上下文,即我的服务是从graphql请求或http请求调用的。
我有一个请求范围服务,需要返回一个标题。标头存储在REQUEST
对象上,如果它是一个HTTP上下文,它可以自动注入,或者在graphql上下文中可用(如我之前设置的那样) - 因此
return this.request.headers["test"]
要么
return this.context.request.headers["test"]
但我需要了解我所处的上下文以返回正确的对象
有任何想法吗?
提前致谢
您可以创建一个帮助函数来检索标头:
getHeader(key: string) {
if (this.request && this.request.headers && this.request.headers[key]) {
return this.request.headers[key];
} else if (this.context.request && this.context.request.headers && this.context.request.headers[key]) {
return this.context.request.headers[key];
} else {
throw new BadRequestException(`Required header ${key} is missing`);
}
}