Package Exports
- @nestjs-cognito/graphql
- @nestjs-cognito/graphql/dist/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@nestjs-cognito/graphql) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
@nestjs-cognito/graphql
Description
GraphQL utilities module for @nestjs-cognito/auth
Installation
npm i @nestjs-cognito/graphqlConfiguration
Usage
You can use the built-in @nestjs-cognito/graphql decorators and guards.
Built-in decorators and guards
- Decorate the resolver with the
@Authenticationdecorator or with the@UseGuardsdecorator to apply theAuthenticationGuardto the resolver in order to ensure that the user is authenticated. - Decorate the resolver with the
@Authorizationdecorator or with the@UseGuardsdecorator to apply theAuthorizationGuardin order to ensure that the user is authorized. - Decorate method arguments with the
@CurrentUserdecorator to get the current user.
During the authorization process, we already check if the user is authenticated, so you don't need to use authentication guard or decorator.
In addition, you can find more details about @UseGuards decorator here.
Here is an example that shows how to use authentication:
import { UseGuards } from "@nestjs/common";
import { Args, Query, Resolver } from "@nestjs/graphql";
import {
Authentication,
AuthenticationGuard,
CurrentUser,
} from "@nestjs-cognito/graphql";
import { User } from "@nestjs-cognito/auth";
@Resolver("dogs")
@Authentication()
export class DogsResolver {
@Query(() => String)
findAll(@CurrentUser() me: User): string {
return "This action returns all my dogs";
}
}
@Resolver("cats")
@UseGuards(AuthenticationGuard)
export class CatsResolver {
@Query(() => String)
findAll(@CurrentUser() me: User): string {
return "This action returns all my cats";
}
}
@Resolver("dogs")
export class DogsResolver {
@Query(() => String)
@UseGuards(AuthenticationGuard)
findAll(@CurrentUser() me: User): string {
return "This action returns all my dogs";
}
}Here is an example that shows how to use authorization:
import { UseGuards } from "@nestjs/common";
import { Args, Query, Resolver } from "@nestjs/graphql";
import { User } from "@nestjs-cognito/auth";
import {
Authorization,
AuthorizationGuard,
CurrentUser,
} from "@nestjs-cognito/graphql";
@Resolver("dogs")
@Authorization({
allowedGroups: ["user", "admin"],
requiredGroups: ["moderator"],
prohibitedGroups: ["visitor"],
})
export class DogsResolver {
@Query(() => String)
findAll(@CurrentUser() me: User): string {
return "This action returns all my dogs";
}
}
@Resolver("cats")
@Authorization(["user"]) // allowedGroups by default
export class CatsResolver {
@Query(() => String)
findAll(@CurrentUser() me: User): string {
return "This action returns all my cats";
}
}
@Resolver("cats")
@UseGuards(
AuthorizationGuard({
allowedGroups: ["user", "admin"],
requiredGroups: ["moderator"],
prohibitedGroups: ["visitor"],
})
)
export class CatsResolver {
@Query(() => String)
findAll(@CurrentUser() me: User): string {
return "This action returns all my cats";
}
}
@Resolver("cats")
export class CatsResolver {
@Query(() => String)
@UseGuards(AuthorizationGuard(["user", "admin"]))
findAll(@CurrentUser() me: User): string {
return "This action returns all my cats";
}
}License
@nestjs-cognito/graphql is MIT licensed.