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 { IPostsService } from './posts.interface' ; import { GqlAuthGuard } from '../auth/gql-auth.guard' ; import { CurrentUser } from '../auth/user.decorator' ; import { User , PostPayload , UpdatePostInput , DeletePostPayload } from '../graphql/typings' ; import { PostDto } from './post.dto' ; @ Resolver ( ) export class PostsMutationResolver implements OnModuleInit { constructor ( @ Inject ( 'PostsServiceClient' ) private readonly postsServiceClient , @ Inject ( 'PubSubService' ) private readonly pubSubService , private readonly logger ) { logger . setContext ( PostsMutationResolver . name ) ; } private postsService : IPostsService ; onModuleInit ( ) { this . postsService = this . postsServiceClient . getService < IPostsService > ( 'PostsService' ) ; } @ Mutation ( ) @ UseGuards ( GqlAuthGuard ) async createPost ( @ CurrentUser ( ) user , @ Args ( 'data' ) data ) { const post = await this . postsService . create ( { ... data , author : user . id } ) . toPromise ( ) ; this . pubSubService . publish ( 'postAdded' , post ) ; return { post } ; } @ Mutation ( ) @ UseGuards ( GqlAuthGuard ) async updatePost ( @ CurrentUser ( ) user , @ Args ( 'id' ) id , @ Args ( 'data' ) data ) { const metadata = new Metadata ( ) ; metadata . add ( 'user' , user . id ) ; const post = await this . postsService . update ( { id , data : { ... data } } , metadata ) . toPromise ( ) ; return { post } ; } @ Mutation ( ) @ UseGuards ( GqlAuthGuard ) async deletePost ( @ CurrentUser ( ) user , @ Args ( 'id' ) id ) { return this . postsService . destroy ( { where : JSON . stringify ( { id , author : user . id } ) } ) . toPromise ( ) ; } }