@@ -37,8 +37,22 @@ function executeQuery(
37
37
return execute ( { schema, document : parse ( query ) , rootValue, variableValues } ) ;
38
38
}
39
39
40
- describe ( 'Execute: Handles Oneof Input Objects and Oneof Objects' , ( ) => {
41
- describe ( 'Oneof Input Objects' , ( ) => {
40
+ async function executeQueryAsync (
41
+ query : string ,
42
+ rootValue : unknown ,
43
+ variableValues ?: { [ variable : string ] : unknown } ,
44
+ ) : Promise < ExecutionResult > {
45
+ const result = await execute ( {
46
+ schema,
47
+ document : parse ( query ) ,
48
+ rootValue,
49
+ variableValues,
50
+ } ) ;
51
+ return result ;
52
+ }
53
+
54
+ describe ( 'Execute: Handles OneOf Input Objects and OneOf Objects' , ( ) => {
55
+ describe ( 'OneOf Input Objects' , ( ) => {
42
56
const rootValue = {
43
57
test ( { input } : { input : { a ?: string ; b ?: number } } ) {
44
58
return input ;
@@ -137,4 +151,123 @@ describe('Execute: Handles Oneof Input Objects and Oneof Objects', () => {
137
151
} ) ;
138
152
} ) ;
139
153
} ) ;
154
+
155
+ describe ( 'OneOf Objects' , ( ) => {
156
+ const query = `
157
+ query ($input: TestInputObject! = {a: "abc"}) {
158
+ test(input: $input) {
159
+ a
160
+ b
161
+ }
162
+ }
163
+ ` ;
164
+
165
+ it ( 'works with a single, non-null value' , ( ) => {
166
+ const rootValue = {
167
+ test : {
168
+ a : null ,
169
+ b : 123 ,
170
+ } ,
171
+ } ;
172
+ const result = executeQuery ( query , rootValue ) ;
173
+
174
+ expectJSON ( result ) . toDeepEqual ( {
175
+ data : {
176
+ test : {
177
+ a : null ,
178
+ b : 123 ,
179
+ } ,
180
+ } ,
181
+ } ) ;
182
+ } ) ;
183
+
184
+ it ( 'works with a single, non-null, async value' , async ( ) => {
185
+ const rootValue = {
186
+ test ( ) {
187
+ return {
188
+ a : null ,
189
+ b : ( ) => new Promise ( ( resolve ) => resolve ( 123 ) ) ,
190
+ } ;
191
+ } ,
192
+ } ;
193
+ const result = await executeQueryAsync ( query , rootValue ) ;
194
+
195
+ expectJSON ( result ) . toDeepEqual ( {
196
+ data : {
197
+ test : {
198
+ a : null ,
199
+ b : 123 ,
200
+ } ,
201
+ } ,
202
+ } ) ;
203
+ } ) ;
204
+
205
+ it ( 'errors when there are no non-null values' , ( ) => {
206
+ const rootValue = {
207
+ test : {
208
+ a : null ,
209
+ b : null ,
210
+ } ,
211
+ } ;
212
+ const result = executeQuery ( query , rootValue ) ;
213
+
214
+ expectJSON ( result ) . toDeepEqual ( {
215
+ data : { test : null } ,
216
+ errors : [
217
+ {
218
+ locations : [ { column : 11 , line : 3 } ] ,
219
+ message :
220
+ 'OneOf Object "TestObject" must have exactly one non-null field but got 0.' ,
221
+ path : [ 'test' ] ,
222
+ } ,
223
+ ] ,
224
+ } ) ;
225
+ } ) ;
226
+
227
+ it ( 'errors when there are multiple non-null values' , ( ) => {
228
+ const rootValue = {
229
+ test : {
230
+ a : 'abc' ,
231
+ b : 456 ,
232
+ } ,
233
+ } ;
234
+ const result = executeQuery ( query , rootValue ) ;
235
+
236
+ expectJSON ( result ) . toDeepEqual ( {
237
+ data : { test : null } ,
238
+ errors : [
239
+ {
240
+ locations : [ { column : 11 , line : 3 } ] ,
241
+ message :
242
+ 'OneOf Object "TestObject" must have exactly one non-null field but got 2.' ,
243
+ path : [ 'test' ] ,
244
+ } ,
245
+ ] ,
246
+ } ) ;
247
+ } ) ;
248
+
249
+ it ( 'errors when there are multiple non-null, async values' , async ( ) => {
250
+ const rootValue = {
251
+ test ( ) {
252
+ return {
253
+ a : 'abc' ,
254
+ b : ( ) => new Promise ( ( resolve ) => resolve ( 123 ) ) ,
255
+ } ;
256
+ } ,
257
+ } ;
258
+ const result = await executeQueryAsync ( query , rootValue ) ;
259
+
260
+ expectJSON ( result ) . toDeepEqual ( {
261
+ data : { test : null } ,
262
+ errors : [
263
+ {
264
+ locations : [ { column : 11 , line : 3 } ] ,
265
+ message :
266
+ 'OneOf Object "TestObject" must have exactly one non-null field but got 2.' ,
267
+ path : [ 'test' ] ,
268
+ } ,
269
+ ] ,
270
+ } ) ;
271
+ } ) ;
272
+ } ) ;
140
273
} ) ;
0 commit comments