Skip to content

refactor(query-graphql): implement global authorizer #1442

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions examples/auth/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ import { SubTaskModule } from './sub-task/sub-task.module';
import { typeormOrmConfig } from '../../helpers';
import { AuthModule } from './auth/auth.module';
import { UserModule } from './user/user.module';
import { NestjsQueryGraphQLModule } from '@nestjs-query/query-graphql';

interface HeadersContainer {
headers?: Record<string, string>;
rawHeaders: string[];
headers: Record<string, string>;
}
interface ContextArgs {
req?: HeadersContainer;
Expand All @@ -19,6 +21,7 @@ interface ContextArgs {
@Module({
imports: [
TypeOrmModule.forRoot(typeormOrmConfig('auth')),
NestjsQueryGraphQLModule.forRoot(),
GraphQLModule.forRoot({
autoSchemaFile: 'schema.gql',
installSubscriptionHandlers: true,
Expand All @@ -27,7 +30,22 @@ interface ContextArgs {
onConnect: (connectionParams: unknown) => ({ headers: connectionParams }),
},
},
context: ({ req, connection }: ContextArgs) => ({ req: { ...req, ...connection?.context } }),
context: ({ req, connection }: ContextArgs) => {
let headers = {};
const idx = req?.rawHeaders?.findIndex((h) => h === 'Authorization');
if (idx && req) {
headers = {
Authorization: req.rawHeaders[idx + 1],
};
}
return {
req: {
...req,
...connection?.context,
headers: { ...headers, ...req?.headers },
},
};
},
}),
AuthModule,
UserModule,
Expand Down
2 changes: 1 addition & 1 deletion examples/auth/src/auth/auth.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class AuthResolver {
async login(@Args('input') input: LoginInputDTO): Promise<LoginResponseDto> {
const user = await this.authService.validateUser(input.username, input.password);
if (!user) {
throw new UnauthorizedException();
throw new UnauthorizedException('NO USER');
}
return this.authService.login(user);
}
Expand Down
2 changes: 1 addition & 1 deletion examples/auth/src/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export class AuthService {
const user = await this.usersService.getById(authUser.id);
return user;
} catch (e) {
throw new UnauthorizedException();
throw new UnauthorizedException('TEST');
}
}

Expand Down
11 changes: 5 additions & 6 deletions examples/auth/src/todo-item/todo-item.resolver.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
import { Filter, InjectAssemblerQueryService, mergeFilter, mergeQuery, QueryService } from '@nestjs-query/core';
import { AuthorizerInterceptor, AuthorizerFilter, ConnectionType, OperationGroup } from '@nestjs-query/query-graphql';
import { AuthorizerFilter, ConnectionType, OperationGroup } from '@nestjs-query/query-graphql';
import { Args, Query, Resolver } from '@nestjs/graphql';
import { UseGuards, UseInterceptors } from '@nestjs/common';
import { UseGuards } from '@nestjs/common';
import { TodoItemDTO } from './dto/todo-item.dto';
import { TodoItemAssembler } from './todo-item.assembler';
import { TodoItemConnection, TodoItemQuery } from './types';
import { JwtAuthGuard } from '../auth/jwt-auth.guard';

@Resolver(() => TodoItemDTO)
@UseGuards(JwtAuthGuard)
@UseInterceptors(AuthorizerInterceptor(TodoItemDTO))
export class TodoItemResolver {
constructor(@InjectAssemblerQueryService(TodoItemAssembler) readonly service: QueryService<TodoItemDTO>) {}

// Set the return type to the TodoItemConnection
@Query(() => TodoItemConnection)
async completedTodoItems(
@Args() query: TodoItemQuery,
@AuthorizerFilter({
@AuthorizerFilter(TodoItemDTO, {
operationGroup: OperationGroup.READ,
many: true,
})
Expand All @@ -37,7 +36,7 @@ export class TodoItemResolver {
@Query(() => TodoItemConnection)
async uncompletedTodoItems(
@Args() query: TodoItemQuery,
@AuthorizerFilter({
@AuthorizerFilter(TodoItemDTO, {
operationName: 'queryUncompletedTodoItems',
operationGroup: OperationGroup.READ,
readonly: true,
Expand All @@ -58,7 +57,7 @@ export class TodoItemResolver {
@Query(() => TodoItemConnection)
async failingTodoItems(
@Args() query: TodoItemQuery,
@AuthorizerFilter() // Intentionally left out argument to test error
@AuthorizerFilter(TodoItemDTO) // Intentionally left out argument to test error
authFilter: Filter<TodoItemDTO>,
): Promise<ConnectionType<TodoItemDTO>> {
// add the completed filter the user provided filter
Expand Down
2 changes: 2 additions & 0 deletions examples/basic/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import { TagModule } from './tag/tag.module';
import { TodoItemModule } from './todo-item/todo-item.module';
import { SubTaskModule } from './sub-task/sub-task.module';
import { typeormOrmConfig } from '../../helpers';
import { NestjsQueryGraphQLModule } from '@nestjs-query/query-graphql';

@Module({
imports: [
TypeOrmModule.forRoot(typeormOrmConfig('basic')),
NestjsQueryGraphQLModule.forRoot(),
GraphQLModule.forRoot({
autoSchemaFile: 'schema.gql',
}),
Expand Down
2 changes: 2 additions & 0 deletions examples/complexity/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import { TagModule } from './tag/tag.module';
import { TodoItemModule } from './todo-item/todo-item.module';
import { SubTaskModule } from './sub-task/sub-task.module';
import { typeormOrmConfig } from '../../helpers';
import { NestjsQueryGraphQLModule } from '@nestjs-query/query-graphql';

@Module({
providers: [ComplexityPlugin],
imports: [
NestjsQueryGraphQLModule.forRoot(),
TypeOrmModule.forRoot(typeormOrmConfig('complexity')),
GraphQLModule.forRoot({
autoSchemaFile: 'schema.gql',
Expand Down
2 changes: 2 additions & 0 deletions examples/custom-id/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import { TagModule } from './tag/tag.module';
import { TodoItemModule } from './todo-item/todo-item.module';
import { SubTaskModule } from './sub-task/sub-task.module';
import { typeormOrmConfig } from '../../helpers';
import { NestjsQueryGraphQLModule } from '@nestjs-query/query-graphql';

@Module({
imports: [
TypeOrmModule.forRoot(typeormOrmConfig('basic')),
NestjsQueryGraphQLModule.forRoot(),
GraphQLModule.forRoot({
autoSchemaFile: 'schema.gql',
}),
Expand Down
2 changes: 2 additions & 0 deletions examples/custom-service/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ import { GraphQLModule } from '@nestjs/graphql';
import { TypeOrmModule } from '@nestjs/typeorm';
import { typeormOrmConfig } from '../../helpers';
import { TodoItemModule } from './todo-item/todo-item.module';
import { NestjsQueryGraphQLModule } from '@nestjs-query/query-graphql';

@Module({
imports: [
NestjsQueryGraphQLModule.forRoot(),
TypeOrmModule.forRoot(typeormOrmConfig('custom_service')),
GraphQLModule.forRoot({
autoSchemaFile: 'schema.gql',
Expand Down
2 changes: 2 additions & 0 deletions examples/federation/sub-task-graphql/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ import { GraphQLFederationModule } from '@nestjs/graphql';
import { TypeOrmModule } from '@nestjs/typeorm';
import { SubTaskModule } from './sub-task/sub-task.module';
import { typeormOrmConfig } from '../../../helpers';
import { NestjsQueryGraphQLModule } from '@nestjs-query/query-graphql';

@Module({
imports: [
NestjsQueryGraphQLModule.forRoot(),
TypeOrmModule.forRoot(typeormOrmConfig('federation_sub_task')),
GraphQLFederationModule.forRoot({
autoSchemaFile: 'schema.gql',
Expand Down
2 changes: 2 additions & 0 deletions examples/federation/tag-graphql/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ import { GraphQLFederationModule } from '@nestjs/graphql';
import { TypeOrmModule } from '@nestjs/typeorm';
import { TagModule } from './tag/tag.module';
import { typeormOrmConfig } from '../../../helpers';
import { NestjsQueryGraphQLModule } from '@nestjs-query/query-graphql';

@Module({
imports: [
NestjsQueryGraphQLModule.forRoot(),
TypeOrmModule.forRoot(typeormOrmConfig('federation_tag')),
GraphQLFederationModule.forRoot({
autoSchemaFile: 'schema.gql',
Expand Down
2 changes: 2 additions & 0 deletions examples/federation/todo-item-graphql/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ import { GraphQLFederationModule } from '@nestjs/graphql';
import { TypeOrmModule } from '@nestjs/typeorm';
import { TodoItemModule } from './todo-item/todo-item.module';
import { typeormOrmConfig } from '../../../helpers';
import { NestjsQueryGraphQLModule } from '@nestjs-query/query-graphql';

@Module({
imports: [
NestjsQueryGraphQLModule.forRoot(),
TypeOrmModule.forRoot(typeormOrmConfig('federation_todo_item')),
GraphQLFederationModule.forRoot({
autoSchemaFile: 'schema.gql',
Expand Down
2 changes: 2 additions & 0 deletions examples/federation/user-graphql/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ import { GraphQLFederationModule } from '@nestjs/graphql';
import { TypeOrmModule } from '@nestjs/typeorm';
import { typeormOrmConfig } from '../../../helpers';
import { UserModule } from './user/user.module';
import { NestjsQueryGraphQLModule } from '@nestjs-query/query-graphql';

@Module({
imports: [
NestjsQueryGraphQLModule.forRoot(),
TypeOrmModule.forRoot(typeormOrmConfig('federation_user')),
GraphQLFederationModule.forRoot({
autoSchemaFile: 'schema.gql',
Expand Down
2 changes: 2 additions & 0 deletions examples/hooks/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import { TagModule } from './tag/tag.module';
import { TodoItemModule } from './todo-item/todo-item.module';
import { SubTaskModule } from './sub-task/sub-task.module';
import { typeormOrmConfig } from '../../helpers';
import { NestjsQueryGraphQLModule } from '@nestjs-query/query-graphql';

@Module({
imports: [
NestjsQueryGraphQLModule.forRoot(),
TypeOrmModule.forRoot(typeormOrmConfig('hooks')),
GraphQLModule.forRoot({
autoSchemaFile: 'schema.gql',
Expand Down
2 changes: 2 additions & 0 deletions examples/mongoose/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import { TagModule } from './tag/tag.module';
import { TodoItemModule } from './todo-item/todo-item.module';
import { SubTaskModule } from './sub-task/sub-task.module';
import { mongooseConfig } from '../../helpers';
import { NestjsQueryGraphQLModule } from '@nestjs-query/query-graphql';

const { uri, ...options } = mongooseConfig('mongoose');
@Module({
imports: [
NestjsQueryGraphQLModule.forRoot(),
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
MongooseModule.forRoot(uri!, options),
GraphQLModule.forRoot({
Expand Down
2 changes: 2 additions & 0 deletions examples/no-paging/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import { TagModule } from './tag/tag.module';
import { TodoItemModule } from './todo-item/todo-item.module';
import { SubTaskModule } from './sub-task/sub-task.module';
import { typeormOrmConfig } from '../../helpers';
import { NestjsQueryGraphQLModule } from '@nestjs-query/query-graphql';

@Module({
imports: [
NestjsQueryGraphQLModule.forRoot(),
TypeOrmModule.forRoot(typeormOrmConfig('no_paging')),
GraphQLModule.forRoot({
autoSchemaFile: 'schema.gql',
Expand Down
2 changes: 2 additions & 0 deletions examples/offset-paging/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import { TagModule } from './tag/tag.module';
import { TodoItemModule } from './todo-item/todo-item.module';
import { SubTaskModule } from './sub-task/sub-task.module';
import { typeormOrmConfig } from '../../helpers';
import { NestjsQueryGraphQLModule } from '@nestjs-query/query-graphql';

@Module({
imports: [
NestjsQueryGraphQLModule.forRoot(),
TypeOrmModule.forRoot(typeormOrmConfig('limit_offset')),
GraphQLModule.forRoot({
autoSchemaFile: 'schema.gql',
Expand Down
2 changes: 2 additions & 0 deletions examples/sequelize/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import { TagModule } from './tag/tag.module';
import { TodoItemModule } from './todo-item/todo-item.module';
import { SubTaskModule } from './sub-task/sub-task.module';
import { sequelizeOrmConfig } from '../../helpers';
import { NestjsQueryGraphQLModule } from '@nestjs-query/query-graphql';

@Module({
imports: [
NestjsQueryGraphQLModule.forRoot(),
SequelizeModule.forRoot(sequelizeOrmConfig('sequelize')),
GraphQLModule.forRoot({
autoSchemaFile: 'schema.gql',
Expand Down
2 changes: 2 additions & 0 deletions examples/subscriptions/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import { TagModule } from './tag/tag.module';
import { TodoItemModule } from './todo-item/todo-item.module';
import { SubTaskModule } from './sub-task/sub-task.module';
import { typeormOrmConfig } from '../../helpers';
import { NestjsQueryGraphQLModule } from '@nestjs-query/query-graphql';

@Module({
imports: [
NestjsQueryGraphQLModule.forRoot(),
TypeOrmModule.forRoot(typeormOrmConfig('subscription')),
GraphQLModule.forRoot({
subscriptions: {
Expand Down
2 changes: 2 additions & 0 deletions examples/typegoose/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { TagModule } from './tag/tag.module';
import { TodoItemModule } from './todo-item/todo-item.module';
import { SubTaskModule } from './sub-task/sub-task.module';
import { mongooseConfig } from '../../helpers';
import { NestjsQueryGraphQLModule } from '@nestjs-query/query-graphql';

const { uri, ...options } = mongooseConfig('typegoose', {
useNewUrlParser: true,
Expand All @@ -16,6 +17,7 @@ const { uri, ...options } = mongooseConfig('typegoose', {

@Module({
imports: [
NestjsQueryGraphQLModule.forRoot(),
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
TypegooseModule.forRoot(uri!, options),
GraphQLModule.forRoot({
Expand Down
2 changes: 2 additions & 0 deletions examples/typeorm-multidb/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import { TypeOrmModule } from '@nestjs/typeorm';
import { TodoItemModule } from './todo-item/todo-item.module';
import { UserModule } from './user/user.module';
import { typeormOrmConfig } from '../../helpers';
import { NestjsQueryGraphQLModule } from '@nestjs-query/query-graphql';

@Module({
imports: [
NestjsQueryGraphQLModule.forRoot(),
TypeOrmModule.forRoot(typeormOrmConfig('typeorm_multidb_1')),
TypeOrmModule.forRoot(typeormOrmConfig('typeorm_multidb_2', 'typeorm_multidb_2', { name: 'user-connection' })),
GraphQLModule.forRoot({
Expand Down
2 changes: 2 additions & 0 deletions examples/typeorm-soft-delete/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ import { GraphQLModule } from '@nestjs/graphql';
import { TypeOrmModule } from '@nestjs/typeorm';
import { TodoItemModule } from './todo-item/todo-item.module';
import { typeormOrmConfig } from '../../helpers';
import { NestjsQueryGraphQLModule } from '@nestjs-query/query-graphql';

@Module({
imports: [
NestjsQueryGraphQLModule.forRoot(),
TypeOrmModule.forRoot(typeormOrmConfig('typeorm_soft_delete')),
GraphQLModule.forRoot({
autoSchemaFile: 'schema.gql',
Expand Down
2 changes: 2 additions & 0 deletions examples/typeorm/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import { TagModule } from './tag/tag.module';
import { TodoItemModule } from './todo-item/todo-item.module';
import { SubTaskModule } from './sub-task/sub-task.module';
import { typeormOrmConfig } from '../../helpers';
import { NestjsQueryGraphQLModule } from '@nestjs-query/query-graphql';

@Module({
imports: [
NestjsQueryGraphQLModule.forRoot(),
TypeOrmModule.forRoot(typeormOrmConfig('typeorm')),
GraphQLModule.forRoot({
autoSchemaFile: 'schema.gql',
Expand Down
Loading