GraphQL + NestJS - 我如何在警卫中访问@Args?

Posted

技术标签:

【中文标题】GraphQL + NestJS - 我如何在警卫中访问@Args?【英文标题】:GraphQL + NestJS - how can I access @Args in a guard? 【发布时间】: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;

【问题讨论】:

【参考方案1】:

你可以使用 GqlExecutionContext ,比如:

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

@nestjs/graphql 版本:^7.6.0

【讨论】:

以上是关于GraphQL + NestJS - 我如何在警卫中访问@Args?的主要内容,如果未能解决你的问题,请参考以下文章