Skip to content

Commit

Permalink
feat: ✨ follow/unfollow에 대해 성공했을 시 subscription 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
niamu01 committed Dec 27, 2023
1 parent d7e74f5 commit d81a908
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 4 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
36 changes: 32 additions & 4 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, 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,22 +35,37 @@ 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;
}

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

@UseGuards(StatAuthGuard)
@Mutation((_returns) => FollowListWithCount)
async getFollowingList(
@MyUserId() userId: number,
Expand Down
4 changes: 4 additions & 0 deletions app/src/schema.gql
Original file line number Diff line number Diff line change
Expand Up @@ -676,4 +676,8 @@ type FollowSuccess {

type FollowFail {
message: String!
}

type Subscription {
followUpdated: FollowResult!
}

0 comments on commit d81a908

Please sign in to comment.