Skip to content

Commit 75114af

Browse files
authored
simplify schema in defer tests (#3881)
1 parent 418a0c8 commit 75114af

File tree

1 file changed

+92
-68
lines changed

1 file changed

+92
-68
lines changed

src/execution/__tests__/defer-test.ts

Lines changed: 92 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,7 @@ 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-
},
29+
nonNullName: { type: new GraphQLNonNull(GraphQLString) },
3330
},
3431
name: 'Friend',
3532
});
@@ -40,64 +37,36 @@ const friends = [
4037
{ name: 'C-3PO', id: 4 },
4138
];
4239

40+
const hero = { name: 'Luke', id: 1, friends };
41+
4342
const heroType = new GraphQLObjectType({
4443
fields: {
4544
id: { type: GraphQLID },
4645
name: { type: GraphQLString },
47-
slowField: {
48-
type: GraphQLString,
49-
resolve: async () => {
50-
await resolveOnNextTick();
51-
return 'slow';
52-
},
53-
},
54-
errorField: {
55-
type: GraphQLString,
56-
resolve: () => {
57-
throw new Error('bad');
58-
},
59-
},
60-
nonNullErrorField: {
61-
type: new GraphQLNonNull(GraphQLString),
62-
resolve: () => null,
63-
},
64-
promiseNonNullErrorField: {
65-
type: new GraphQLNonNull(GraphQLString),
66-
resolve: () => Promise.resolve(null),
67-
},
46+
nonNullName: { type: new GraphQLNonNull(GraphQLString) },
6847
friends: {
6948
type: new GraphQLList(friendType),
70-
resolve: () => friends,
71-
},
72-
asyncFriends: {
73-
type: new GraphQLList(friendType),
74-
async *resolve() {
75-
yield await Promise.resolve(friends[0]);
76-
},
7749
},
7850
},
7951
name: 'Hero',
8052
});
8153

82-
const hero = { name: 'Luke', id: 1 };
83-
8454
const query = new GraphQLObjectType({
8555
fields: {
8656
hero: {
8757
type: heroType,
88-
resolve: () => hero,
8958
},
9059
},
9160
name: 'Query',
9261
});
9362

9463
const schema = new GraphQLSchema({ query });
9564

96-
async function complete(document: DocumentNode) {
65+
async function complete(document: DocumentNode, rootValue: unknown = { hero }) {
9766
const result = await experimentalExecuteIncrementally({
9867
schema,
9968
document,
100-
rootValue: {},
69+
rootValue,
10170
});
10271

10372
if ('initialResult' in result) {
@@ -244,11 +213,18 @@ describe('Execute: defer directive', () => {
244213
}
245214
fragment QueryFragment on Query {
246215
hero {
247-
errorField
216+
name
248217
}
249218
}
250219
`);
251-
const result = await complete(document);
220+
const result = await complete(document, {
221+
hero: {
222+
...hero,
223+
name: () => {
224+
throw new Error('bad');
225+
},
226+
},
227+
});
252228

253229
expectJSON(result).toDeepEqual([
254230
{
@@ -260,14 +236,14 @@ describe('Execute: defer directive', () => {
260236
{
261237
data: {
262238
hero: {
263-
errorField: null,
239+
name: null,
264240
},
265241
},
266242
errors: [
267243
{
268244
message: 'bad',
269245
locations: [{ line: 7, column: 11 }],
270-
path: ['hero', 'errorField'],
246+
path: ['hero', 'name'],
271247
},
272248
],
273249
path: [],
@@ -440,10 +416,17 @@ describe('Execute: defer directive', () => {
440416
}
441417
}
442418
fragment NameFragment on Hero {
443-
errorField
419+
name
444420
}
445421
`);
446-
const result = await complete(document);
422+
const result = await complete(document, {
423+
hero: {
424+
...hero,
425+
name: () => {
426+
throw new Error('bad');
427+
},
428+
},
429+
});
447430
expectJSON(result).toDeepEqual([
448431
{
449432
data: { hero: { id: '1' } },
@@ -452,13 +435,13 @@ describe('Execute: defer directive', () => {
452435
{
453436
incremental: [
454437
{
455-
data: { errorField: null },
438+
data: { name: null },
456439
path: ['hero'],
457440
errors: [
458441
{
459442
message: 'bad',
460443
locations: [{ line: 9, column: 9 }],
461-
path: ['hero', 'errorField'],
444+
path: ['hero', 'name'],
462445
},
463446
],
464447
},
@@ -476,10 +459,15 @@ describe('Execute: defer directive', () => {
476459
}
477460
}
478461
fragment NameFragment on Hero {
479-
nonNullErrorField
462+
nonNullName
480463
}
481464
`);
482-
const result = await complete(document);
465+
const result = await complete(document, {
466+
hero: {
467+
...hero,
468+
nonNullName: () => null,
469+
},
470+
});
483471
expectJSON(result).toDeepEqual([
484472
{
485473
data: { hero: { id: '1' } },
@@ -493,9 +481,9 @@ describe('Execute: defer directive', () => {
493481
errors: [
494482
{
495483
message:
496-
'Cannot return null for non-nullable field Hero.nonNullErrorField.',
484+
'Cannot return null for non-nullable field Hero.nonNullName.',
497485
locations: [{ line: 9, column: 9 }],
498-
path: ['hero', 'nonNullErrorField'],
486+
path: ['hero', 'nonNullName'],
499487
},
500488
],
501489
},
@@ -508,27 +496,32 @@ describe('Execute: defer directive', () => {
508496
const document = parse(`
509497
query HeroNameQuery {
510498
hero {
511-
nonNullErrorField
499+
nonNullName
512500
...NameFragment @defer
513501
}
514502
}
515503
fragment NameFragment on Hero {
516504
id
517505
}
518506
`);
519-
const result = await complete(document);
507+
const result = await complete(document, {
508+
hero: {
509+
...hero,
510+
nonNullName: () => null,
511+
},
512+
});
520513
expectJSON(result).toDeepEqual({
521514
errors: [
522515
{
523516
message:
524-
'Cannot return null for non-nullable field Hero.nonNullErrorField.',
517+
'Cannot return null for non-nullable field Hero.nonNullName.',
525518
locations: [
526519
{
527520
line: 4,
528521
column: 11,
529522
},
530523
],
531-
path: ['hero', 'nonNullErrorField'],
524+
path: ['hero', 'nonNullName'],
532525
},
533526
],
534527
data: {
@@ -545,10 +538,15 @@ describe('Execute: defer directive', () => {
545538
}
546539
}
547540
fragment NameFragment on Hero {
548-
promiseNonNullErrorField
541+
nonNullName
549542
}
550543
`);
551-
const result = await complete(document);
544+
const result = await complete(document, {
545+
hero: {
546+
...hero,
547+
nonNullName: () => Promise.resolve(null),
548+
},
549+
});
552550
expectJSON(result).toDeepEqual([
553551
{
554552
data: { hero: { id: '1' } },
@@ -562,9 +560,9 @@ describe('Execute: defer directive', () => {
562560
errors: [
563561
{
564562
message:
565-
'Cannot return null for non-nullable field Hero.promiseNonNullErrorField.',
563+
'Cannot return null for non-nullable field Hero.nonNullName.',
566564
locations: [{ line: 9, column: 9 }],
567-
path: ['hero', 'promiseNonNullErrorField'],
565+
path: ['hero', 'nonNullName'],
568566
},
569567
],
570568
},
@@ -582,7 +580,7 @@ describe('Execute: defer directive', () => {
582580
}
583581
}
584582
fragment NameFragment on Hero {
585-
slowField
583+
name
586584
friends {
587585
...NestedFragment @defer
588586
}
@@ -591,7 +589,15 @@ describe('Execute: defer directive', () => {
591589
name
592590
}
593591
`);
594-
const result = await complete(document);
592+
const result = await complete(document, {
593+
hero: {
594+
...hero,
595+
name: async () => {
596+
await resolveOnNextTick();
597+
return 'slow';
598+
},
599+
},
600+
});
595601
expectJSON(result).toDeepEqual([
596602
{
597603
data: {
@@ -602,7 +608,7 @@ describe('Execute: defer directive', () => {
602608
{
603609
incremental: [
604610
{
605-
data: { slowField: 'slow', friends: [{}, {}, {}] },
611+
data: { name: 'slow', friends: [{}, {}, {}] },
606612
path: ['hero'],
607613
},
608614
],
@@ -671,8 +677,8 @@ describe('Execute: defer directive', () => {
671677
const document = parse(`
672678
query {
673679
hero {
674-
asyncFriends {
675-
promiseNonNullErrorField
680+
friends {
681+
nonNullName
676682
...NameFragment @defer
677683
}
678684
}
@@ -681,19 +687,29 @@ describe('Execute: defer directive', () => {
681687
name
682688
}
683689
`);
684-
const result = await complete(document);
690+
const result = await complete(document, {
691+
hero: {
692+
...hero,
693+
async *friends() {
694+
yield await Promise.resolve({
695+
...friends[0],
696+
nonNullName: () => Promise.resolve(null),
697+
});
698+
},
699+
},
700+
});
685701
expectJSON(result).toDeepEqual({
686702
data: {
687703
hero: {
688-
asyncFriends: [null],
704+
friends: [null],
689705
},
690706
},
691707
errors: [
692708
{
693709
message:
694-
'Cannot return null for non-nullable field Friend.promiseNonNullErrorField.',
710+
'Cannot return null for non-nullable field Friend.nonNullName.',
695711
locations: [{ line: 5, column: 11 }],
696-
path: ['hero', 'asyncFriends', 0, 'promiseNonNullErrorField'],
712+
path: ['hero', 'friends', 0, 'nonNullName'],
697713
},
698714
],
699715
});
@@ -719,15 +735,23 @@ describe('Execute: defer directive', () => {
719735
it('original execute function resolves to error if anything is deferred and something else is async', async () => {
720736
const doc = `
721737
query Deferred {
722-
hero { slowField }
738+
hero { name }
723739
... @defer { hero { id } }
724740
}
725741
`;
726742
await expectPromise(
727743
execute({
728744
schema,
729745
document: parse(doc),
730-
rootValue: {},
746+
rootValue: {
747+
hero: {
748+
...hero,
749+
name: async () => {
750+
await resolveOnNextTick();
751+
return 'slow';
752+
},
753+
},
754+
},
731755
}),
732756
).toRejectWith(
733757
'Executing this GraphQL operation would unexpectedly produce multiple payloads (due to @defer or @stream directive)',

0 commit comments

Comments
 (0)