Skip to content

Commit df90c93

Browse files
committed
refactor: ♻️ unionType -> throw Exception
- #406
1 parent f268b91 commit df90c93

File tree

4 files changed

+37
-76
lines changed

4 files changed

+37
-76
lines changed

app/src/follow/follow.resolver.ts

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import { UseFilters, UseGuards } from '@nestjs/common';
1+
import { NotFoundException, UseFilters, UseGuards } from '@nestjs/common';
22
import { Args, Mutation, Query, Resolver, Subscription } from '@nestjs/graphql';
33
import { PubSub } from 'graphql-subscriptions';
44
import { MyUserId } from 'src/auth/myContext';
55
import { StatAuthGuard } from 'src/auth/statAuthGuard';
66
import { HttpExceptionFilter } from 'src/http-exception.filter';
77
import { FollowListPaginatedArgs } from './dto/follow.dto.getFollowList';
88
import { FollowService } from './follow.service';
9-
import { FollowListPaginated, FollowResult } from './model/follow.model';
9+
import { FollowListPaginated, FollowSuccess } from './model/follow.model';
1010

1111
const pubSub = new PubSub();
1212

@@ -15,44 +15,46 @@ const pubSub = new PubSub();
1515
export class FollowResolver {
1616
constructor(private readonly followService: FollowService) {}
1717

18-
@Subscription((_returns) => FollowResult, {
19-
name: 'followUpdated',
20-
filter: (payload, _variables) => {
21-
return payload.followUpdated.message === 'OK';
22-
},
23-
})
18+
@Subscription((_returns) => FollowSuccess, { name: 'followUpdated' })
2419
followUpdated() {
2520
return pubSub.asyncIterator('followUpdated');
2621
}
2722

2823
@UseGuards(StatAuthGuard)
29-
@Mutation((_returns) => FollowResult)
24+
@Mutation((_returns) => FollowSuccess)
3025
async followUser(
3126
@MyUserId() userId: number,
3227
@Args('target') target: string,
33-
): Promise<typeof FollowResult> {
34-
const followResult = await this.followService.followUser(userId, target);
28+
): Promise<FollowSuccess> {
29+
try {
30+
const followResult = await this.followService.followUser(userId, target);
3531

36-
if (followResult.message === 'OK') {
3732
await pubSub.publish('followUpdated', { followUpdated: followResult });
38-
}
3933

40-
return followResult;
34+
return followResult;
35+
} catch (e) {
36+
throw new NotFoundException();
37+
}
4138
}
4239

4340
@UseGuards(StatAuthGuard)
44-
@Mutation((_returns) => FollowResult)
41+
@Mutation((_returns) => FollowSuccess)
4542
async unfollowUser(
4643
@MyUserId() userId: number,
4744
@Args('target') target: string,
48-
): Promise<typeof FollowResult> {
49-
const followResult = await this.followService.unfollowUser(userId, target);
45+
): Promise<FollowSuccess> {
46+
try {
47+
const followResult = await this.followService.unfollowUser(
48+
userId,
49+
target,
50+
);
5051

51-
if (followResult.message === 'OK') {
5252
await pubSub.publish('followUpdated', { followUpdated: followResult });
53-
}
5453

55-
return followResult;
54+
return followResult;
55+
} catch (e) {
56+
throw new NotFoundException();
57+
}
5658
}
5759

5860
@UseGuards(StatAuthGuard)

app/src/follow/follow.service.ts

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@ import {
88
findAllAndLean,
99
findOneAndLean,
1010
} from 'src/database/mongoose/database.mongoose.query';
11-
import {
12-
CursorExtractor,
13-
FieldExtractor,
14-
} from 'src/pagination/cursor/pagination.cursor.service';
1511
import { PaginationIndexService } from 'src/pagination/index/pagination.index.service';
1612
import { CursusUserService } from '../api/cursusUser/cursusUser.service';
1713
import { follow } from './db/follow.database.schema';
@@ -22,7 +18,7 @@ import {
2218
import type {
2319
FollowList,
2420
FollowListPaginated,
25-
FollowResult,
21+
FollowSuccess,
2622
} from './model/follow.model';
2723

2824
type FollowListCursorField = [number, string];
@@ -56,10 +52,7 @@ export class FollowService {
5652
return id;
5753
}
5854

59-
async followUser(
60-
userId: number,
61-
target: string,
62-
): Promise<typeof FollowResult> {
55+
async followUser(userId: number, target: string): Promise<FollowSuccess> {
6356
const following = await this.cursusUserService.getuserIdByLogin(target);
6457

6558
const alreadyFollow = await this.followModel.findOne(
@@ -71,7 +64,7 @@ export class FollowService {
7164
);
7265

7366
if (!following || userId === following || alreadyFollow) {
74-
return { message: 'fail' };
67+
throw new NotFoundException();
7568
}
7669

7770
const result = await this.followModel.create({
@@ -81,20 +74,16 @@ export class FollowService {
8174
});
8275

8376
return {
84-
message: 'OK',
8577
userId: result.userId,
8678
followId: result.followId,
8779
};
8880
}
8981

90-
async unfollowUser(
91-
userId: number,
92-
target: string,
93-
): Promise<typeof FollowResult> {
82+
async unfollowUser(userId: number, target: string): Promise<FollowSuccess> {
9483
const following = await this.cursusUserService.getuserIdByLogin(target);
9584

9685
if (!following || userId === following) {
97-
return { message: 'fail' };
86+
throw new NotFoundException();
9887
}
9988

10089
const deletedCount = await this.followModel
@@ -106,13 +95,12 @@ export class FollowService {
10695

10796
if (deletedCount === 1) {
10897
return {
109-
message: 'OK',
11098
userId: userId,
11199
followId: following,
112100
};
113101
}
114102

115-
return { message: 'fail' };
103+
throw new NotFoundException();
116104
}
117105

118106
async followerList(

app/src/follow/model/follow.model.ts

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Field, ObjectType, createUnionType } from '@nestjs/graphql';
1+
import { Field, ObjectType } from '@nestjs/graphql';
22
import { UserPreview } from 'src/common/models/common.user.model';
33
import { IndexPaginated } from 'src/pagination/index/models/pagination.index.model';
44

@@ -23,33 +23,11 @@ export class FollowListWithCount {
2323
count: number;
2424
}
2525

26-
@ObjectType()
27-
export class FollowFail {
28-
@Field()
29-
message: 'fail';
30-
}
31-
3226
@ObjectType()
3327
export class FollowSuccess {
34-
@Field()
35-
message: 'OK';
36-
3728
@Field()
3829
userId: number;
3930

4031
@Field()
4132
followId: number;
4233
}
43-
44-
export const FollowResult = createUnionType({
45-
name: 'FollowResult',
46-
types: () => [FollowSuccess, FollowFail] as const,
47-
resolveType(value) {
48-
switch (value.message) {
49-
case 'OK':
50-
return FollowSuccess;
51-
case 'fail':
52-
return FollowFail;
53-
}
54-
},
55-
});

app/src/schema.gql

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ type FollowListPaginated {
7171
pageNumber: Int!
7272
}
7373

74+
type FollowSuccess {
75+
userId: Int!
76+
followId: Int!
77+
}
78+
7479
type LinkableAccount {
7580
platform: String!
7681
id: String!
@@ -656,8 +661,8 @@ type Mutation {
656661
refreshToken(refreshToken: String!): LoginSuccess!
657662
logout: Int!
658663
deleteAccount: Int!
659-
followUser(target: String!): FollowResult!
660-
unfollowUser(target: String!): FollowResult!
664+
followUser(target: String!): FollowSuccess!
665+
unfollowUser(target: String!): FollowSuccess!
661666
}
662667

663668
union LoginResult = LoginSuccess | LoginNotLinked
@@ -671,18 +676,6 @@ input GoogleLoginInput {
671676
credential: String!
672677
}
673678

674-
union FollowResult = FollowSuccess | FollowFail
675-
676-
type FollowSuccess {
677-
message: String!
678-
userId: Int!
679-
followId: Int!
680-
}
681-
682-
type FollowFail {
683-
message: String!
684-
}
685-
686679
type Subscription {
687-
followUpdated: FollowResult!
680+
followUpdated: FollowSuccess!
688681
}

0 commit comments

Comments
 (0)