Skip to content

Commit 4148c1c

Browse files
committed
2 way of data loading in subscriptions
1 parent bfc38ce commit 4148c1c

File tree

2 files changed

+108
-8
lines changed

2 files changed

+108
-8
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/* eslint-disable prefer-destructuring */
2+
/* eslint-disable no-prototype-builtins */
3+
/* eslint-disable no-await-in-loop */
4+
/* eslint-disable no-lonely-if */
5+
6+
// https://gist.github.com/OliverJAsh/2c327ae63941a237594eed34fe60a47b
7+
export class FunctifiedAsync {
8+
constructor(iterable) {
9+
this.iterable = iterable;
10+
}
11+
12+
async *[Symbol.asyncIterator]() {
13+
for await (const value of this.iterable) {
14+
yield value;
15+
}
16+
}
17+
18+
map(callback) {
19+
const iterable = this.iterable;
20+
return FunctifiedAsync.fromGenerator(async function*() {
21+
for await (const value of iterable) {
22+
yield callback(value);
23+
}
24+
});
25+
}
26+
27+
skipWhile(predicate) {
28+
const iterable = this.iterable;
29+
return FunctifiedAsync.fromGenerator(async function*() {
30+
let skip = true;
31+
for await (const value of iterable) {
32+
if (!predicate(value)) {
33+
skip = false;
34+
}
35+
if (!skip) {
36+
yield value;
37+
}
38+
}
39+
});
40+
}
41+
42+
flatten() {
43+
const iterable = this.iterable;
44+
return FunctifiedAsync.fromGenerator(async function*() {
45+
for await (const value of iterable) {
46+
if (value[Symbol.iterator] || value[Symbol.asyncIterator]) {
47+
yield* new FunctifiedAsync(value);
48+
} else {
49+
yield value;
50+
}
51+
}
52+
});
53+
}
54+
55+
takeUntil(predicate) {
56+
const iterator = this.iterable[Symbol.asyncIterator]();
57+
const self = this;
58+
return FunctifiedAsync.fromGenerator(async function*() {
59+
if (self.hasOwnProperty('startValue')) {
60+
yield self.startValue;
61+
}
62+
while (true) {
63+
const result = await iterator.next();
64+
if (result.done) {
65+
break;
66+
} else {
67+
if (predicate(result.value)) {
68+
// save the value so we can yield if takeUntil is called again
69+
self.startValue = result.value;
70+
break;
71+
} else {
72+
yield result.value;
73+
}
74+
}
75+
}
76+
});
77+
}
78+
79+
static load(iterable) {
80+
return new FunctifiedAsync(iterable);
81+
}
82+
83+
static map(iterable, cb) {
84+
return this.load(iterable).map(cb);
85+
}
86+
87+
static fromGenerator(generator) {
88+
return new FunctifiedAsync({
89+
[Symbol.asyncIterator]: generator,
90+
});
91+
}
92+
}

examples/northwind/schema.js

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { schemaComposer } from './schemaComposer';
1212
import { CategoryTC } from './models/category';
1313
import { CustomerTC } from './models/customer';
1414
import { EmployeeTC } from './models/employee';
15-
import { OrderTC } from './models/order';
15+
import { OrderTC, Order } from './models/order';
1616
import { ProductTC } from './models/product';
1717
import { RegionTC } from './models/region';
1818
import { ShipperTC } from './models/shipper';
@@ -21,6 +21,7 @@ import { SupplierTC } from './models/supplier';
2121
import { addQueryToPayload } from './wrappers/addQueryToPayload';
2222
import { autoResetDataIn30min } from './wrappers/autoResetDataIn30min';
2323
import { seedByName } from '../../scripts/seedHelpers';
24+
import { FunctifiedAsync } from './FunctifiedAsync';
2425

2526
const 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({
118121
schemaComposer.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

Comments
 (0)