【问题标题】:How to access Response object in NestJS GraphQL resolver如何在 NestJS GraphQL 解析器中访问 Response 对象
【发布时间】:2021-07-27 06:15:40
【问题描述】:

如何将@Res() 传递到我的 graphql 解析器?

这不起作用:

@Mutation(() => String)
  login(@Args('loginInput') loginInput: LoginInput, @Res() res: Response) {
    return this.authService.login(loginInput, res);
  }

【问题讨论】:

    标签: node.js typescript graphql nestjs typegraphql


    【解决方案1】:

    @Res() 用于 HTTP 请求。要访问res 对象,您首先需要确保通过在GraphqlModule 的选项中使用context: ({ req, res }) => ({ req, res }) 将其添加到graphql 的context,然后您可以使用@Context() ctx 来获取context 和ctx.res 获取响应对象

    【讨论】:

    • 有没有办法以类型安全的方式实现这一点,而无需使用as 运算符的显式类型断言?
    猜你喜欢
    • 2019-11-17
    • 2018-02-18
    • 2019-05-27
    • 2014-11-18
    • 2020-05-22
    • 2020-09-04
    • 2020-12-14
    • 2021-11-17
    • 2020-07-21
    相关资源
    最近更新 更多