1
1
import { expect } from 'chai' ;
2
2
import { describe , it } from 'mocha' ;
3
3
4
+ import { expectJSON } from '../../__testUtils__/expectJSON.js' ;
5
+
4
6
import { parse } from '../../language/parser.js' ;
5
7
6
8
import {
@@ -12,7 +14,7 @@ import {
12
14
import { GraphQLBoolean , GraphQLString } from '../../type/scalars.js' ;
13
15
import { GraphQLSchema } from '../../type/schema.js' ;
14
16
15
- import { executeSync } from '../execute.js' ;
17
+ import { execute , executeSync } from '../execute.js' ;
16
18
17
19
class Dog {
18
20
name : string ;
@@ -42,19 +44,30 @@ class Cat {
42
44
}
43
45
}
44
46
47
+ class Plant {
48
+ name : string ;
49
+
50
+ constructor ( name : string ) {
51
+ this . name = name ;
52
+ }
53
+ }
54
+
45
55
class Person {
46
56
name : string ;
47
57
pets : ReadonlyArray < Dog | Cat > | undefined ;
48
58
friends : ReadonlyArray < Dog | Cat | Person > | undefined ;
59
+ responsibilities : ReadonlyArray < Dog | Cat | Plant > | undefined ;
49
60
50
61
constructor (
51
62
name : string ,
52
63
pets ?: ReadonlyArray < Dog | Cat > ,
53
64
friends ?: ReadonlyArray < Dog | Cat | Person > ,
65
+ responsibilities ?: ReadonlyArray < Dog | Cat | Plant > ,
54
66
) {
55
67
this . name = name ;
56
68
this . pets = pets ;
57
69
this . friends = friends ;
70
+ this . responsibilities = responsibilities ;
58
71
}
59
72
}
60
73
@@ -108,6 +121,18 @@ const CatType: GraphQLObjectType = new GraphQLObjectType({
108
121
isTypeOf : ( value ) => value instanceof Cat ,
109
122
} ) ;
110
123
124
+ const PlantType : GraphQLObjectType = new GraphQLObjectType ( {
125
+ name : 'Plant' ,
126
+ interfaces : [ NamedType ] ,
127
+ fields : ( ) => ( {
128
+ name : { type : GraphQLString } ,
129
+ } ) ,
130
+ // eslint-disable-next-line @typescript-eslint/require-await
131
+ isTypeOf : async ( ) => {
132
+ throw new Error ( 'Not sure if this is a plant' ) ;
133
+ } ,
134
+ } ) ;
135
+
111
136
const PetType = new GraphQLUnionType ( {
112
137
name : 'Pet' ,
113
138
types : [ DogType , CatType ] ,
@@ -124,13 +149,19 @@ const PetType = new GraphQLUnionType({
124
149
} ,
125
150
} ) ;
126
151
152
+ const PetOrPlantType = new GraphQLUnionType ( {
153
+ name : 'PetOrPlantType' ,
154
+ types : [ PlantType , DogType , CatType ] ,
155
+ } ) ;
156
+
127
157
const PersonType : GraphQLObjectType = new GraphQLObjectType ( {
128
158
name : 'Person' ,
129
159
interfaces : [ NamedType , MammalType , LifeType ] ,
130
160
fields : ( ) => ( {
131
161
name : { type : GraphQLString } ,
132
162
pets : { type : new GraphQLList ( PetType ) } ,
133
163
friends : { type : new GraphQLList ( NamedType ) } ,
164
+ responsibilities : { type : new GraphQLList ( PetOrPlantType ) } ,
134
165
progeny : { type : new GraphQLList ( PersonType ) } ,
135
166
mother : { type : PersonType } ,
136
167
father : { type : PersonType } ,
@@ -151,8 +182,14 @@ const odie = new Dog('Odie', true);
151
182
odie . mother = new Dog ( "Odie's Mom" , true ) ;
152
183
odie . mother . progeny = [ odie ] ;
153
184
185
+ const fern = new Plant ( 'Fern' ) ;
154
186
const liz = new Person ( 'Liz' ) ;
155
- const john = new Person ( 'John' , [ garfield , odie ] , [ liz , odie ] ) ;
187
+ const john = new Person (
188
+ 'John' ,
189
+ [ garfield , odie ] ,
190
+ [ liz , odie ] ,
191
+ [ garfield , fern ] ,
192
+ ) ;
156
193
157
194
describe ( 'Execute: Union and intersection types' , ( ) => {
158
195
it ( 'can introspect on union and intersection types' , ( ) => {
@@ -195,7 +232,12 @@ describe('Execute: Union and intersection types', () => {
195
232
name : 'Named' ,
196
233
fields : [ { name : 'name' } ] ,
197
234
interfaces : [ ] ,
198
- possibleTypes : [ { name : 'Dog' } , { name : 'Cat' } , { name : 'Person' } ] ,
235
+ possibleTypes : [
236
+ { name : 'Dog' } ,
237
+ { name : 'Cat' } ,
238
+ { name : 'Person' } ,
239
+ { name : 'Plant' } ,
240
+ ] ,
199
241
enumValues : null ,
200
242
inputFields : null ,
201
243
} ,
@@ -545,4 +587,56 @@ describe('Execute: Union and intersection types', () => {
545
587
expect ( encounteredRootValue ) . to . equal ( rootValue ) ;
546
588
expect ( encounteredContext ) . to . equal ( contextValue ) ;
547
589
} ) ;
590
+
591
+ it ( 'it rejects if isTypeOf check rejects in defaultResolveType' , async ( ) => {
592
+ const document = parse ( `
593
+ {
594
+ responsibilities {
595
+ __typename
596
+ ... on Dog {
597
+ name
598
+ barks
599
+ }
600
+ ... on Cat {
601
+ name
602
+ meows
603
+ }
604
+ }
605
+ }
606
+ ` ) ;
607
+
608
+ const rootValue = new Person ( 'John' , [ ] , [ liz ] , [ garfield ] ) ;
609
+ const contextValue = { authToken : '123abc' } ;
610
+
611
+ /* c8 ignore next 4 */
612
+ // eslint-disable-next-line no-undef
613
+ process . on ( 'unhandledRejection' , ( ) => {
614
+ expect . fail ( 'Unhandled rejection' ) ;
615
+ } ) ;
616
+
617
+ const result = await execute ( {
618
+ schema,
619
+ document,
620
+ rootValue,
621
+ contextValue,
622
+ } ) ;
623
+
624
+ expectJSON ( result ) . toDeepEqual ( {
625
+ data : {
626
+ responsibilities : [ null ] ,
627
+ } ,
628
+ errors : [
629
+ {
630
+ message : 'Not sure if this is a plant' ,
631
+ locations : [
632
+ {
633
+ column : 9 ,
634
+ line : 3 ,
635
+ } ,
636
+ ] ,
637
+ path : [ 'responsibilities' , 0 ] ,
638
+ } ,
639
+ ] ,
640
+ } ) ;
641
+ } ) ;
548
642
} ) ;
0 commit comments