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

1

6

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;
Envoi answered 25/8, 2020 at 19:31 Comment(0)
G
10

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

Grath answered 25/8, 2020 at 19:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.