Skip to content

Commit 5ae2e06

Browse files
committed
add filtering for async iterator lists
1 parent 770acd6 commit 5ae2e06

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

src/execution/__tests__/defer-test.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ const friendType = new GraphQLObjectType({
2626
fields: {
2727
id: { type: GraphQLID },
2828
name: { type: GraphQLString },
29+
promiseNonNullErrorField: {
30+
type: new GraphQLNonNull(GraphQLString),
31+
resolve: () => Promise.resolve(null),
32+
},
2933
},
3034
name: 'Friend',
3135
});
@@ -65,6 +69,12 @@ const heroType = new GraphQLObjectType({
6569
type: new GraphQLList(friendType),
6670
resolve: () => friends,
6771
},
72+
asyncFriends: {
73+
type: new GraphQLList(friendType),
74+
async *resolve() {
75+
yield await Promise.resolve(friends[0]);
76+
},
77+
},
6878
},
6979
name: 'Hero',
7080
});
@@ -657,6 +667,38 @@ describe('Execute: defer directive', () => {
657667
]);
658668
});
659669

670+
it('Filters deferred payloads when a list item returned by an async iterable is nulled', async () => {
671+
const document = parse(`
672+
query {
673+
hero {
674+
asyncFriends {
675+
promiseNonNullErrorField
676+
...NameFragment @defer
677+
}
678+
}
679+
}
680+
fragment NameFragment on Friend {
681+
name
682+
}
683+
`);
684+
const result = await complete(document);
685+
expectJSON(result).toDeepEqual({
686+
data: {
687+
hero: {
688+
asyncFriends: [null],
689+
},
690+
},
691+
errors: [
692+
{
693+
message:
694+
'Cannot return null for non-nullable field Friend.promiseNonNullErrorField.',
695+
locations: [{ line: 5, column: 11 }],
696+
path: ['hero', 'asyncFriends', 0, 'promiseNonNullErrorField'],
697+
},
698+
],
699+
});
700+
});
701+
660702
it('original execute function throws error if anything is deferred and everything else is sync', () => {
661703
const doc = `
662704
query Deferred {

src/execution/execute.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1027,6 +1027,7 @@ async function completeAsyncIteratorValue(
10271027
pathToArray(fieldPath),
10281028
);
10291029
const handledError = handleFieldError(error, itemType, errors);
1030+
filterSubsequentPayloads(exeContext, fieldPath);
10301031
return handledError;
10311032
}),
10321033
);
@@ -1040,6 +1041,7 @@ async function completeAsyncIteratorValue(
10401041
fieldNodes,
10411042
pathToArray(fieldPath),
10421043
);
1044+
filterSubsequentPayloads(exeContext, fieldPath);
10431045
handleFieldError(error, itemType, errors);
10441046
}
10451047
} catch (rawError) {

0 commit comments

Comments
 (0)