Émerson Felinto
Émerson Felinto

Reputation: 543

How can I use the same RoleGuard for REST and GraphQL?

I have this problem: nestjs context.swithToHttp().getRequest() returns undefined

The difference is I need to support GraphQL and REST for my application, but actually, I can't figure out any way to use the same RoleGuard for GraphQL and REST because they have context different.

Upvotes: 1

Views: 141

Answers (1)

Émerson Felinto
Émerson Felinto

Reputation: 543

I figure out it myself

    let user: User;

    if (context.getType() === 'http') {
      user = context.switchToHttp().getRequest().user;
    } else if (context.getType<GqlContextType>() === 'graphql') {
      const ctx = GqlExecutionContext.create(context);
      user = ctx.getContext().req.user;
    }

Upvotes: 2

Related Questions