1
1
import { InjectModel } from '@nestjs/mongoose'
2
2
import { Note } from '../schema/note'
3
3
import { Model } from 'mongoose'
4
- import { Args , Mutation , Query , Resolver } from '@nestjs/graphql'
4
+ import { Args , Mutation , Query , Resolver , Subscription } from '@nestjs/graphql'
5
5
import { NotePageableResponse , NoteView } from '../dto/note.view'
6
6
import { NoteService } from '../service/note.service'
7
7
import { NoteDto } from '../dto/note.dto'
@@ -12,6 +12,9 @@ import { noteAssembler } from '../dto/note-assembler'
12
12
import { NpRequestContext } from '../../shared/service/np-request-context.service'
13
13
import { NoteShareDTO } from '../dto/note-shared.dto'
14
14
import { handleNoteFindRequest , NoteFindRequest } from '../dto/note-find.request'
15
+ import { PubSub } from 'graphql-subscriptions'
16
+
17
+ const pubSub = new PubSub ( )
15
18
16
19
@UseGuards ( JwtGqlAuthenticationGuard )
17
20
@Resolver ( ( ) => NoteView )
@@ -47,8 +50,13 @@ export class NoteResolver {
47
50
48
51
@Mutation ( ( ) => NoteView , { name : 'noteCreate' } )
49
52
async create ( @Args ( 'input' ) dto : NoteDto ) : Promise < NoteView > {
50
- const result = await this . noteService . create ( dto )
51
- return noteAssembler ( result )
53
+ const newNote = await this . noteService . create ( dto )
54
+
55
+ const view = noteAssembler ( newNote )
56
+
57
+ await pubSub . publish ( 'noteCreated' , { noteCreated : view } )
58
+
59
+ return view
52
60
}
53
61
54
62
@Mutation ( ( ) => VoidResolver , {
@@ -81,3 +89,13 @@ export class NoteResolver {
81
89
return this . noteService . share ( id , dto )
82
90
}
83
91
}
92
+
93
+ @Resolver ( ( ) => NoteView )
94
+ export class NoteSubscriptionResolver {
95
+ @Subscription ( ( ) => NoteView , {
96
+ name : 'noteCreated'
97
+ } )
98
+ noteCreated ( ) {
99
+ return pubSub . asyncIterator ( 'noteCreated' )
100
+ }
101
+ }
0 commit comments