Skip to content

Commit

Permalink
Make print() break long List and Object Values over multiple line
Browse files Browse the repository at this point in the history
  • Loading branch information
Cito committed Apr 7, 2024
1 parent 860064f commit e8559b0
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/graphql/language/printer.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,11 +200,19 @@ def leave_enum_value(node: PrintedNode, *_args: Any) -> str:

@staticmethod
def leave_list_value(node: PrintedNode, *_args: Any) -> str:
return f"[{join(node.values, ', ')}]"
values = node.values
values_line = f"[{join(values, ', ')}]"
return (
"\n".join(("[", indent(join(values, "\n")), "]"))
if len(values_line) > 80
else values_line
)

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

@staticmethod
def leave_object_field(node: PrintedNode, *_args: Any) -> str:
Expand Down
69 changes: 69 additions & 0 deletions tests/language/test_printer.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,75 @@ def puts_arguments_on_multiple_lines_if_line_has_more_than_80_chars():
"""
)

def puts_large_object_values_on_multiple_lines_if_line_has_more_than_80_chars():
printed = print_ast(
parse(
"{trip(obj:{wheelchair:false,smallObj:{a: 1},largeObj:"
"{wheelchair:false,smallObj:{a: 1},arriveBy:false,"
"includePlannedCancellations:true,transitDistanceReluctance:2000,"
'anotherLongFieldName:"Lots and lots and lots and lots of text"},'
"arriveBy:false,includePlannedCancellations:true,"
"transitDistanceReluctance:2000,anotherLongFieldName:"
'"Lots and lots and lots and lots of text"}){dateTime}}'
)
)

assert printed == dedent(
"""
{
trip(
obj: {
wheelchair: false
smallObj: { a: 1 }
largeObj: {
wheelchair: false
smallObj: { a: 1 }
arriveBy: false
includePlannedCancellations: true
transitDistanceReluctance: 2000
anotherLongFieldName: "Lots and lots and lots and lots of text"
}
arriveBy: false
includePlannedCancellations: true
transitDistanceReluctance: 2000
anotherLongFieldName: "Lots and lots and lots and lots of text"
}
) {
dateTime
}
}
"""
)

def puts_large_list_values_on_multiple_lines_if_line_has_more_than_80_chars():
printed = print_ast(
parse(
'{trip(list:[["small array", "small", "small"],'
' ["Lots and lots and lots and lots of text",'
' "Lots and lots and lots and lots of text",'
' "Lots and lots and lots and lots of text"]]){dateTime}}'
)
)

assert printed == dedent(
"""
{
trip(
list: [
["small array", "small", "small"]
[
"Lots and lots and lots and lots of text"
"Lots and lots and lots and lots of text"
"Lots and lots and lots and lots of text"
]
]
) {
dateTime
}
}
"""
)

def legacy_prints_fragment_with_variable_directives():
query_ast_with_variable_directive = parse(
"fragment Foo($foo: TestType @test) on TestType @testDirective { id }",
Expand Down

0 comments on commit e8559b0

Please sign in to comment.