【问题标题】:GraphQL + NestJS - how can I access @Args in a guard?GraphQL + NestJS - 我如何在警卫中访问@Args?
【发布时间】:2020-12-14 13:27:42
【问题描述】:

我需要以某种方式从警卫内部的@Args 访问objectId,以检查发件人是否将objectId 分配给他的帐户。知道如何实现它吗?

 @Query(() => [Person])
      @UseGuards(ObjectMatch)
      async pplWithObject(@Args('objectId') id: string): Promise<Person[]> {
        return await this.objService.getPeopleWithObject(id);
      }

是否可以从上下文中访问传递的参数?

const ctx = GqlExecutionContext.create(context);
    const request = ctx.getContext().req;

【问题讨论】:

    标签: graphql nestjs guard args


    【解决方案1】:

    你可以使用 GqlExecutionContext ,比如:

    const ctx = GqlExecutionContext.create(context);
    console.log(ctx.getArgs()) // object with your query args
    ctx.getArgs()['objectId']
    

    @nestjs/graphql 版本:^7.6.0

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-29
      • 2022-08-11
      • 2022-06-11
      • 2021-11-17
      • 2021-04-03
      • 2021-07-27
      • 2020-03-13
      • 2021-09-24
      相关资源
      最近更新 更多