Skip to content

Commit 3232706

Browse files
committed
Provides a correct OperationType without name in GraphQLPrinter
Related GraphQL-js commit: graphql/graphql-js@6741c31
1 parent 117fbb0 commit 3232706

File tree

2 files changed

+45
-5
lines changed

2 files changed

+45
-5
lines changed

graphql/core/language/printer.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ def leave_Document(self, node, *args):
2424
def leave_OperationDefinition(self, node, *args):
2525
name = node.name
2626
selection_set = node.selection_set
27-
if not name:
28-
return selection_set
29-
3027
op = node.operation
31-
defs = wrap('(', join(node.variable_definitions, ', '), ')')
28+
var_defs = wrap('(', join(node.variable_definitions, ', '), ')')
3229
directives = join(node.directives, ' ')
3330

34-
return join([op, join([name, defs]), directives, selection_set], ' ')
31+
if not name and not directives and not var_defs and op == 'query':
32+
return selection_set
33+
34+
return join([op, join([name, var_defs]), directives, selection_set], ' ')
3535

3636
def leave_VariableDefinition(self, node, *args):
3737
return node.variable + ': ' + node.type + wrap(' = ', node.default_value)

tests/core_language/test_printer.py

+40
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,46 @@ def test_produces_helpful_error_messages():
2828
assert 'Invalid AST Node' in str(excinfo.value)
2929

3030

31+
def test_correctly_prints_query_operation_without_name():
32+
query_ast_shorthanded = parse('query { id, name }')
33+
assert print_ast(query_ast_shorthanded) == '''{
34+
id
35+
name
36+
}
37+
'''
38+
39+
40+
def test_correctly_prints_mutation_operation_without_name():
41+
mutation_ast = parse('mutation { id, name }')
42+
assert print_ast(mutation_ast) == '''mutation {
43+
id
44+
name
45+
}
46+
'''
47+
48+
49+
def test_correctly_prints_query_with_artifacts():
50+
query_ast_shorthanded = parse(
51+
'query ($foo: TestType) @testDirective { id, name }'
52+
)
53+
assert print_ast(query_ast_shorthanded) == '''query ($foo: TestType) @testDirective {
54+
id
55+
name
56+
}
57+
'''
58+
59+
60+
def test_correctly_prints_mutation_with_artifacts():
61+
query_ast_shorthanded = parse(
62+
'mutation ($foo: TestType) @testDirective { id, name }'
63+
)
64+
assert print_ast(query_ast_shorthanded) == '''mutation ($foo: TestType) @testDirective {
65+
id
66+
name
67+
}
68+
'''
69+
70+
3171
def test_prints_kitchen_sink():
3272
ast = parse(KITCHEN_SINK)
3373
printed = print_ast(ast)

0 commit comments

Comments
 (0)