如何在 NestJS GraphQL 解析器中访问 Response 对象

bra*_*rke 3 node.js typescript graphql nestjs typegraphql

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

这不起作用:

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

Jay*_*iel 6

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

  • 有没有办法以类型安全的方式实现这一点,而无需使用“as”运算符的显式类型断言? (2认同)