@nestjs-cognito/graphql

GraphQL-specific decorators and guards for NestJS applications.

Installation

npm install @nestjs-cognito/graphql @nestjs-cognito/auth @nestjs-cognito/core

Decorators

Guards

@GqlAuthentication()

Apply JWT authentication to GraphQL resolvers or operations.

@Resolver()
@GqlAuthentication()
export class UserResolver {}

@GqlAuthorization(groups: string[])

Require specific Cognito groups for GraphQL operations.

@Mutation()
@GqlAuthorization(['admin'])
async adminMutation() {}

@GqlPublicRoute()

Bypass authentication for specific GraphQL operations.

@Query()
@GqlPublicRoute()
async publicQuery() {}

Parameter Decorators

@GqlCognitoUser(property?: string)

Extract authenticated user from GraphQL context.

@Query()
async me(@GqlCognitoUser() user: CognitoJwtPayload) {}

@GqlCognitoIdUser(property?: string)

Extract ID token payload (validates token type).

@Query()
async profile(@GqlCognitoIdUser() user: CognitoIdTokenPayload) {}

@GqlCognitoAccessUser(property?: string)

Extract access token payload (validates token type).

@Query()
async permissions(@GqlCognitoAccessUser() token: CognitoAccessTokenPayload) {}

Exports

Decorators:

  • @GqlAuthentication()
  • @GqlAuthorization(groups: string[])
  • @GqlPublicRoute()
  • @GqlCognitoUser(property?: string)
  • @GqlCognitoIdUser(property?: string)
  • @GqlCognitoAccessUser(property?: string)

Guards:

  • GqlCognitoAuthGuard
  • GqlCognitoGroupGuard

Constants:

  • GQL_PUBLIC_ROUTE_METADATA

Guides

  • GraphQL - Complete GraphQL integration guide