Skip to content

Commit 1df92df

Browse files
committed
Fix validators to be aware of type-condition-less inline frags.
1 parent 6eae1f5 commit 1df92df

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

graphql/core/validation/rules.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ class FragmentsOnCompositeTypes(ValidationRule):
8383
def enter_InlineFragment(self, node, *args):
8484
type = self.context.get_type()
8585

86-
if type and not is_composite_type(type):
86+
if node.type_condition and type and not is_composite_type(type):
8787
return GraphQLError(
8888
self.inline_fragment_on_non_composite_error_message(print_ast(node.type_condition)),
8989
[node.type_condition]
@@ -910,8 +910,13 @@ def collect_field_asts_and_defs(self, parent_type, selection_set, visited_fragme
910910
ast_and_defs[response_name].append((selection, field_def))
911911

912912
elif isinstance(selection, ast.InlineFragment):
913+
type_condition = selection.type_condition
914+
inline_fragment_type = \
915+
type_from_ast(self.context.get_schema(), type_condition) \
916+
if type_condition else parent_type
917+
913918
self.collect_field_asts_and_defs(
914-
type_from_ast(self.context.get_schema(), selection.type_condition),
919+
inline_fragment_type,
915920
selection.selection_set,
916921
visited_fragment_names,
917922
ast_and_defs

tests/core_validation/test_fragments_on_composite_types.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,16 @@ def test_object_is_valid_inline_fragment_type():
4343
''')
4444

4545

46+
def test_inline_fragment_without_type_is_valid():
47+
expect_passes_rule(FragmentsOnCompositeTypes, '''
48+
fragment validFragment on Pet {
49+
... {
50+
name
51+
}
52+
}
53+
''')
54+
55+
4656
def test_union_is_valid_fragment_type():
4757
expect_passes_rule(FragmentsOnCompositeTypes, '''
4858
fragment validFragment on CatOrDog {

tests/core_validation/test_overlapping_fields_can_be_merged.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,17 @@ def test_same_wrapped_scalar_return_types():
344344
''')
345345

346346

347+
def test_allows_inline_typeless_fragments():
348+
expect_passes_rule_with_schema(schema, OverlappingFieldsCanBeMerged, '''
349+
{
350+
a
351+
... {
352+
a
353+
}
354+
}
355+
''')
356+
357+
347358
def test_compares_deep_types_including_list():
348359
expect_fails_rule_with_schema(schema, OverlappingFieldsCanBeMerged, '''
349360
{

0 commit comments

Comments
 (0)