import { Inject , OnModuleInit , UseGuards } from '@nestjs/common' ; import { ClientGrpcProxy } from '@nestjs/microservices' ; import { Resolver , Args , Mutation } from '@nestjs/graphql' ; import { Metadata } from 'grpc' ; import { PinoLogger } from 'nestjs-pino' ; import { PubSub } from 'graphql-subscriptions' ; import { CommentDto } from './comment.dto' ; import { ICommentsService } from './comments.interface' ; import { GqlAuthGuard } from '../auth/gql-auth.guard' ; import { CurrentUser } from '../auth/user.decorator' ; import { Comment , User , CommentPayload , UpdateCommentInput , DeleteCommentPayload } from '../graphql/typings' ; @ Resolver ( ) export class CommentsMutationResolver implements OnModuleInit { constructor ( @ Inject ( 'CommentsServiceClient' ) private readonly commentsServiceClient , @ Inject ( 'PubSubService' ) private readonly pubSubService , private readonly logger ) { logger . setContext ( CommentsMutationResolver . name ) ; } private commentsService : ICommentsService ; onModuleInit ( ) { this . commentsService = this . commentsServiceClient . getService < ICommentsService > ( 'CommentsService' ) ; } @ Mutation ( ) @ UseGuards ( GqlAuthGuard ) async createComment ( @ CurrentUser ( ) user , @ Args ( 'data' ) data ) { const comment = await this . commentsService . create ( { ... data , author : user . id } ) . toPromise ( ) ; this . pubSubService . publish ( 'commentAdded' , comment ) ; return { comment } ; } @ Mutation ( ) @ UseGuards ( GqlAuthGuard ) async updateComment ( @ CurrentUser ( ) user , @ Args ( 'id' ) id , @ Args ( 'data' ) data ) { const metadata = new Metadata ( ) ; metadata . add ( 'user' , user . id ) ; const comment = await this . commentsService . update ( { id , data : { ... data } } , metadata ) . toPromise ( ) ; return { comment } ; } @ Mutation ( ) @ UseGuards ( GqlAuthGuard ) async deleteComment ( @ CurrentUser ( ) user , @ Args ( 'id' ) id ) { return this . commentsService . destroy ( { where : JSON . stringify ( { id , author : user . id } ) } ) . toPromise ( ) ; } }