16
16
GraphQLSchema ,
17
17
GraphQLType ,
18
18
InlineFragmentNode ,
19
+ NameNode ,
19
20
Node ,
20
21
OperationDefinitionNode ,
21
22
SelectionSetNode ,
@@ -71,6 +72,7 @@ def __init__(
71
72
type_info : TypeInfo ,
72
73
visit_fragment : bool = False ,
73
74
inside_list_level : int = 0 ,
75
+ operation_name : Optional [str ] = None ,
74
76
):
75
77
"""Recursive Implementation of a Visitor class to parse results
76
78
correspondind to a schema and a document.
@@ -96,6 +98,7 @@ def __init__(
96
98
self .type_info : TypeInfo = type_info
97
99
self .visit_fragment : bool = visit_fragment
98
100
self .inside_list_level = inside_list_level
101
+ self .operation_name = operation_name
99
102
100
103
self .result_stack : List [Any ] = []
101
104
@@ -111,6 +114,22 @@ def leave_document(node: DocumentNode, *_args: Any) -> Dict[str, Any]:
111
114
results = cast (List [Dict [str , Any ]], node .definitions )
112
115
return {k : v for result in results for k , v in result .items ()}
113
116
117
+ def enter_operation_definition (
118
+ self , node : OperationDefinitionNode , * _args : Any
119
+ ) -> Union [None , VisitorActionEnum ]:
120
+
121
+ if self .operation_name is not None :
122
+ if not hasattr (node .name , "value" ):
123
+ return REMOVE # pragma: no cover
124
+
125
+ node .name = cast (NameNode , node .name )
126
+
127
+ if node .name .value != self .operation_name :
128
+ log .debug (f"SKIPPING operation { node .name .value } " )
129
+ return REMOVE
130
+
131
+ return IDLE
132
+
114
133
@staticmethod
115
134
def leave_operation_definition (
116
135
node : OperationDefinitionNode , * _args : Any
@@ -374,6 +393,7 @@ def parse_result_recursive(
374
393
initial_type : Optional [GraphQLType ] = None ,
375
394
inside_list_level : int = 0 ,
376
395
visit_fragment : bool = False ,
396
+ operation_name : Optional [str ] = None ,
377
397
) -> Any :
378
398
379
399
if result is None :
@@ -393,6 +413,7 @@ def parse_result_recursive(
393
413
type_info = type_info ,
394
414
inside_list_level = inside_list_level ,
395
415
visit_fragment = visit_fragment ,
416
+ operation_name = operation_name ,
396
417
),
397
418
),
398
419
visitor_keys = RESULT_DOCUMENT_KEYS ,
@@ -402,13 +423,17 @@ def parse_result_recursive(
402
423
403
424
404
425
def parse_result (
405
- schema : GraphQLSchema , document : DocumentNode , result : Optional [Dict [str , Any ]],
426
+ schema : GraphQLSchema ,
427
+ document : DocumentNode ,
428
+ result : Optional [Dict [str , Any ]],
429
+ operation_name : Optional [str ] = None ,
406
430
) -> Optional [Dict [str , Any ]]:
407
431
"""Unserialize a result received from a GraphQL backend.
408
432
409
433
:param schema: the GraphQL schema
410
434
:param document: the document representing the query sent to the backend
411
435
:param result: the serialized result received from the backend
436
+ :param operation_name: the optional operation name
412
437
413
438
:returns: a parsed result with scalars and enums parsed depending on
414
439
their definition in the schema.
@@ -423,4 +448,6 @@ def parse_result(
423
448
will be parsed with the parse_value method of the custom scalar or enum
424
449
definition in the schema."""
425
450
426
- return parse_result_recursive (schema , document , document , result )
451
+ return parse_result_recursive (
452
+ schema , document , document , result , operation_name = operation_name
453
+ )
0 commit comments