Skip to content

Commit 1e6ff7f

Browse files
committed
add operation_name to parse_results
1 parent 7ea8a8c commit 1e6ff7f

File tree

3 files changed

+85
-5
lines changed

3 files changed

+85
-5
lines changed

gql/client.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,12 @@ def _execute(
350350
# Unserialize the result if requested
351351
if self.client.schema:
352352
if parse_result or (parse_result is None and self.client.parse_results):
353-
result.data = parse_result_fn(self.client.schema, document, result.data)
353+
result.data = parse_result_fn(
354+
self.client.schema,
355+
document,
356+
result.data,
357+
operation_name=operation_name,
358+
)
354359

355360
return result
356361

@@ -495,7 +500,10 @@ async def _subscribe(
495500
parse_result is None and self.client.parse_results
496501
):
497502
result.data = parse_result_fn(
498-
self.client.schema, document, result.data
503+
self.client.schema,
504+
document,
505+
result.data,
506+
operation_name=operation_name,
499507
)
500508

501509
yield result
@@ -611,7 +619,12 @@ async def _execute(
611619
# Unserialize the result if requested
612620
if self.client.schema:
613621
if parse_result or (parse_result is None and self.client.parse_results):
614-
result.data = parse_result_fn(self.client.schema, document, result.data)
622+
result.data = parse_result_fn(
623+
self.client.schema,
624+
document,
625+
result.data,
626+
operation_name=operation_name,
627+
)
615628

616629
return result
617630

gql/utilities/parse_result.py

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
GraphQLSchema,
1717
GraphQLType,
1818
InlineFragmentNode,
19+
NameNode,
1920
Node,
2021
OperationDefinitionNode,
2122
SelectionSetNode,
@@ -71,6 +72,7 @@ def __init__(
7172
type_info: TypeInfo,
7273
visit_fragment: bool = False,
7374
inside_list_level: int = 0,
75+
operation_name: Optional[str] = None,
7476
):
7577
"""Recursive Implementation of a Visitor class to parse results
7678
correspondind to a schema and a document.
@@ -96,6 +98,7 @@ def __init__(
9698
self.type_info: TypeInfo = type_info
9799
self.visit_fragment: bool = visit_fragment
98100
self.inside_list_level = inside_list_level
101+
self.operation_name = operation_name
99102

100103
self.result_stack: List[Any] = []
101104

@@ -111,6 +114,22 @@ def leave_document(node: DocumentNode, *_args: Any) -> Dict[str, Any]:
111114
results = cast(List[Dict[str, Any]], node.definitions)
112115
return {k: v for result in results for k, v in result.items()}
113116

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+
114133
@staticmethod
115134
def leave_operation_definition(
116135
node: OperationDefinitionNode, *_args: Any
@@ -374,6 +393,7 @@ def parse_result_recursive(
374393
initial_type: Optional[GraphQLType] = None,
375394
inside_list_level: int = 0,
376395
visit_fragment: bool = False,
396+
operation_name: Optional[str] = None,
377397
) -> Any:
378398

379399
if result is None:
@@ -393,6 +413,7 @@ def parse_result_recursive(
393413
type_info=type_info,
394414
inside_list_level=inside_list_level,
395415
visit_fragment=visit_fragment,
416+
operation_name=operation_name,
396417
),
397418
),
398419
visitor_keys=RESULT_DOCUMENT_KEYS,
@@ -402,13 +423,17 @@ def parse_result_recursive(
402423

403424

404425
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,
406430
) -> Optional[Dict[str, Any]]:
407431
"""Unserialize a result received from a GraphQL backend.
408432
409433
:param schema: the GraphQL schema
410434
:param document: the document representing the query sent to the backend
411435
:param result: the serialized result received from the backend
436+
:param operation_name: the optional operation name
412437
413438
:returns: a parsed result with scalars and enums parsed depending on
414439
their definition in the schema.
@@ -423,4 +448,6 @@ def parse_result(
423448
will be parsed with the parse_value method of the custom scalar or enum
424449
definition in the schema."""
425450

426-
return parse_result_recursive(schema, document, document, result)
451+
return parse_result_recursive(
452+
schema, document, document, result, operation_name=operation_name
453+
)

tests/custom_scalars/test_enum_colors.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,3 +283,43 @@ def test_update_schema_enum_errors():
283283
update_schema_enum(schema, "Color", {"RED": Color.RED})
284284

285285
assert 'Enum key "GREEN" not found in provided values!' in str(exc_info)
286+
287+
288+
def test_parse_results_with_operation_type():
289+
290+
client = Client(schema=schema, parse_results=True)
291+
292+
query = gql(
293+
"""
294+
query GetAll {
295+
all
296+
}
297+
query GetOppositeColor($color: Color) {
298+
opposite(color:$color)
299+
}
300+
query GetOppositeColor2($color: Color) {
301+
other_opposite:opposite(color:$color)
302+
}
303+
query GetOppositeColor3 {
304+
opposite(color: YELLOW)
305+
}
306+
query GetListOfListOfList {
307+
list_of_list_of_list
308+
}
309+
"""
310+
)
311+
312+
variable_values = {
313+
"color": "RED",
314+
}
315+
316+
result = client.execute(
317+
query, variable_values=variable_values, operation_name="GetOppositeColor"
318+
)
319+
320+
print(result)
321+
322+
opposite_color = result["opposite"]
323+
324+
assert isinstance(opposite_color, Color)
325+
assert opposite_color == CYAN

0 commit comments

Comments
 (0)