Skip to content

Commit 321c606

Browse files
Fix parsing of None with parse_results=True (#326)
1 parent 64c9b5b commit 321c606

File tree

2 files changed

+102
-9
lines changed

2 files changed

+102
-9
lines changed

gql/utilities/parse_result.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -293,8 +293,7 @@ def leave_field(
293293

294294
if self.current_result is None:
295295

296-
log.debug(f"Leave field {name}: returning None")
297-
return {name: None}
296+
return_value = None
298297

299298
elif node.selection_set is None:
300299

@@ -308,23 +307,19 @@ def leave_field(
308307
assert is_leaf_type(result_type)
309308

310309
# Finally parsing a single scalar using the parse_value method
311-
parsed_value = result_type.parse_value(self.current_result)
312-
313-
return_value = {name: parsed_value}
310+
return_value = result_type.parse_value(self.current_result)
314311
else:
315312

316313
partial_results = cast(List[Dict[str, Any]], node.selection_set)
317314

318-
return_value = {
319-
name: {k: v for pr in partial_results for k, v in pr.items()}
320-
}
315+
return_value = {k: v for pr in partial_results for k, v in pr.items()}
321316

322317
# Go up a level in the result stack
323318
self.result_stack.pop()
324319

325320
log.debug(f"Leave field {name}: returning {return_value}")
326321

327-
return return_value
322+
return {name: return_value}
328323

329324
# Fragments
330325

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
from graphql.type import (
2+
GraphQLArgument,
3+
GraphQLField,
4+
GraphQLInt,
5+
GraphQLList,
6+
GraphQLNonNull,
7+
GraphQLObjectType,
8+
GraphQLSchema,
9+
GraphQLString,
10+
)
11+
12+
from gql import Client, gql
13+
14+
static_result = {
15+
"edges": [
16+
{
17+
"node": {
18+
"from": {"address": "0x45b9ad45995577fe"},
19+
"to": {"address": "0x6394e988297f5ed2"},
20+
}
21+
},
22+
{"node": {"from": None, "to": {"address": "0x6394e988297f5ed2"}}},
23+
]
24+
}
25+
26+
27+
def resolve_test(root, _info, count):
28+
return static_result
29+
30+
31+
Account = GraphQLObjectType(
32+
name="Account",
33+
fields={"address": GraphQLField(GraphQLNonNull(GraphQLString))},
34+
)
35+
36+
37+
queryType = GraphQLObjectType(
38+
name="RootQueryType",
39+
fields={
40+
"test": GraphQLField(
41+
GraphQLObjectType(
42+
name="test",
43+
fields={
44+
"edges": GraphQLField(
45+
GraphQLList(
46+
GraphQLObjectType(
47+
"example",
48+
fields={
49+
"node": GraphQLField(
50+
GraphQLObjectType(
51+
name="node",
52+
fields={
53+
"from": GraphQLField(Account),
54+
"to": GraphQLField(Account),
55+
},
56+
)
57+
)
58+
},
59+
)
60+
)
61+
)
62+
},
63+
),
64+
args={"count": GraphQLArgument(GraphQLInt)},
65+
resolve=resolve_test,
66+
),
67+
},
68+
)
69+
70+
schema = GraphQLSchema(query=queryType)
71+
72+
73+
def test_parse_results_null_mapping():
74+
"""This is a regression test for the issue:
75+
https://github.com/graphql-python/gql/issues/325
76+
77+
Most of the parse_results tests are in tests/starwars/test_parse_results.py
78+
"""
79+
80+
client = Client(schema=schema, parse_results=True)
81+
query = gql(
82+
"""query testQ($count: Int) {test(count: $count){
83+
edges {
84+
node {
85+
from {
86+
address
87+
}
88+
to {
89+
address
90+
}
91+
}
92+
}
93+
} }"""
94+
)
95+
96+
assert client.execute(query, variable_values={"count": 2}) == {
97+
"test": static_result
98+
}

0 commit comments

Comments
 (0)