import { Inject , OnModuleInit } from '@nestjs/common' ; import { ClientGrpcProxy } from '@nestjs/microservices' ; import { Resolver , Args , Parent , ResolveField } from '@nestjs/graphql' ; import { isEmpty , merge } from 'lodash' ; import { PinoLogger } from 'nestjs-pino' ; import { ICommentsService } from '../comments/comments.interface' ; import { IPostsService } from '../posts/posts.interface' ; import { CommentsConnection , User , PostsConnection } from '../graphql/typings' ; import { QueryUtils } from '../utils/query.utils' ; @ Resolver ( 'User' ) export class UsersTypeResolver implements OnModuleInit { constructor ( @ Inject ( 'CommentsServiceClient' ) private readonly commentsServiceClient , @ Inject ( 'PostsServiceClient' ) private readonly postsServiceClient , private readonly queryUtils , private readonly logger ) { logger . setContext ( UsersTypeResolver . name ) ; } private commentsService : ICommentsService ; private postsService : IPostsService ; onModuleInit ( ) { this . commentsService = this . commentsServiceClient . getService < ICommentsService > ( 'CommentsService' ) ; this . postsService = this . postsServiceClient . getService < IPostsService > ( 'PostsService' ) ; } @ ResolveField ( 'posts' ) async getPosts ( @ Parent ( ) user , @ Args ( 'q' ) q , @ Args ( 'first' ) first , @ Args ( 'last' ) last , @ Args ( 'before' ) before , @ Args ( 'after' ) after , @ Args ( 'filterBy' ) filterBy , @ Args ( 'orderBy' ) orderBy ) { const query = { where : { author : 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 ( ) ; } @ ResolveField ( 'comments' ) async getComments ( @ Parent ( ) user , @ Args ( 'q' ) q , @ Args ( 'first' ) first , @ Args ( 'last' ) last , @ Args ( 'before' ) before , @ Args ( 'after' ) after , @ Args ( 'filterBy' ) filterBy , @ Args ( 'orderBy' ) orderBy ) { const query = { where : { author : user . id } } ; if ( ! isEmpty ( q ) ) merge ( query , { where : { text : { _iLike : q } } } ) ; merge ( query , await this . queryUtils . buildQuery ( filterBy , orderBy , first , last , before , after ) ) ; return this . commentsService . find ( { ... query , where : JSON . stringify ( query . where ) } ) . toPromise ( ) ; } }