@@ -12,7 +12,7 @@ import { schemaComposer } from './schemaComposer';
1212import { CategoryTC } from './models/category' ;
1313import { CustomerTC } from './models/customer' ;
1414import { EmployeeTC } from './models/employee' ;
15- import { OrderTC } from './models/order' ;
15+ import { OrderTC , Order } from './models/order' ;
1616import { ProductTC } from './models/product' ;
1717import { RegionTC } from './models/region' ;
1818import { ShipperTC } from './models/shipper' ;
@@ -21,6 +21,7 @@ import { SupplierTC } from './models/supplier';
2121import { addQueryToPayload } from './wrappers/addQueryToPayload' ;
2222import { autoResetDataIn30min } from './wrappers/autoResetDataIn30min' ;
2323import { seedByName } from '../../scripts/seedHelpers' ;
24+ import { FunctifiedAsync } from './FunctifiedAsync' ;
2425
2526const pubsub = new PubSub ( ) ;
2627
@@ -77,21 +78,23 @@ schemaComposer.Mutation.addFields({
7778 createOrder : OrderTC . getResolver ( 'createOne' , [
7879 async ( next , s , a , c , i ) => {
7980 const res = await next ( s , a , c , i ) ;
80- pubsub . publish ( 'ORDER_CREATED' , res . record ) ;
81+ const _id = res ?. record ?. _id ;
82+ if ( _id ) pubsub . publish ( 'ORDER_CREATED' , _id ) ;
8183 return res ;
8284 } ,
8385 ] ) ,
8486 updateOrder : OrderTC . getResolver ( 'updateById' , [
8587 async ( next , s , a , c , i ) => {
8688 const res = await next ( s , a , c , i ) ;
87- pubsub . publish ( 'ORDER_UPDATED' , res . record ) ;
89+ const _id = res ?. record ?. _id ;
90+ if ( _id ) pubsub . publish ( 'ORDER_UPDATED' , _id ) ;
8891 return res ;
8992 } ,
9093 ] ) ,
9194 removeOrder : OrderTC . getResolver ( 'removeOne' , [
9295 async ( next , s , a , c , i ) => {
9396 const res = await next ( s , a , c , i ) ;
94- pubsub . publish ( 'ORDER_REMOVED' , res . record ) ;
97+ if ( res ?. _id ) pubsub . publish ( 'ORDER_REMOVED' , res ?. _id ) ;
9598 return res ;
9699 } ,
97100 ] ) ,
@@ -118,17 +121,22 @@ schemaComposer.Mutation.addFields({
118121schemaComposer . Subscription . addFields ( {
119122 orderCreated : {
120123 type : OrderTC ,
121- resolve : ( record ) => record ,
124+ // way 1: load Order in resolver
125+ resolve : ( _id ) => Order . findById ( _id ) ,
122126 subscribe : ( ) => pubsub . asyncIterator ( [ 'ORDER_CREATED' ] ) ,
123127 } ,
124128 orderUpdated : {
125129 type : OrderTC ,
126- resolve : ( record ) => record ,
127- subscribe : ( ) => pubsub . asyncIterator ( [ 'ORDER_UPDATED' ] ) ,
130+ // way 2: load Order in AsyncIterator
131+ resolve : ( order ) => order ,
132+ subscribe : ( ) =>
133+ FunctifiedAsync . map ( pubsub . asyncIterator ( [ 'ORDER_UPDATED' ] ) , ( _id ) => {
134+ return Order . findById ( _id ) ;
135+ } ) ,
128136 } ,
129137 orderRemoved : {
130138 type : 'MongoID' ,
131- resolve : ( record ) => record ,
139+ resolve : ( _id ) => _id ,
132140 subscribe : ( ) => pubsub . asyncIterator ( [ 'ORDER_REMOVED' ] ) ,
133141 } ,
134142} ) ;
0 commit comments