Skip to content

Commit 99a8fd7

Browse files
committed
refactor(forAwaitEach): in favor of native for await
1 parent e2c7ba5 commit 99a8fd7

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,
@@ -306,7 +304,7 @@ describe('merge schemas through transforms', () => {
306304
});
307305
});
308306

309-
test('local subscriptions should work even if root fields are renamed', (done) => {
307+
test('local subscriptions should work even if root fields are renamed', async () => {
310308
const originalNotification = {
311309
notifications: {
312310
text: 'Hello world',
@@ -319,34 +317,23 @@ describe('merge schemas through transforms', () => {
319317
};
320318

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

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,
@@ -838,7 +837,7 @@ bookingById(id: "b1") {
838837
expect(stitchedResult).toEqual(bookingResult);
839838
});
840839

841-
test('local subscriptions working in merged schema', (done) => {
840+
test('local subscriptions working in merged schema', async () => {
842841
const mockNotification = {
843842
notifications: {
844843
text: 'Hello world',
@@ -853,30 +852,19 @@ bookingById(id: "b1") {
853852
}
854853
`);
855854

856-
let notificationCnt = 0;
857-
subscribe(stitchedSchema, subscription)
858-
.then((results) => {
859-
forAwaitEach(
860-
results as AsyncIterable<ExecutionResult>,
861-
(result: ExecutionResult) => {
862-
expect(result).toHaveProperty('data');
863-
expect(result.data).toEqual(mockNotification);
864-
if (!notificationCnt++) {
865-
done();
866-
}
867-
},
868-
).catch(done);
869-
})
870-
.then(() =>
871-
subscriptionPubSub.publish(
872-
subscriptionPubSubTrigger,
873-
mockNotification,
874-
),
875-
)
876-
.catch(done);
855+
const sub = await subscribe(stitchedSchema, subscription) as AsyncIterableIterator<ExecutionResult>;
856+
857+
const payload = sub.next();
858+
859+
await subscriptionPubSub.publish(
860+
subscriptionPubSubTrigger,
861+
mockNotification,
862+
);
863+
864+
expect(await payload).toEqual({ done: false, value: { data: mockNotification } });
877865
});
878866

879-
test('subscription errors are working correctly in merged schema', (done) => {
867+
test('subscription errors are working correctly in merged schema', async () => {
880868
const mockNotification = {
881869
notifications: {
882870
text: 'Hello world',
@@ -913,30 +901,16 @@ bookingById(id: "b1") {
913901
}
914902
`);
915903

916-
let notificationCnt = 0;
917-
subscribe(stitchedSchema, subscription)
918-
.then((results) => {
919-
forAwaitEach(
920-
results as AsyncIterable<ExecutionResult>,
921-
(result: ExecutionResult) => {
922-
expect(result).toHaveProperty('data');
923-
expect(result).toHaveProperty('errors');
924-
expect(result.errors).toHaveLength(1);
925-
expect(result.errors).toEqual(expectedResult.errors);
926-
expect(result.data).toEqual(expectedResult.data);
927-
if (!notificationCnt++) {
928-
done();
929-
}
930-
},
931-
).catch(done);
932-
})
933-
.then(() =>
934-
subscriptionPubSub.publish(
935-
subscriptionPubSubTrigger,
936-
mockNotification,
937-
),
938-
)
939-
.catch(done);
904+
const sub = await subscribe(stitchedSchema, subscription) as AsyncIterableIterator<ExecutionResult>;
905+
906+
const payload = sub.next();
907+
908+
await subscriptionPubSub.publish(
909+
subscriptionPubSubTrigger,
910+
mockNotification,
911+
);
912+
913+
expect(await payload).toEqual({ done: false, value: expectedResult });
940914
});
941915

942916
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)