Skip to content

Commit 0638a50

Browse files
committed
refactor(forAwaitEach): in favor of native for await
1 parent 811e43b commit 0638a50

File tree

7 files changed

+104
-253
lines changed

7 files changed

+104
-253
lines changed

packages/schema/tests/forAwaitEach.ts

Lines changed: 0 additions & 19 deletions
This file was deleted.

packages/schema/tests/resolution.test.ts

Lines changed: 45 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ import { makeExecutableSchema, addSchemaLevelResolver } from '@graphql-tools/sch
1111

1212
import { ExecutionResult } from '@graphql-tools/utils';
1313

14-
import { forAwaitEach } from './forAwaitEach';
15-
1614
describe('Resolve', () => {
1715
describe('addSchemaLevelResolver', () => {
1816
const pubsub = new PubSub();
@@ -79,97 +77,58 @@ describe('Resolve', () => {
7977
});
8078
});
8179

82-
test('should isolate roots from the different operation types', (done) => {
80+
test('should isolate roots from the different operation types', async () => {
8381
schemaLevelResolverCalls = 0;
8482
const queryRoot = 'queryRoot';
8583
const mutationRoot = 'mutationRoot';
8684
const subscriptionRoot = 'subscriptionRoot';
8785
const subscriptionRoot2 = 'subscriptionRoot2';
8886

89-
let subsCbkCalls = 0;
90-
const firstSubsTriggered = new Promise((resolveFirst) => {
91-
subscribe(
92-
schema,
93-
parse(`
94-
subscription TestSubscription {
95-
printRoot
96-
}
97-
`),
98-
)
99-
.then((results) => {
100-
forAwaitEach(
101-
results as AsyncIterable<ExecutionResult>,
102-
(result: ExecutionResult) => {
103-
if (result.errors != null) {
104-
return done(
105-
new Error(
106-
`Unexpected errors in GraphQL result: ${JSON.stringify(
107-
result.errors,
108-
)}`,
109-
),
110-
);
111-
}
112-
113-
const subsData = result.data;
114-
subsCbkCalls++;
115-
try {
116-
if (subsCbkCalls === 1) {
117-
expect(schemaLevelResolverCalls).toEqual(1);
118-
expect(subsData).toEqual({ printRoot: subscriptionRoot });
119-
return resolveFirst();
120-
} else if (subsCbkCalls === 2) {
121-
expect(schemaLevelResolverCalls).toEqual(4);
122-
expect(subsData).toEqual({
123-
printRoot: subscriptionRoot2,
124-
});
125-
return done();
126-
}
127-
} catch (e) {
128-
return done(e);
129-
}
130-
done(new Error('Too many subscription fired'));
131-
},
132-
).catch(done);
133-
})
134-
.then(() =>
135-
pubsub.publish('printRootChannel', { printRoot: subscriptionRoot }),
136-
)
137-
.catch(done);
138-
});
87+
const sub = await subscribe(
88+
schema,
89+
parse(`
90+
subscription TestSubscription {
91+
printRoot
92+
}
93+
`),
94+
) as AsyncIterableIterator<ExecutionResult>;
95+
96+
const payload1 = sub.next();
97+
await pubsub.publish('printRootChannel', { printRoot: subscriptionRoot });
98+
99+
expect(await payload1).toEqual({ done: false, value: { data: { printRoot: subscriptionRoot } } });
100+
expect(schemaLevelResolverCalls).toEqual(1);
101+
102+
const queryResult = await graphql(
103+
schema,
104+
`
105+
query TestQuery {
106+
printRoot
107+
}
108+
`,
109+
queryRoot,
110+
);
111+
112+
expect(queryResult).toEqual({ data: { printRoot: queryRoot } });
113+
expect(schemaLevelResolverCalls).toEqual(2);
114+
115+
const mutationResult = await graphql(
116+
schema,
117+
`
118+
mutation TestMutation {
119+
printRoot
120+
}
121+
`,
122+
mutationRoot,
123+
);
124+
125+
expect(mutationResult).toEqual({ data: { printRoot: mutationRoot } });
126+
expect(schemaLevelResolverCalls).toEqual(3);
127+
128+
await pubsub.publish('printRootChannel', { printRoot: subscriptionRoot2 });
139129

140-
firstSubsTriggered
141-
.then(() =>
142-
graphql(
143-
schema,
144-
`
145-
query TestQuery {
146-
printRoot
147-
}
148-
`,
149-
queryRoot,
150-
),
151-
)
152-
.then(({ data }) => {
153-
expect(schemaLevelResolverCalls).toEqual(2);
154-
expect(data).toEqual({ printRoot: queryRoot });
155-
return graphql(
156-
schema,
157-
`
158-
mutation TestMutation {
159-
printRoot
160-
}
161-
`,
162-
mutationRoot,
163-
);
164-
})
165-
.then(({ data: mutationData }) => {
166-
expect(schemaLevelResolverCalls).toEqual(3);
167-
expect(mutationData).toEqual({ printRoot: mutationRoot });
168-
return pubsub.publish('printRootChannel', {
169-
printRoot: subscriptionRoot2,
170-
});
171-
})
172-
.catch(done);
130+
expect(await sub.next()).toEqual({ done: false, value: { data: { printRoot: subscriptionRoot2 } } });
131+
expect(schemaLevelResolverCalls).toEqual(4);
173132
});
174133

175134
it('should not force an otherwise synchronous operation to be asynchronous', () => {

packages/stitch/tests/alternateStitchSchemas.test.ts

Lines changed: 17 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@ import { filterSchema, ExecutionResult } from '@graphql-tools/utils';
3737

3838
import { stitchSchemas } from '../src/stitchSchemas';
3939

40-
import { forAwaitEach } from './forAwaitEach';
41-
4240
import {
4341
bookingSchema,
4442
propertySchema,
@@ -309,7 +307,7 @@ describe('merge schemas through transforms', () => {
309307
});
310308
});
311309

312-
test('local subscriptions should work even if root fields are renamed', (done) => {
310+
test('local subscriptions should work even if root fields are renamed', async () => {
313311
const originalNotification = {
314312
notifications: {
315313
text: 'Hello world',
@@ -322,34 +320,23 @@ describe('merge schemas through transforms', () => {
322320
};
323321

324322
const subscription = parse(`
325-
subscription Subscription {
326-
Subscriptions_notifications {
327-
text
328-
}
323+
subscription Subscription {
324+
Subscriptions_notifications {
325+
text
329326
}
330-
`);
331-
332-
let notificationCnt = 0;
333-
subscribe(stitchedSchema, subscription)
334-
.then((results) => {
335-
forAwaitEach(
336-
results as AsyncIterable<ExecutionResult>,
337-
(result: ExecutionResult) => {
338-
expect(result).toHaveProperty('data');
339-
expect(result.data).toEqual(transformedNotification);
340-
if (!notificationCnt++) {
341-
return done();
342-
}
343-
},
344-
).catch(done);
345-
})
346-
.then(() =>
347-
subscriptionPubSub.publish(
348-
subscriptionPubSubTrigger,
349-
originalNotification,
350-
),
351-
)
352-
.catch(done);
327+
}
328+
`);
329+
330+
const sub = await subscribe(stitchedSchema, subscription) as AsyncIterableIterator<ExecutionResult>;
331+
332+
const payload = sub.next();
333+
334+
await subscriptionPubSub.publish(
335+
subscriptionPubSubTrigger,
336+
originalNotification,
337+
);
338+
339+
expect(await payload).toEqual({ done: false, value: { data: transformedNotification } } );
353340
});
354341
});
355342

packages/stitch/tests/forAwaitEach.ts

Lines changed: 0 additions & 19 deletions
This file was deleted.

packages/stitch/tests/stitchSchemas.test.ts

Lines changed: 23 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,8 @@ import {
2121
IResolvers,
2222
ExecutionResult,
2323
} from '@graphql-tools/utils';
24-
import { addMocksToSchema } from '@graphql-tools/mock';
2524

26-
import { forAwaitEach } from './forAwaitEach';
25+
import { addMocksToSchema } from '@graphql-tools/mock';
2726

2827
import {
2928
propertySchema as localPropertySchema,
@@ -846,7 +845,7 @@ bookingById(id: "b1") {
846845
expect(stitchedResult).toEqual(bookingResult);
847846
});
848847

849-
test('local subscriptions working in merged schema', (done) => {
848+
test('local subscriptions working in merged schema', async () => {
850849
const mockNotification = {
851850
notifications: {
852851
text: 'Hello world',
@@ -861,30 +860,19 @@ bookingById(id: "b1") {
861860
}
862861
`);
863862

864-
let notificationCnt = 0;
865-
subscribe(stitchedSchema, subscription)
866-
.then((results) => {
867-
forAwaitEach(
868-
results as AsyncIterable<ExecutionResult>,
869-
(result: ExecutionResult) => {
870-
expect(result).toHaveProperty('data');
871-
expect(result.data).toEqual(mockNotification);
872-
if (!notificationCnt++) {
873-
done();
874-
}
875-
},
876-
).catch(done);
877-
})
878-
.then(() =>
879-
subscriptionPubSub.publish(
880-
subscriptionPubSubTrigger,
881-
mockNotification,
882-
),
883-
)
884-
.catch(done);
863+
const sub = await subscribe(stitchedSchema, subscription) as AsyncIterableIterator<ExecutionResult>;
864+
865+
const payload = sub.next();
866+
867+
await subscriptionPubSub.publish(
868+
subscriptionPubSubTrigger,
869+
mockNotification,
870+
);
871+
872+
expect(await payload).toEqual({ done: false, value: { data: mockNotification } });
885873
});
886874

887-
test('subscription errors are working correctly in merged schema', (done) => {
875+
test('subscription errors are working correctly in merged schema', async () => {
888876
const mockNotification = {
889877
notifications: {
890878
text: 'Hello world',
@@ -921,30 +909,16 @@ bookingById(id: "b1") {
921909
}
922910
`);
923911

924-
let notificationCnt = 0;
925-
subscribe(stitchedSchema, subscription)
926-
.then((results) => {
927-
forAwaitEach(
928-
results as AsyncIterable<ExecutionResult>,
929-
(result: ExecutionResult) => {
930-
expect(result).toHaveProperty('data');
931-
expect(result).toHaveProperty('errors');
932-
expect(result.errors).toHaveLength(1);
933-
expect(result.errors).toEqual(expectedResult.errors);
934-
expect(result.data).toEqual(expectedResult.data);
935-
if (!notificationCnt++) {
936-
done();
937-
}
938-
},
939-
).catch(done);
940-
})
941-
.then(() =>
942-
subscriptionPubSub.publish(
943-
subscriptionPubSubTrigger,
944-
mockNotification,
945-
),
946-
)
947-
.catch(done);
912+
const sub = await subscribe(stitchedSchema, subscription) as AsyncIterableIterator<ExecutionResult>;
913+
914+
const payload = sub.next();
915+
916+
await subscriptionPubSub.publish(
917+
subscriptionPubSubTrigger,
918+
mockNotification,
919+
);
920+
921+
expect(await payload).toEqual({ done: false, value: expectedResult });
948922
});
949923

950924
test('links in queries', async () => {

packages/wrap/tests/forAwaitEach.ts

Lines changed: 0 additions & 19 deletions
This file was deleted.

0 commit comments

Comments
 (0)