Skip to content

Commit 7fd1ddb

Browse files
authored
polish: do not repeat isPromise check (#3793)
1 parent e9c221c commit 7fd1ddb

File tree

2 files changed

+250
-80
lines changed

2 files changed

+250
-80
lines changed

src/execution/__tests__/stream-test.ts

+137
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,54 @@ describe('Execute: stream directive', () => {
402402
},
403403
]);
404404
});
405+
it('Can stream a field that returns a list with nested promises', async () => {
406+
const document = parse(`
407+
query {
408+
friendList @stream(initialCount: 2) {
409+
name
410+
id
411+
}
412+
}
413+
`);
414+
const result = await complete(document, {
415+
friendList: () =>
416+
friends.map((f) => ({
417+
name: Promise.resolve(f.name),
418+
id: Promise.resolve(f.id),
419+
})),
420+
});
421+
expectJSON(result).toDeepEqual([
422+
{
423+
data: {
424+
friendList: [
425+
{
426+
name: 'Luke',
427+
id: '1',
428+
},
429+
{
430+
name: 'Han',
431+
id: '2',
432+
},
433+
],
434+
},
435+
hasNext: true,
436+
},
437+
{
438+
incremental: [
439+
{
440+
items: [
441+
{
442+
name: 'Leia',
443+
id: '3',
444+
},
445+
],
446+
path: ['friendList', 2],
447+
},
448+
],
449+
hasNext: false,
450+
},
451+
]);
452+
});
405453
it('Handles rejections in a field that returns a list of promises before initialCount is reached', async () => {
406454
const document = parse(`
407455
query {
@@ -901,6 +949,55 @@ describe('Execute: stream directive', () => {
901949
},
902950
]);
903951
});
952+
it('Handles nested async errors thrown by completeValue after initialCount is reached', async () => {
953+
const document = parse(`
954+
query {
955+
friendList @stream(initialCount: 1) {
956+
nonNullName
957+
}
958+
}
959+
`);
960+
const result = await complete(document, {
961+
friendList: () => [
962+
{ nonNullName: Promise.resolve(friends[0].name) },
963+
{ nonNullName: Promise.reject(new Error('Oops')) },
964+
{ nonNullName: Promise.resolve(friends[1].name) },
965+
],
966+
});
967+
expectJSON(result).toDeepEqual([
968+
{
969+
data: {
970+
friendList: [{ nonNullName: 'Luke' }],
971+
},
972+
hasNext: true,
973+
},
974+
{
975+
incremental: [
976+
{
977+
items: [null],
978+
path: ['friendList', 1],
979+
errors: [
980+
{
981+
message: 'Oops',
982+
locations: [{ line: 4, column: 11 }],
983+
path: ['friendList', 1, 'nonNullName'],
984+
},
985+
],
986+
},
987+
],
988+
hasNext: true,
989+
},
990+
{
991+
incremental: [
992+
{
993+
items: [{ nonNullName: 'Han' }],
994+
path: ['friendList', 2],
995+
},
996+
],
997+
hasNext: false,
998+
},
999+
]);
1000+
});
9041001
it('Handles async errors thrown by completeValue after initialCount is reached for a non-nullable list', async () => {
9051002
const document = parse(`
9061003
query {
@@ -943,6 +1040,46 @@ describe('Execute: stream directive', () => {
9431040
},
9441041
]);
9451042
});
1043+
it('Handles nested async errors thrown by completeValue after initialCount is reached for a non-nullable list', async () => {
1044+
const document = parse(`
1045+
query {
1046+
nonNullFriendList @stream(initialCount: 1) {
1047+
nonNullName
1048+
}
1049+
}
1050+
`);
1051+
const result = await complete(document, {
1052+
nonNullFriendList: () => [
1053+
{ nonNullName: Promise.resolve(friends[0].name) },
1054+
{ nonNullName: Promise.reject(new Error('Oops')) },
1055+
{ nonNullName: Promise.resolve(friends[1].name) },
1056+
],
1057+
});
1058+
expectJSON(result).toDeepEqual([
1059+
{
1060+
data: {
1061+
nonNullFriendList: [{ nonNullName: 'Luke' }],
1062+
},
1063+
hasNext: true,
1064+
},
1065+
{
1066+
incremental: [
1067+
{
1068+
items: null,
1069+
path: ['nonNullFriendList', 1],
1070+
errors: [
1071+
{
1072+
message: 'Oops',
1073+
locations: [{ line: 4, column: 11 }],
1074+
path: ['nonNullFriendList', 1, 'nonNullName'],
1075+
},
1076+
],
1077+
},
1078+
],
1079+
hasNext: false,
1080+
},
1081+
]);
1082+
});
9461083
it('Handles async errors thrown by completeValue after initialCount is reached from async iterable', async () => {
9471084
const document = parse(`
9481085
query {

0 commit comments

Comments
 (0)