1
- from functools import wraps
2
1
from json import dumps
3
- from typing import Any , Callable , Collection , Optional
2
+ from typing import Any , Collection , Optional
4
3
5
4
from ..language .ast import Node , OperationType
6
5
from .visitor import visit , Visitor
@@ -50,16 +49,6 @@ def print_ast(ast: Node) -> str:
50
49
return visit (ast , PrintAstVisitor ())
51
50
52
51
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
-
63
52
class PrintAstVisitor (Visitor ):
64
53
@staticmethod
65
54
def leave_name (node : PrintedNode , * _args : Any ) -> str :
@@ -206,25 +195,34 @@ def leave_non_null_type(node: PrintedNode, *_args: Any) -> str:
206
195
# Type System Definitions
207
196
208
197
@staticmethod
209
- @add_description
210
198
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
+ " " ,
213
206
)
214
207
215
208
@staticmethod
216
209
def leave_operation_type_definition (node : PrintedNode , * _args : Any ) -> str :
217
210
return f"{ node .operation .value } : { node .type } "
218
211
219
212
@staticmethod
220
- @add_description
221
213
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
+ )
223
222
224
223
@staticmethod
225
- @add_description
226
224
def leave_object_type_definition (node : PrintedNode , * _args : Any ) -> str :
227
- return join (
225
+ return wrap ( "" , node . description , " \n " ) + join (
228
226
(
229
227
"type" ,
230
228
node .name ,
@@ -236,7 +234,6 @@ def leave_object_type_definition(node: PrintedNode, *_args: Any) -> str:
236
234
)
237
235
238
236
@staticmethod
239
- @add_description
240
237
def leave_field_definition (node : PrintedNode , * _args : Any ) -> str :
241
238
args = node .arguments
242
239
args = (
@@ -245,12 +242,14 @@ def leave_field_definition(node: PrintedNode, *_args: Any) -> str:
245
242
else wrap ("(" , join (args , ", " ), ")" )
246
243
)
247
244
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
+ )
249
249
250
250
@staticmethod
251
- @add_description
252
251
def leave_input_value_definition (node : PrintedNode , * _args : Any ) -> str :
253
- return join (
252
+ return wrap ( "" , node . description , " \n " ) + join (
254
253
(
255
254
f"{ node .name } : { node .type } " ,
256
255
wrap ("= " , node .default_value ),
@@ -260,9 +259,8 @@ def leave_input_value_definition(node: PrintedNode, *_args: Any) -> str:
260
259
)
261
260
262
261
@staticmethod
263
- @add_description
264
262
def leave_interface_type_definition (node : PrintedNode , * _args : Any ) -> str :
265
- return join (
263
+ return wrap ( "" , node . description , " \n " ) + join (
266
264
(
267
265
"interface" ,
268
266
node .name ,
@@ -274,9 +272,8 @@ def leave_interface_type_definition(node: PrintedNode, *_args: Any) -> str:
274
272
)
275
273
276
274
@staticmethod
277
- @add_description
278
275
def leave_union_type_definition (node : PrintedNode , * _args : Any ) -> str :
279
- return join (
276
+ return wrap ( "" , node . description , " \n " ) + join (
280
277
(
281
278
"union" ,
282
279
node .name ,
@@ -287,26 +284,24 @@ def leave_union_type_definition(node: PrintedNode, *_args: Any) -> str:
287
284
)
288
285
289
286
@staticmethod
290
- @add_description
291
287
def leave_enum_type_definition (node : PrintedNode , * _args : Any ) -> str :
292
- return join (
288
+ return wrap ( "" , node . description , " \n " ) + join (
293
289
("enum" , node .name , join (node .directives , " " ), block (node .values )), " "
294
290
)
295
291
296
292
@staticmethod
297
- @add_description
298
293
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
+ )
300
297
301
298
@staticmethod
302
- @add_description
303
299
def leave_input_object_type_definition (node : PrintedNode , * _args : Any ) -> str :
304
- return join (
300
+ return wrap ( "" , node . description , " \n " ) + join (
305
301
("input" , node .name , join (node .directives , " " ), block (node .fields )), " "
306
302
)
307
303
308
304
@staticmethod
309
- @add_description
310
305
def leave_directive_definition (node : PrintedNode , * _args : Any ) -> str :
311
306
args = node .arguments
312
307
args = (
@@ -316,7 +311,10 @@ def leave_directive_definition(node: PrintedNode, *_args: Any) -> str:
316
311
)
317
312
repeatable = " repeatable" if node .repeatable else ""
318
313
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
+ )
320
318
321
319
@staticmethod
322
320
def leave_schema_extension (node : PrintedNode , * _args : Any ) -> str :
0 commit comments