dzzzzzy / Nestjs-Learning

nestjs 学习教程 :books:,跟我一起学习 nest 框架~ :muscle:

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

graphql-api demo能完善auth 验证部分?

callmesoul opened this issue · comments

发现很多graphql很多教程都只有graphql部分,
然后auth都只有auth部分。
然后grahpql部分没问题,
auth restful部分也没问题。
但是graphql加 auth几各种问题了

graphql 的 auth 功能和 restful 的基本一致,区别如下:

Guard

@Injectable()
export class GqlAuthGuard extends AuthGuard('jwt') {
    getRequest(context: ExecutionContext) {
        const ctx = GqlExecutionContext.create(context);
        return ctx.getContext().req;
    }
}

GraphQLModule

GraphQLModule.forRoot({
    ......
    // 将 req 传给 context
    context: ({ req }) => ({ req })
}),