Skip to content
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

feat: add pagination support to graphql query #113

Merged
merged 2 commits into from
Jan 17, 2024
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { ObjectType, Field, Int } from '@nestjs/graphql';
import { Notification } from '../entities/notification.entity';

@ObjectType()
export class NotificationResponse {
@Field(() => [Notification])
notifications: Notification[];

@Field(() => Int)
total: number;

@Field(() => Int)
offset: number;

@Field(() => Int)
limit: number;
}
17 changes: 17 additions & 0 deletions apps/api/src/modules/notifications/dtos/query-options.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Field, Int, InputType } from '@nestjs/graphql';
import { IsOptional, IsInt, Min } from 'class-validator';

@InputType()
export class QueryOptionsDto {
@Field(() => Int, { defaultValue: 0 })
@IsOptional()
@IsInt()
@Min(0)
offset?: number = 0;

@Field(() => Int, { defaultValue: 10 })
@IsOptional()
@IsInt()
@Min(1)
limit?: number = 10;
}
13 changes: 9 additions & 4 deletions apps/api/src/modules/notifications/notifications.resolver.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
import { Query, Resolver } from '@nestjs/graphql';
import { Args, Query, Resolver } from '@nestjs/graphql';
import { NotificationsService } from './notifications.service';
import { Notification } from './entities/notification.entity';
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import { ApiKeyGuard } from 'src/common/guards/api-key/api-key.guard';
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import { UseGuards } from '@nestjs/common';
import { NotificationResponse } from './dtos/notification-response.dto';
import { QueryOptionsDto } from './dtos/query-options.dto';

@Resolver(() => Notification)
@UseGuards(ApiKeyGuard)
export class NotificationsResolver {
constructor(private readonly notificationsService: NotificationsService) {}

@Query(() => [Notification], { name: 'notifications' })
async findAll(): Promise<Notification[]> {
return this.notificationsService.getAllNotifications();
@Query(() => NotificationResponse, { name: 'notifications' })
async findAll(
@Args('options', { type: () => QueryOptionsDto, nullable: true, defaultValue: {} })
options: QueryOptionsDto,
): Promise<NotificationResponse> {
return this.notificationsService.getAllNotifications(options);
}
}
12 changes: 9 additions & 3 deletions apps/api/src/modules/notifications/notifications.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { NotificationQueueProducer } from 'src/jobs/producers/notifications/noti
import { Status } from 'src/common/constants/database';
import { CreateNotificationDto } from './dtos/create-notification.dto';
import { ConfigService } from '@nestjs/config';
import { QueryOptionsDto } from './dtos/query-options.dto';
import { NotificationResponse } from './dtos/notification-response.dto';

@Injectable()
export class NotificationsService {
Expand Down Expand Up @@ -98,12 +100,16 @@ export class NotificationsService {
});
}

getAllNotifications(): Promise<Notification[]> {
this.logger.log('Getting all active notifications');
return this.notificationRepository.find({
async getAllNotifications(options: QueryOptionsDto): Promise<NotificationResponse> {
this.logger.log('Getting all active notifications with options');
const [notifications, total] = await this.notificationRepository.findAndCount({
where: {
status: Status.ACTIVE,
},
skip: options.offset,
take: options.limit,
});

return { notifications, total, offset: options.offset, limit: options.limit };
}
}
Loading