import { Inject , OnModuleInit , UseGuards } from '@nestjs/common' ; import { ClientGrpcProxy } from '@nestjs/microservices' ; import { Query , Resolver , Args , Context } from '@nestjs/graphql' ; import { isEmpty , merge } from 'lodash' ; import { PinoLogger } from 'nestjs-pino' ; import { IPostsService } from './posts.interface' ; import { GqlAuthGuard } from '../auth/gql-auth.guard' ; import { Post , PostsConnection } from '../graphql/typings' ; import { QueryUtils } from '../utils/query.utils' ; @ Resolver ( ) export class PostsQueryResolver implements OnModuleInit { constructor ( @ Inject ( 'PostsServiceClient' ) private readonly postsServiceClient , private readonly queryUtils , private readonly logger ) { logger . setContext ( PostsQueryResolver . name ) ; } private postsService : IPostsService ; onModuleInit ( ) { this . postsService = this . postsServiceClient . getService < IPostsService > ( 'PostsService' ) ; } @ Query ( 'posts' ) async getPosts ( @ Args ( 'q' ) q , @ Args ( 'first' ) first , @ Args ( 'last' ) last , @ Args ( 'before' ) before , @ Args ( 'after' ) after , @ Args ( 'filterBy' ) filterBy , @ Args ( 'orderBy' ) orderBy ) { const query = { where : { } } ; if ( ! isEmpty ( q ) ) merge ( query , { where : { title : { _iLike : q } } } ) ; merge ( query , await this . queryUtils . buildQuery ( filterBy , orderBy , first , last , before , after ) ) ; return this . postsService . find ( { ... query , where : JSON . stringify ( query . where ) } ) . toPromise ( ) ; } @ Query ( 'post' ) async getPost ( @ Args ( 'id' ) id ) { return this . postsService . findById ( { id } ) . toPromise ( ) ; } @ Query ( 'postCount' ) async getPostCount ( @ Args ( 'q' ) q , @ Args ( 'filterBy' ) filterBy ) { const query = { where : { } } ; if ( ! isEmpty ( q ) ) merge ( query , { where : { title : { _iLike : q } } } ) ; merge ( query , await this . queryUtils . getFilters ( filterBy ) ) ; const { count } = await this . postsService . count ( { ... query , where : JSON . stringify ( query . where ) } ) . toPromise ( ) ; return count ; } @ Query ( 'myPosts' ) @ UseGuards ( GqlAuthGuard ) async getMyPosts ( @ Context ( ) context , @ Args ( 'q' ) q , @ Args ( 'first' ) first , @ Args ( 'last' ) last , @ Args ( 'before' ) before , @ Args ( 'after' ) after , @ Args ( 'filterBy' ) filterBy , @ Args ( 'orderBy' ) orderBy ) { this . logger . info ( '========USER %o' , context . req . user ) ; const query = { where : { author : context . req . user . id } } ; if ( ! isEmpty ( q ) ) merge ( query , { where : { title : { _iLike : q } } } ) ; merge ( query , await this . queryUtils . buildQuery ( filterBy , orderBy , first , last , before , after ) ) ; return this . postsService . find ( { ... query , where : JSON . stringify ( query . where ) } ) . toPromise ( ) ; } }