Skip to content

Commit e8559b0

Browse files
committed
Make print() break long List and Object Values over multiple line
Replicates graphql/graphql-js@ddd6a01
1 parent 860064f commit e8559b0

File tree

2 files changed

+79
-2
lines changed

2 files changed

+79
-2
lines changed

src/graphql/language/printer.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,11 +200,19 @@ def leave_enum_value(node: PrintedNode, *_args: Any) -> str:
200200

201201
@staticmethod
202202
def leave_list_value(node: PrintedNode, *_args: Any) -> str:
203-
return f"[{join(node.values, ', ')}]"
203+
values = node.values
204+
values_line = f"[{join(values, ', ')}]"
205+
return (
206+
"\n".join(("[", indent(join(values, "\n")), "]"))
207+
if len(values_line) > 80
208+
else values_line
209+
)
204210

205211
@staticmethod
206212
def leave_object_value(node: PrintedNode, *_args: Any) -> str:
207-
return f"{{ {join(node.fields, ', ')} }}"
213+
fields = node.fields
214+
fields_line = f"{{ {join(fields, ', ')} }}"
215+
return block(fields) if len(fields_line) > MAX_LINE_LENGTH else fields_line
208216

209217
@staticmethod
210218
def leave_object_field(node: PrintedNode, *_args: Any) -> str:

tests/language/test_printer.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,75 @@ def puts_arguments_on_multiple_lines_if_line_has_more_than_80_chars():
106106
"""
107107
)
108108

109+
def puts_large_object_values_on_multiple_lines_if_line_has_more_than_80_chars():
110+
printed = print_ast(
111+
parse(
112+
"{trip(obj:{wheelchair:false,smallObj:{a: 1},largeObj:"
113+
"{wheelchair:false,smallObj:{a: 1},arriveBy:false,"
114+
"includePlannedCancellations:true,transitDistanceReluctance:2000,"
115+
'anotherLongFieldName:"Lots and lots and lots and lots of text"},'
116+
"arriveBy:false,includePlannedCancellations:true,"
117+
"transitDistanceReluctance:2000,anotherLongFieldName:"
118+
'"Lots and lots and lots and lots of text"}){dateTime}}'
119+
)
120+
)
121+
122+
assert printed == dedent(
123+
"""
124+
{
125+
trip(
126+
obj: {
127+
wheelchair: false
128+
smallObj: { a: 1 }
129+
largeObj: {
130+
wheelchair: false
131+
smallObj: { a: 1 }
132+
arriveBy: false
133+
includePlannedCancellations: true
134+
transitDistanceReluctance: 2000
135+
anotherLongFieldName: "Lots and lots and lots and lots of text"
136+
}
137+
arriveBy: false
138+
includePlannedCancellations: true
139+
transitDistanceReluctance: 2000
140+
anotherLongFieldName: "Lots and lots and lots and lots of text"
141+
}
142+
) {
143+
dateTime
144+
}
145+
}
146+
"""
147+
)
148+
149+
def puts_large_list_values_on_multiple_lines_if_line_has_more_than_80_chars():
150+
printed = print_ast(
151+
parse(
152+
'{trip(list:[["small array", "small", "small"],'
153+
' ["Lots and lots and lots and lots of text",'
154+
' "Lots and lots and lots and lots of text",'
155+
' "Lots and lots and lots and lots of text"]]){dateTime}}'
156+
)
157+
)
158+
159+
assert printed == dedent(
160+
"""
161+
{
162+
trip(
163+
list: [
164+
["small array", "small", "small"]
165+
[
166+
"Lots and lots and lots and lots of text"
167+
"Lots and lots and lots and lots of text"
168+
"Lots and lots and lots and lots of text"
169+
]
170+
]
171+
) {
172+
dateTime
173+
}
174+
}
175+
"""
176+
)
177+
109178
def legacy_prints_fragment_with_variable_directives():
110179
query_ast_with_variable_directive = parse(
111180
"fragment Foo($foo: TestType @test) on TestType @testDirective { id }",

0 commit comments

Comments
 (0)