Skip to content

Commit 8a12c36

Browse files
committed
Schema Printer Tests
* Implement remaining part of printer so that it can print Schema types.
1 parent 310d0a9 commit 8a12c36

File tree

4 files changed

+109
-2
lines changed

4 files changed

+109
-2
lines changed

graphql/core/language/printer.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,11 @@ def leave_OperationDefinition(self, node, *args):
2323
selection_set = node.selection_set
2424
if not name:
2525
return selection_set
26+
2627
op = node.operation
2728
defs = wrap('(', join(node.variable_definitions, ', '), ')')
2829
directives = join(node.directives, ' ')
30+
2931
return join([op, join([name, defs]), directives, selection_set], ' ')
3032

3133
def leave_VariableDefinition(self, node, *args):
@@ -104,6 +106,42 @@ def leave_ListType(self, node, *args):
104106
def leave_NonNullType(self, node, *args):
105107
return node.type + '!'
106108

109+
# Type Definitions:
110+
111+
def leave_ObjectTypeDefinition(self, node, *args):
112+
return (
113+
'type ' + node.name + ' ' +
114+
wrap('implements ', join(node.interfaces, ', '), ' ') +
115+
block(node.fields)
116+
)
117+
118+
def leave_FieldDefinition(self, node, *args):
119+
return node.name + wrap('(', join(node.arguments, ', '), ')') + ': ' + node.type
120+
121+
def leave_InputValueDefinition(self, node, *args):
122+
return node.name + ': ' + node.type + wrap(' = ', node.default_value)
123+
124+
def leave_InterfaceTypeDefinition(self, node, *args):
125+
return 'interface ' + node.name + ' ' + block(node.fields)
126+
127+
def leave_UnionTypeDefinition(self, node, *args):
128+
return 'union ' + node.name + ' = ' + join(node.types, ' | ')
129+
130+
def leave_ScalarTypeDefinition(self, node, *args):
131+
return 'scalar ' + node.name
132+
133+
def leave_EnumTypeDefinition(self, node, *args):
134+
return 'enum ' + node.name + ' ' + block(node.values)
135+
136+
def leave_EnumValueDefinition(self, node, *args):
137+
return node.name
138+
139+
def leave_InputObjectTypeDefinition(self, node, *args):
140+
return 'input ' + node.name + ' ' + block(node.fields)
141+
142+
def leave_TypeExtensionDefinition(self, node, *args):
143+
return 'extend ' + node.definition
144+
107145

108146
def join(maybe_list, separator=''):
109147
if maybe_list:

graphql/core/language/visitor_meta.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232

3333
ast.ObjectTypeDefinition: ('name', 'interfaces', 'fields'),
3434
ast.FieldDefinition: ('name', 'arguments', 'type'),
35-
ast.InputValueDefinition: ('name', 'type', 'defaultValue'),
35+
ast.InputValueDefinition: ('name', 'type', 'default_value'),
3636
ast.InterfaceTypeDefinition: ('name', 'fields'),
3737
ast.UnionTypeDefinition: ('name', 'types'),
3838
ast.ScalarTypeDefinition: ('name',),

graphql/core/utils/type_info.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import six
22

3-
from ..language import ast, visitor_meta
3+
from ..language import visitor_meta
44
from ..type.definition import (
55
GraphQLInputObjectType,
66
GraphQLList,
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
from copy import deepcopy
2+
from graphql.core import parse
3+
from pytest import raises
4+
from graphql.core.language.printer import print_ast
5+
from graphql.core.language import ast
6+
from .fixtures import SCHEMA_KITCHEN_SINK
7+
8+
9+
def test_prints_minimal_ast():
10+
node = ast.ScalarTypeDefinition(
11+
name=ast.Name('foo')
12+
)
13+
14+
assert print_ast(node) == 'scalar foo'
15+
16+
17+
def test_print_produces_helpful_error_messages():
18+
bad_ast = {'random': 'Data'}
19+
with raises(AssertionError) as excinfo:
20+
print_ast(bad_ast)
21+
22+
assert "Invalid AST Node: {'random': 'Data'}" in str(excinfo.value)
23+
24+
25+
def test_does_not_alter_ast():
26+
ast = parse(SCHEMA_KITCHEN_SINK)
27+
ast_copy = deepcopy(ast)
28+
print_ast(ast)
29+
assert ast == ast_copy
30+
31+
32+
def test_prints_kitchen_sink():
33+
ast = parse(SCHEMA_KITCHEN_SINK)
34+
printed = print_ast(ast)
35+
36+
expected = '''type Foo implements Bar {
37+
one: Type
38+
two(argument: InputType!): Type
39+
three(argument: InputType, other: String): Int
40+
four(argument: String = "string"): String
41+
five(argument: [String] = ["string", "string"]): String
42+
six(argument: InputType = {key: "value"}): Type
43+
}
44+
45+
interface Bar {
46+
one: Type
47+
four(argument: String = "string"): String
48+
}
49+
50+
union Feed = Story | Article | Advert
51+
52+
scalar CustomScalar
53+
54+
enum Site {
55+
DESKTOP
56+
MOBILE
57+
}
58+
59+
input InputType {
60+
key: String!
61+
answer: Int = 42
62+
}
63+
64+
extend type Foo {
65+
seven(argument: [String]): Type
66+
}
67+
'''
68+
69+
assert printed == expected

0 commit comments

Comments
 (0)