Skip to content

Commit 6eae1f5

Browse files
committed
[RFC] Type condition optional on inline fragments
Implements graphql/graphql-spec#100
1 parent fd83a77 commit 6eae1f5

File tree

10 files changed

+328
-265
lines changed

10 files changed

+328
-265
lines changed

graphql/core/execution/base.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,11 +166,17 @@ def should_include_node(ctx, directives):
166166

167167

168168
def does_fragment_condition_match(ctx, fragment, type_):
169-
conditional_type = type_from_ast(ctx.schema, fragment.type_condition)
169+
type_condition_ast = fragment.type_condition
170+
if not type_condition_ast:
171+
return True
172+
173+
conditional_type = type_from_ast(ctx.schema, type_condition_ast)
170174
if conditional_type == type_:
171175
return True
176+
172177
if isinstance(conditional_type, (GraphQLInterfaceType, GraphQLUnionType)):
173178
return conditional_type.is_possible_type(type_)
179+
174180
return False
175181

176182

graphql/core/language/parser.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -315,17 +315,22 @@ def parse_fragment(parser):
315315
# Corresponds to both FragmentSpread and InlineFragment in the spec
316316
start = parser.token.start
317317
expect(parser, TokenKind.SPREAD)
318-
if parser.token.value == 'on':
319-
advance(parser)
320-
return ast.InlineFragment(
321-
type_condition=parse_named_type(parser),
318+
if peek(parser, TokenKind.NAME) and parser.token.value != 'on':
319+
return ast.FragmentSpread(
320+
name=parse_fragment_name(parser),
322321
directives=parse_directives(parser),
323-
selection_set=parse_selection_set(parser),
324322
loc=loc(parser, start)
325323
)
326-
return ast.FragmentSpread(
327-
name=parse_name(parser),
324+
325+
type_condition = None
326+
if parser.token.value == 'on':
327+
advance(parser)
328+
type_condition = parse_named_type(parser)
329+
330+
return ast.InlineFragment(
331+
type_condition=type_condition,
328332
directives=parse_directives(parser),
333+
selection_set=parse_selection_set(parser),
329334
loc=loc(parser, start)
330335
)
331336

graphql/core/language/printer.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,12 @@ def leave_FragmentSpread(self, node, *args):
5050
return '...' + node.name + wrap(' ', join(node.directives, ' '))
5151

5252
def leave_InlineFragment(self, node, *args):
53-
return ('... on ' + node.type_condition + ' ' +
54-
wrap('', join(node.directives, ' '), ' ') +
55-
node.selection_set)
53+
return join([
54+
'...',
55+
wrap('on ', node.type_condition),
56+
join(node.directives, ''),
57+
node.selection_set
58+
], ' ')
5659

5760
def leave_FragmentDefinition(self, node, *args):
5861
return ('fragment {} on {} '.format(node.name, node.type_condition) +

graphql/core/utils/type_info.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ def enter(self, node):
7373
type = schema.get_mutation_type()
7474
self._type_stack.append(type)
7575
elif isinstance(node, (ast.InlineFragment, ast.FragmentDefinition)):
76-
type = type_from_ast(schema, node.type_condition)
76+
type_condition_ast = node.type_condition
77+
type = type_from_ast(schema, type_condition_ast) if type_condition_ast else self.get_type()
7778
self._type_stack.append(type)
7879
elif isinstance(node, ast.VariableDefinition):
7980
self._input_type_stack.append(type_from_ast(schema, node.type))

tests/core_language/fixtures.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@
1818
}
1919
}
2020
}
21+
... @skip(unless: $foo) {
22+
id
23+
}
24+
... {
25+
id
26+
}
2127
}
2228
}
2329

tests/core_language/test_printer.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ def test_prints_kitchen_sink():
4040
}
4141
}
4242
}
43+
... @skip(unless: $foo) {
44+
id
45+
}
46+
... {
47+
id
48+
}
4349
}
4450
}
4551

0 commit comments

Comments
 (0)