1
1
from graphql .core .language .location import SourceLocation as L
2
- from graphql .core .type .definition import GraphQLObjectType , GraphQLArgument , GraphQLNonNull , GraphQLUnionType , \
2
+ from graphql .core .type .definition import GraphQLObjectType , GraphQLArgument , GraphQLNonNull , GraphQLInterfaceType , \
3
3
GraphQLList , GraphQLField
4
4
from graphql .core .type .scalars import GraphQLString , GraphQLInt , GraphQLID
5
5
from graphql .core .type .schema import GraphQLSchema
@@ -79,6 +79,19 @@ def test_same_aliases_with_different_field_targets():
79
79
], sort_list = False )
80
80
81
81
82
+ def test_same_aliases_allowed_on_nonoverlapping_fields ():
83
+ expect_passes_rule (OverlappingFieldsCanBeMerged , '''
84
+ fragment sameAliasesWithDifferentFieldTargets on Pet {
85
+ ... on Dog {
86
+ name
87
+ }
88
+ ... on Cat {
89
+ name: nickname
90
+ }
91
+ }
92
+ ''' )
93
+
94
+
82
95
def test_alias_masking_direct_field_access ():
83
96
expect_fails_rule (OverlappingFieldsCanBeMerged , '''
84
97
fragment aliasMaskingDirectFieldAccess on Dog {
@@ -90,6 +103,28 @@ def test_alias_masking_direct_field_access():
90
103
], sort_list = False )
91
104
92
105
106
+ def test_diferent_args_second_adds_an_argument ():
107
+ expect_fails_rule (OverlappingFieldsCanBeMerged , '''
108
+ fragment conflictingArgs on Dog {
109
+ doesKnowCommand
110
+ doesKnowCommand(dogCommand: HEEL)
111
+ }
112
+ ''' , [
113
+ fields_conflict ('doesKnowCommand' , 'they have differing arguments' , L (3 , 9 ), L (4 , 9 ))
114
+ ], sort_list = False )
115
+
116
+
117
+ def test_diferent_args_second_missing_an_argument ():
118
+ expect_fails_rule (OverlappingFieldsCanBeMerged , '''
119
+ fragment conflictingArgs on Dog {
120
+ doesKnowCommand(dogCommand: SIT)
121
+ doesKnowCommand
122
+ }
123
+ ''' , [
124
+ fields_conflict ('doesKnowCommand' , 'they have differing arguments' , L (3 , 9 ), L (4 , 9 ))
125
+ ], sort_list = False )
126
+
127
+
93
128
def test_conflicting_args ():
94
129
expect_fails_rule (OverlappingFieldsCanBeMerged , '''
95
130
fragment conflictingArgs on Dog {
@@ -101,6 +136,18 @@ def test_conflicting_args():
101
136
], sort_list = False )
102
137
103
138
139
+ def test_allows_different_args_where_no_conflict_is_possible ():
140
+ expect_passes_rule (OverlappingFieldsCanBeMerged , '''
141
+ fragment conflictingArgs on Pet {
142
+ ... on Dog {
143
+ name(surname: true)
144
+ }
145
+ ... on Cat {
146
+ name
147
+ }
148
+ }
149
+ ''' )
150
+
104
151
def test_conflicting_directives ():
105
152
expect_fails_rule (OverlappingFieldsCanBeMerged , '''
106
153
fragment conflictingDirectiveArgs on Dog {
@@ -276,25 +323,37 @@ def test_reports_deep_conflict_to_nearest_common_ancestor():
276
323
], sort_list = False )
277
324
278
325
326
+ SomeBox = GraphQLInterfaceType ('SomeBox' , {
327
+ 'unrelatedField' : GraphQLField (GraphQLString )
328
+ }, resolve_type = lambda * _ : StringBox )
329
+
279
330
StringBox = GraphQLObjectType ('StringBox' , {
280
- 'scalar' : GraphQLField (GraphQLString )
281
- })
331
+ 'scalar' : GraphQLField (GraphQLString ),
332
+ 'unrelatedField' : GraphQLField (GraphQLString )
333
+ }, interfaces = [SomeBox ])
282
334
283
335
IntBox = GraphQLObjectType ('IntBox' , {
284
- 'scalar' : GraphQLField (GraphQLInt )
285
- })
336
+ 'scalar' : GraphQLField (GraphQLInt ),
337
+ 'unrelatedField' : GraphQLField (GraphQLString )
338
+ }, interfaces = [SomeBox ])
286
339
287
- NonNullStringBox1 = GraphQLObjectType ('NonNullStringBox1' , {
340
+ NonNullStringBox1 = GraphQLInterfaceType ('NonNullStringBox1' , {
288
341
'scalar' : GraphQLField (GraphQLNonNull (GraphQLString ))
289
- })
342
+ }, resolve_type = lambda * _ : StringBox )
343
+
344
+ NonNullStringBox1Impl = GraphQLObjectType ('NonNullStringBox1Impl' , {
345
+ 'scalar' : GraphQLField (GraphQLNonNull (GraphQLString )),
346
+ 'unrelatedField' : GraphQLField (GraphQLString )
347
+ }, interfaces = [ SomeBox , NonNullStringBox1 ])
290
348
291
- NonNullStringBox2 = GraphQLObjectType ('NonNullStringBox2' , {
349
+ NonNullStringBox2 = GraphQLInterfaceType ('NonNullStringBox2' , {
292
350
'scalar' : GraphQLField (GraphQLNonNull (GraphQLString ))
293
- })
351
+ }, resolve_type = lambda * _ : StringBox )
294
352
295
- BoxUnion = GraphQLUnionType ('BoxUnion' , [
296
- StringBox , IntBox , NonNullStringBox1 , NonNullStringBox2
297
- ], resolve_type = lambda * _ : StringBox )
353
+ NonNullStringBox2Impl = GraphQLObjectType ('NonNullStringBox2Impl' , {
354
+ 'scalar' : GraphQLField (GraphQLNonNull (GraphQLString )),
355
+ 'unrelatedField' : GraphQLField (GraphQLString )
356
+ }, interfaces = [ SomeBox , NonNullStringBox2 ])
298
357
299
358
Connection = GraphQLObjectType ('Connection' , {
300
359
'edges' : GraphQLField (GraphQLList (GraphQLObjectType ('Edge' , {
@@ -306,33 +365,48 @@ def test_reports_deep_conflict_to_nearest_common_ancestor():
306
365
})
307
366
308
367
schema = GraphQLSchema (GraphQLObjectType ('QueryRoot' , {
309
- 'boxUnion ' : GraphQLField (BoxUnion ),
368
+ 'someBox ' : GraphQLField (SomeBox ),
310
369
'connection' : GraphQLField (Connection )
311
370
}))
312
371
313
372
314
- def test_conflicting_scalar_return_types ():
373
+ def test_conflicting_return_types_which_potentially_overlap ():
315
374
expect_fails_rule_with_schema (schema , OverlappingFieldsCanBeMerged , '''
316
375
{
317
- boxUnion {
376
+ someBox {
318
377
...on IntBox {
319
378
scalar
320
379
}
321
- ...on StringBox {
380
+ ...on NonNullStringBox1 {
322
381
scalar
323
382
}
324
383
}
325
384
}
326
385
327
386
''' , [
328
- fields_conflict ('scalar' , 'they return differing types Int and String' , L (5 , 17 ), L (8 , 17 ))
387
+ fields_conflict ('scalar' , 'they return differing types Int and String! ' , L (5 , 17 ), L (8 , 17 ))
329
388
], sort_list = False )
330
389
331
390
391
+ def test_allows_differing_return_types_which_cannot_overlap ():
392
+ expect_passes_rule_with_schema (schema , OverlappingFieldsCanBeMerged , '''
393
+ {
394
+ someBox {
395
+ ...on IntBox {
396
+ scalar
397
+ }
398
+ ...on StringBox {
399
+ scalar
400
+ }
401
+ }
402
+ }
403
+ ''' )
404
+
405
+
332
406
def test_same_wrapped_scalar_return_types ():
333
407
expect_passes_rule_with_schema (schema , OverlappingFieldsCanBeMerged , '''
334
408
{
335
- boxUnion {
409
+ someBox {
336
410
...on NonNullStringBox1 {
337
411
scalar
338
412
}
0 commit comments