@@ -14,6 +14,8 @@ import {
14
14
customerPaginationResolver ,
15
15
} from './models/customer' ;
16
16
import {
17
+ EmployeeTC ,
18
+ Employee ,
17
19
employeeFindOneResolver ,
18
20
employeeFindManyResolver ,
19
21
employeePaginationResolver ,
@@ -30,6 +32,8 @@ import {
30
32
orderRemoveOneResolver ,
31
33
} from './models/order' ;
32
34
import {
35
+ ProductTC ,
36
+ Product ,
33
37
productConnectionResolver ,
34
38
productCreateOneResolver ,
35
39
productFindManyResolver ,
@@ -92,39 +96,28 @@ const fields = {
92
96
93
97
ViewerTC . addFields ( fields ) ;
94
98
99
+ function emitEventMW ( eventName : string ) {
100
+ return async ( next , s , a , c , i ) => {
101
+ const res = await next ( s , a , c , i ) ;
102
+ const _id = res ?. record ?. _id ;
103
+ if ( _id ) pubsub . publish ( eventName , _id ) ;
104
+ return res ;
105
+ } ;
106
+ }
107
+
95
108
schemaComposer . Mutation . addFields ( {
96
109
// ...allowOnlyForLocalhost({
97
110
...autoResetDataIn30min ( {
98
111
...addQueryToPayload ( {
99
- createProduct : productCreateOneResolver ,
100
- updateProduct : productUpdateByIdResolver ,
101
- removeProduct : productRemoveOneResolver ,
102
-
103
- createOrder : orderCreateOneResolver . withMiddlewares ( [
104
- async ( next , s , a , c , i ) => {
105
- const res = await next ( s , a , c , i ) ;
106
- const _id = res ?. record ?. _id ;
107
- if ( _id ) pubsub . publish ( 'ORDER_CREATED' , _id ) ;
108
- return res ;
109
- } ,
110
- ] ) ,
111
- updateOrder : orderUpdateByIdResolver . withMiddlewares ( [
112
- async ( next , s , a , c , i ) => {
113
- const res = await next ( s , a , c , i ) ;
114
- const _id = res ?. record ?. _id ;
115
- if ( _id ) pubsub . publish ( 'ORDER_UPDATED' , _id ) ;
116
- return res ;
117
- } ,
118
- ] ) ,
119
- removeOrder : orderRemoveOneResolver . withMiddlewares ( [
120
- async ( next , s , a , c , i ) => {
121
- const res = await next ( s , a , c , i ) ;
122
- if ( res ?. record ?. _id ) pubsub . publish ( 'ORDER_REMOVED' , res ?. record ?. _id ) ;
123
- return res ;
124
- } ,
125
- ] ) ,
126
-
127
- updateEmployee : employeeUpdateByIdResolver ,
112
+ createProduct : productCreateOneResolver . withMiddlewares ( [ emitEventMW ( 'PRODUCT_CREATED' ) ] ) ,
113
+ updateProduct : productUpdateByIdResolver . withMiddlewares ( [ emitEventMW ( 'PRODUCT_UPDATED' ) ] ) ,
114
+ removeProduct : productRemoveOneResolver . withMiddlewares ( [ emitEventMW ( 'PRODUCT_REMOVED' ) ] ) ,
115
+
116
+ createOrder : orderCreateOneResolver . withMiddlewares ( [ emitEventMW ( 'ORDER_CREATED' ) ] ) ,
117
+ updateOrder : orderUpdateByIdResolver . withMiddlewares ( [ emitEventMW ( 'ORDER_UPDATED' ) ] ) ,
118
+ removeOrder : orderRemoveOneResolver . withMiddlewares ( [ emitEventMW ( 'ORDER_REMOVED' ) ] ) ,
119
+
120
+ updateEmployee : employeeUpdateByIdResolver . withMiddlewares ( [ emitEventMW ( 'EMPLOYEE_UPDATED' ) ] ) ,
128
121
} ) ,
129
122
} ) ,
130
123
resetData : {
@@ -166,6 +159,35 @@ schemaComposer.Subscription.addFields({
166
159
resolve : ( _id ) => _id ,
167
160
subscribe : ( ) => pubsub . asyncIterator ( [ 'ORDER_REMOVED' ] ) ,
168
161
} ,
162
+ productCreated : {
163
+ type : ProductTC ,
164
+ resolve : ( product ) => product ,
165
+ subscribe : ( ) =>
166
+ FunctifiedAsync . map ( pubsub . asyncIterator ( [ 'PRODUCT_CREATED' ] ) , ( _id ) => {
167
+ return Product . findById ( _id ) ;
168
+ } ) ,
169
+ } ,
170
+ productUpdated : {
171
+ type : ProductTC ,
172
+ resolve : ( product ) => product ,
173
+ subscribe : ( ) =>
174
+ FunctifiedAsync . map ( pubsub . asyncIterator ( [ 'PRODUCT_UPDATED' ] ) , ( _id ) => {
175
+ return Product . findById ( _id ) ;
176
+ } ) ,
177
+ } ,
178
+ productRemoved : {
179
+ type : 'MongoID' ,
180
+ resolve : ( _id ) => _id ,
181
+ subscribe : ( ) => pubsub . asyncIterator ( [ 'PRODUCT_REMOVED' ] ) ,
182
+ } ,
183
+ employeeUpdated : {
184
+ type : EmployeeTC ,
185
+ resolve : ( employee ) => employee ,
186
+ subscribe : ( ) =>
187
+ FunctifiedAsync . map ( pubsub . asyncIterator ( [ 'EMPLOYEE_UPDATED' ] ) , ( _id ) => {
188
+ return Employee . findById ( _id ) ;
189
+ } ) ,
190
+ } ,
169
191
} ) ;
170
192
171
193
export default schemaComposer . buildSchema ( ) ;
0 commit comments