Search code examples
graphqlnestjsguardargs

GraphQL + NestJS - how can I access @Args in a guard?


I need the to somehow access the objectId from @Args inside the guard so as to check if the sender has the objectId assigned to his account. Any idea how I could implement it?

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

Is it possible to access the passed argument from the context?

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

Solution

  • You can use GqlExecutionContext for it, like:

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

    @nestjs/graphql version: ^7.6.0