Skip to content

Commit

Permalink
feat: ✨ follow/unfollow에 대해 성공했을 시 subscription 추가
Browse files Browse the repository at this point in the history
- #405

refactor: ♻️ 프론트 테스트용 임시 함수 리팩토링

- #405

refactor: ♻️ type-only import 추가

- #405

fix: 🐛 mutation이 아닌 쿼리에 대해서 mutation -> query 변경

- #405
  • Loading branch information
niamu01 committed Feb 20, 2024
1 parent 18749ea commit 58fc13e
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 42 deletions.
1 change: 1 addition & 0 deletions app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"graphql": "^16.8.1",
"graphql-query-complexity": "^0.12.0",
"graphql-scalars": "^1.22.2",
"graphql-subscriptions": "^2.0.0",
"lru-cache": "^10.0.0",
"mongoose": "^7.3.3",
"reflect-metadata": "^0.1.13",
Expand Down
12 changes: 12 additions & 0 deletions app/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions app/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,14 @@ import { TeamInfoModule } from './page/teamInfo/teamInfo.module';
numberScalarMode: 'integer',
},
autoSchemaFile: join(process.cwd(), 'src/schema.gql'),
subscriptions: {
'graphql-ws': {
path: '/graphql',
},
'subscriptions-transport-ws': {
path: '/graphql',
},
},
};
},
}),
Expand Down
40 changes: 34 additions & 6 deletions app/src/follow/follow.resolve.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,29 @@
import { UseFilters, UseGuards } from '@nestjs/common';
import { Args, Mutation, Resolver } from '@nestjs/graphql';
import { Args, Mutation, Query, Resolver, Subscription } from '@nestjs/graphql';
import { PubSub } from 'graphql-subscriptions';
import { MyUserId } from 'src/auth/myContext';
import { StatAuthGuard } from 'src/auth/statAuthGuard';
import { HttpExceptionFilter } from 'src/http-exception.filter';
import { FollowService } from './follow.service';
import { FollowListWithCount, FollowResult } from './model/follow.model';

const pubSub = new PubSub();

@UseFilters(HttpExceptionFilter)
@UseGuards(StatAuthGuard)
@Resolver()
export class FollowResolver {
constructor(private readonly followService: FollowService) {}

@Subscription((_returns) => FollowResult, {
name: 'followUpdated',
filter: (payload, _variables) => {
return payload.followUpdated.message === 'OK';
},
})
followUpdated() {
return pubSub.asyncIterator('followUpdated');
}

@Mutation((_returns) => FollowResult, {
description: '프론트 테스트용 임시 함수',
})
Expand All @@ -23,23 +35,38 @@ export class FollowResolver {
return await this.followService.MakeFollowUser(to, from, type);
}

@UseGuards(StatAuthGuard)
@Mutation((_returns) => FollowResult)
async followUser(
@MyUserId() userId: number,
@Args('target') target: string,
): Promise<typeof FollowResult> {
return await this.followService.followUser(userId, target);
const followResult = await this.followService.followUser(userId, target);

if (followResult.message === 'OK') {
pubSub.publish('followUpdated', { followUpdated: followResult });
}

return followResult;
}

@UseGuards(StatAuthGuard)
@Mutation((_returns) => FollowResult)
async unfollowUser(
@MyUserId() userId: number,
@Args('target') target: string,
): Promise<typeof FollowResult> {
return await this.followService.unfollowUser(userId, target);
const followResult = await this.followService.unfollowUser(userId, target);

if (followResult.message === 'OK') {
pubSub.publish('followUpdated', { followUpdated: followResult });
}

return followResult;
}

@Mutation((_returns) => FollowListWithCount)
@UseGuards(StatAuthGuard)
@Query((_returns) => FollowListWithCount)
async getFollowerList(
@MyUserId() userId: number,
@Args('target') target: string,
Expand All @@ -56,7 +83,8 @@ export class FollowResolver {
};
}

@Mutation((_returns) => FollowListWithCount)
@UseGuards(StatAuthGuard)
@Query((_returns) => FollowListWithCount)
async getFollowingList(
@MyUserId() userId: number,
@Args('target') target: string,
Expand Down
38 changes: 4 additions & 34 deletions app/src/follow/follow.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
} from 'src/database/mongoose/database.mongoose.query';
import { CursusUserService } from '../api/cursusUser/cursusUser.service';
import { follow } from './db/follow.database.schema';
import { FollowList, FollowResult } from './model/follow.model';
import type { FollowList, FollowResult } from './model/follow.model';

@Injectable()
export class FollowService {
Expand All @@ -28,41 +28,11 @@ export class FollowService {
from: string,
type: 'follow' | 'unfollow',
): Promise<typeof FollowResult> {
const toId = await this.cursusUserService
.findOneAndLean({
filter: { 'user.login': to },
})
.then((following) => following?.user.id);

const fromId = await this.cursusUserService
.findOneAndLean({
filter: { 'user.login': from },
})
.then((following) => following?.user.id);

const userId = await this.cursusUserService.getuserIdByLogin(from);
if (type === 'follow') {
await this.followModel
.create({ userId: fromId, followId: toId })
.then((result) => result.toObject());

return {
message: 'OK',
userId: fromId!,
followId: toId!,
};
return await this.followUser(userId!, to);
} else if (type === 'unfollow') {
await this.followModel
.deleteOne({
userId: fromId,
followId: toId,
})
.then((result) => result.deletedCount);

return {
message: 'OK',
userId: fromId!,
followId: toId!,
};
return await this.unfollowUser(userId!, to);
}
return { message: 'fail' };
}
Expand Down
8 changes: 6 additions & 2 deletions app/src/schema.gql
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,8 @@ type Query {
getEvalLogs(after: String, first: Int! = 20, corrector: String, corrected: String, projectName: String, outstandingOnly: Boolean! = false, imperfectOnly: Boolean! = false, sortOrder: EvalLogSortOrder! = BEGIN_AT_DESC): EvalLogsPaginated!
getSetting: Setting!
getExpTable: [ExpTable!]!
getFollowerList(target: String!): FollowListWithCount!
getFollowingList(target: String!): FollowListWithCount!
}

enum EvalLogSortOrder {
Expand All @@ -651,8 +653,6 @@ type Mutation {
MakeFollow(to: String!, from: String!, type: String!): FollowResult!
followUser(target: String!): FollowResult!
unfollowUser(target: String!): FollowResult!
getFollowerList(target: String!): FollowListWithCount!
getFollowingList(target: String!): FollowListWithCount!
}

union LoginResult = LoginSuccess | LoginNotLinked
Expand All @@ -676,4 +676,8 @@ type FollowSuccess {

type FollowFail {
message: String!
}

type Subscription {
followUpdated: FollowResult!
}

0 comments on commit 58fc13e

Please sign in to comment.