是否可以注入(inject)任何内容来了解我所处的上下文,即我的服务是从 graphql 请求或 http 请求调用的。
我有一个请求范围服务,需要返回一个 header 。 header 存储在 REQUEST
对象上,如果它是 HTTP 上下文,则可以自动注入(inject)该对象,或者它在 graphql 上下文上可用(正如我之前设置的那样) - 因此
return this.request.headers["test"]
或者
return this.context.request.headers["test"]
但我需要了解我所处的上下文才能返回正确的对象
有什么想法吗?
提前致谢
最佳答案
您可以创建一个辅助函数来检索 header :
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`);
}
}
关于javascript - 通用服务,了解我是在 GRAPHQL 上下文还是 HTTP 上下文中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55902611/