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

Viewed 1068

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;
1 Answers

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

Related