Skip to content

Commit 7355769

Browse files
committed
printer_ast: remove add_description wrapper
Replicates graphql/graphql-js@b6b2789
1 parent 81798ba commit 7355769

File tree

1 file changed

+33
-35
lines changed

1 file changed

+33
-35
lines changed

src/graphql/language/printer.py

Lines changed: 33 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
from functools import wraps
21
from json import dumps
3-
from typing import Any, Callable, Collection, Optional
2+
from typing import Any, Collection, Optional
43

54
from ..language.ast import Node, OperationType
65
from .visitor import visit, Visitor
@@ -50,16 +49,6 @@ def print_ast(ast: Node) -> str:
5049
return visit(ast, PrintAstVisitor())
5150

5251

53-
def add_description(method: Callable[..., str]) -> Callable:
54-
"""Decorator adding the description to the output of a static visitor method."""
55-
56-
@wraps(method)
57-
def wrapped(node: PrintedNode, *args: Any) -> str:
58-
return join((node.description, method(node, *args)), "\n")
59-
60-
return wrapped
61-
62-
6352
class PrintAstVisitor(Visitor):
6453
@staticmethod
6554
def leave_name(node: PrintedNode, *_args: Any) -> str:
@@ -206,25 +195,34 @@ def leave_non_null_type(node: PrintedNode, *_args: Any) -> str:
206195
# Type System Definitions
207196

208197
@staticmethod
209-
@add_description
210198
def leave_schema_definition(node: PrintedNode, *_args: Any) -> str:
211-
return join(
212-
("schema", join(node.directives, " "), block(node.operation_types)), " "
199+
return wrap("", node.description, "\n") + join(
200+
(
201+
"schema",
202+
join(node.directives, " "),
203+
block(node.operation_types),
204+
),
205+
" ",
213206
)
214207

215208
@staticmethod
216209
def leave_operation_type_definition(node: PrintedNode, *_args: Any) -> str:
217210
return f"{node.operation.value}: {node.type}"
218211

219212
@staticmethod
220-
@add_description
221213
def leave_scalar_type_definition(node: PrintedNode, *_args: Any) -> str:
222-
return join(("scalar", node.name, join(node.directives, " ")), " ")
214+
return wrap("", node.description, "\n") + join(
215+
(
216+
"scalar",
217+
node.name,
218+
join(node.directives, " "),
219+
),
220+
" ",
221+
)
223222

224223
@staticmethod
225-
@add_description
226224
def leave_object_type_definition(node: PrintedNode, *_args: Any) -> str:
227-
return join(
225+
return wrap("", node.description, "\n") + join(
228226
(
229227
"type",
230228
node.name,
@@ -236,7 +234,6 @@ def leave_object_type_definition(node: PrintedNode, *_args: Any) -> str:
236234
)
237235

238236
@staticmethod
239-
@add_description
240237
def leave_field_definition(node: PrintedNode, *_args: Any) -> str:
241238
args = node.arguments
242239
args = (
@@ -245,12 +242,14 @@ def leave_field_definition(node: PrintedNode, *_args: Any) -> str:
245242
else wrap("(", join(args, ", "), ")")
246243
)
247244
directives = wrap(" ", join(node.directives, " "))
248-
return f"{node.name}{args}: {node.type}{directives}"
245+
return (
246+
wrap("", node.description, "\n")
247+
+ f"{node.name}{args}: {node.type}{directives}"
248+
)
249249

250250
@staticmethod
251-
@add_description
252251
def leave_input_value_definition(node: PrintedNode, *_args: Any) -> str:
253-
return join(
252+
return wrap("", node.description, "\n") + join(
254253
(
255254
f"{node.name}: {node.type}",
256255
wrap("= ", node.default_value),
@@ -260,9 +259,8 @@ def leave_input_value_definition(node: PrintedNode, *_args: Any) -> str:
260259
)
261260

262261
@staticmethod
263-
@add_description
264262
def leave_interface_type_definition(node: PrintedNode, *_args: Any) -> str:
265-
return join(
263+
return wrap("", node.description, "\n") + join(
266264
(
267265
"interface",
268266
node.name,
@@ -274,9 +272,8 @@ def leave_interface_type_definition(node: PrintedNode, *_args: Any) -> str:
274272
)
275273

276274
@staticmethod
277-
@add_description
278275
def leave_union_type_definition(node: PrintedNode, *_args: Any) -> str:
279-
return join(
276+
return wrap("", node.description, "\n") + join(
280277
(
281278
"union",
282279
node.name,
@@ -287,26 +284,24 @@ def leave_union_type_definition(node: PrintedNode, *_args: Any) -> str:
287284
)
288285

289286
@staticmethod
290-
@add_description
291287
def leave_enum_type_definition(node: PrintedNode, *_args: Any) -> str:
292-
return join(
288+
return wrap("", node.description, "\n") + join(
293289
("enum", node.name, join(node.directives, " "), block(node.values)), " "
294290
)
295291

296292
@staticmethod
297-
@add_description
298293
def leave_enum_value_definition(node: PrintedNode, *_args: Any) -> str:
299-
return join((node.name, join(node.directives, " ")), " ")
294+
return wrap("", node.description, "\n") + join(
295+
(node.name, join(node.directives, " ")), " "
296+
)
300297

301298
@staticmethod
302-
@add_description
303299
def leave_input_object_type_definition(node: PrintedNode, *_args: Any) -> str:
304-
return join(
300+
return wrap("", node.description, "\n") + join(
305301
("input", node.name, join(node.directives, " "), block(node.fields)), " "
306302
)
307303

308304
@staticmethod
309-
@add_description
310305
def leave_directive_definition(node: PrintedNode, *_args: Any) -> str:
311306
args = node.arguments
312307
args = (
@@ -316,7 +311,10 @@ def leave_directive_definition(node: PrintedNode, *_args: Any) -> str:
316311
)
317312
repeatable = " repeatable" if node.repeatable else ""
318313
locations = join(node.locations, " | ")
319-
return f"directive @{node.name}{args}{repeatable} on {locations}"
314+
return (
315+
wrap("", node.description, "\n")
316+
+ f"directive @{node.name}{args}{repeatable} on {locations}"
317+
)
320318

321319
@staticmethod
322320
def leave_schema_extension(node: PrintedNode, *_args: Any) -> str:

0 commit comments

Comments
 (0)