import { Resolver , Mutation , Args , Query , Context , Subscription } from '@nestjs/graphql' ; import { getMongoRepository } from 'typeorm' ; import { Notification } from '@entities' ; import { NOTIFICATION_SUBSCRIPTION } from '@environments' ; @ Resolver ( 'Notification' ) export class NotificationResolver { @ Query ( ) async notifications ( ) { return getMongoRepository ( Notification ) . find ( { cache : true } ) ; } @ Mutation ( ) async pushNotification ( @ Args ( 'userIds' ) userIds , @ Args ( 'label' ) label , @ Context ( 'pubsub' ) pubsub ) { const newNotification = await getMongoRepository ( Notification ) . save ( new Notification ( { label } ) ) ; pubsub . publish ( NOTIFICATION_SUBSCRIPTION , { userIds , newNotification } ) ; return newNotification ; } @ Subscription ( ( ) => Object , { filter : ( payload , variables , context ) => { const { userIds } = payload ; const { _id } = context . currentUser ; return userIds . indexOf ( _id ) > - 1 ; } } ) async newNotification ( @ Context ( 'pubsub' ) pubsub ) { return pubsub . asyncIterator ( NOTIFICATION_SUBSCRIPTION ) ; } }