From 0c767db3dccc8aad94a00454b34b5384557699c4 Mon Sep 17 00:00:00 2001 From: niamu01 Date: Wed, 10 Jan 2024 12:45:48 +0900 Subject: [PATCH] refactor: :recycle: unionType -> throw Exception - #406 --- app/src/follow/follow.resolver.ts | 42 +++++++++++++++------------- app/src/follow/follow.service.ts | 24 ++++------------ app/src/follow/model/follow.model.ts | 24 +--------------- app/src/schema.gql | 23 ++++++--------- 4 files changed, 37 insertions(+), 76 deletions(-) diff --git a/app/src/follow/follow.resolver.ts b/app/src/follow/follow.resolver.ts index d7ce5294..aab86aec 100644 --- a/app/src/follow/follow.resolver.ts +++ b/app/src/follow/follow.resolver.ts @@ -1,4 +1,4 @@ -import { UseFilters, UseGuards } from '@nestjs/common'; +import { NotFoundException, UseFilters, UseGuards } from '@nestjs/common'; import { Args, Mutation, Query, Resolver, Subscription } from '@nestjs/graphql'; import { PubSub } from 'graphql-subscriptions'; import { MyUserId } from 'src/auth/myContext'; @@ -6,7 +6,7 @@ import { StatAuthGuard } from 'src/auth/statAuthGuard'; import { HttpExceptionFilter } from 'src/http-exception.filter'; import { FollowListPaginatedArgs } from './dto/follow.dto.getFollowList'; import { FollowService } from './follow.service'; -import { FollowListPaginated, FollowResult } from './model/follow.model'; +import { FollowListPaginated, FollowSuccess } from './model/follow.model'; const pubSub = new PubSub(); @@ -15,44 +15,46 @@ const pubSub = new PubSub(); export class FollowResolver { constructor(private readonly followService: FollowService) {} - @Subscription((_returns) => FollowResult, { - name: 'followUpdated', - filter: (payload, _variables) => { - return payload.followUpdated.message === 'OK'; - }, - }) + @Subscription((_returns) => FollowSuccess, { name: 'followUpdated' }) followUpdated() { return pubSub.asyncIterator('followUpdated'); } @UseGuards(StatAuthGuard) - @Mutation((_returns) => FollowResult) + @Mutation((_returns) => FollowSuccess) async followUser( @MyUserId() userId: number, @Args('target') target: string, - ): Promise { - const followResult = await this.followService.followUser(userId, target); + ): Promise { + try { + const followResult = await this.followService.followUser(userId, target); - if (followResult.message === 'OK') { await pubSub.publish('followUpdated', { followUpdated: followResult }); - } - return followResult; + return followResult; + } catch (e) { + throw new NotFoundException(); + } } @UseGuards(StatAuthGuard) - @Mutation((_returns) => FollowResult) + @Mutation((_returns) => FollowSuccess) async unfollowUser( @MyUserId() userId: number, @Args('target') target: string, - ): Promise { - const followResult = await this.followService.unfollowUser(userId, target); + ): Promise { + try { + const followResult = await this.followService.unfollowUser( + userId, + target, + ); - if (followResult.message === 'OK') { await pubSub.publish('followUpdated', { followUpdated: followResult }); - } - return followResult; + return followResult; + } catch (e) { + throw new NotFoundException(); + } } @UseGuards(StatAuthGuard) diff --git a/app/src/follow/follow.service.ts b/app/src/follow/follow.service.ts index 0a60c20a..a273e7e4 100644 --- a/app/src/follow/follow.service.ts +++ b/app/src/follow/follow.service.ts @@ -8,10 +8,6 @@ import { findAllAndLean, findOneAndLean, } from 'src/database/mongoose/database.mongoose.query'; -import { - CursorExtractor, - FieldExtractor, -} from 'src/pagination/cursor/pagination.cursor.service'; import { PaginationIndexService } from 'src/pagination/index/pagination.index.service'; import { CursusUserService } from '../api/cursusUser/cursusUser.service'; import { follow } from './db/follow.database.schema'; @@ -22,7 +18,7 @@ import { import type { FollowList, FollowListPaginated, - FollowResult, + FollowSuccess, } from './model/follow.model'; type FollowListCursorField = [number, string]; @@ -56,10 +52,7 @@ export class FollowService { return id; } - async followUser( - userId: number, - target: string, - ): Promise { + async followUser(userId: number, target: string): Promise { const following = await this.cursusUserService.getuserIdByLogin(target); const alreadyFollow = await this.followModel.findOne( @@ -71,7 +64,7 @@ export class FollowService { ); if (!following || userId === following || alreadyFollow) { - return { message: 'fail' }; + throw new NotFoundException(); } const result = await this.followModel.create({ @@ -81,20 +74,16 @@ export class FollowService { }); return { - message: 'OK', userId: result.userId, followId: result.followId, }; } - async unfollowUser( - userId: number, - target: string, - ): Promise { + async unfollowUser(userId: number, target: string): Promise { const following = await this.cursusUserService.getuserIdByLogin(target); if (!following || userId === following) { - return { message: 'fail' }; + throw new NotFoundException(); } const deletedCount = await this.followModel @@ -106,13 +95,12 @@ export class FollowService { if (deletedCount === 1) { return { - message: 'OK', userId: userId, followId: following, }; } - return { message: 'fail' }; + throw new NotFoundException(); } async followerList( diff --git a/app/src/follow/model/follow.model.ts b/app/src/follow/model/follow.model.ts index 2c92d89f..5e5428b0 100644 --- a/app/src/follow/model/follow.model.ts +++ b/app/src/follow/model/follow.model.ts @@ -1,4 +1,4 @@ -import { Field, ObjectType, createUnionType } from '@nestjs/graphql'; +import { Field, ObjectType } from '@nestjs/graphql'; import { UserPreview } from 'src/common/models/common.user.model'; import { IndexPaginated } from 'src/pagination/index/models/pagination.index.model'; @@ -23,33 +23,11 @@ export class FollowListWithCount { count: number; } -@ObjectType() -export class FollowFail { - @Field() - message: 'fail'; -} - @ObjectType() export class FollowSuccess { - @Field() - message: 'OK'; - @Field() userId: number; @Field() followId: number; } - -export const FollowResult = createUnionType({ - name: 'FollowResult', - types: () => [FollowSuccess, FollowFail] as const, - resolveType(value) { - switch (value.message) { - case 'OK': - return FollowSuccess; - case 'fail': - return FollowFail; - } - }, -}); diff --git a/app/src/schema.gql b/app/src/schema.gql index b8545c92..ce930d6f 100644 --- a/app/src/schema.gql +++ b/app/src/schema.gql @@ -71,6 +71,11 @@ type FollowListPaginated { pageNumber: Int! } +type FollowSuccess { + userId: Int! + followId: Int! +} + type LinkableAccount { platform: String! id: String! @@ -656,8 +661,8 @@ type Mutation { refreshToken(refreshToken: String!): LoginSuccess! logout: Int! deleteAccount: Int! - followUser(target: String!): FollowResult! - unfollowUser(target: String!): FollowResult! + followUser(target: String!): FollowSuccess! + unfollowUser(target: String!): FollowSuccess! } union LoginResult = LoginSuccess | LoginNotLinked @@ -671,18 +676,6 @@ input GoogleLoginInput { credential: String! } -union FollowResult = FollowSuccess | FollowFail - -type FollowSuccess { - message: String! - userId: Int! - followId: Int! -} - -type FollowFail { - message: String! -} - type Subscription { - followUpdated: FollowResult! + followUpdated: FollowSuccess! } \ No newline at end of file