diff --git a/gql/utilities/parse_result.py b/gql/utilities/parse_result.py index 5f9dd2a4..ede627ae 100644 --- a/gql/utilities/parse_result.py +++ b/gql/utilities/parse_result.py @@ -293,8 +293,7 @@ def leave_field( if self.current_result is None: - log.debug(f"Leave field {name}: returning None") - return {name: None} + return_value = None elif node.selection_set is None: @@ -308,23 +307,19 @@ def leave_field( assert is_leaf_type(result_type) # Finally parsing a single scalar using the parse_value method - parsed_value = result_type.parse_value(self.current_result) - - return_value = {name: parsed_value} + return_value = result_type.parse_value(self.current_result) else: partial_results = cast(List[Dict[str, Any]], node.selection_set) - return_value = { - name: {k: v for pr in partial_results for k, v in pr.items()} - } + return_value = {k: v for pr in partial_results for k, v in pr.items()} # Go up a level in the result stack self.result_stack.pop() log.debug(f"Leave field {name}: returning {return_value}") - return return_value + return {name: return_value} # Fragments diff --git a/tests/custom_scalars/test_parse_results.py b/tests/custom_scalars/test_parse_results.py new file mode 100644 index 00000000..e3c6d6f6 --- /dev/null +++ b/tests/custom_scalars/test_parse_results.py @@ -0,0 +1,98 @@ +from graphql.type import ( + GraphQLArgument, + GraphQLField, + GraphQLInt, + GraphQLList, + GraphQLNonNull, + GraphQLObjectType, + GraphQLSchema, + GraphQLString, +) + +from gql import Client, gql + +static_result = { + "edges": [ + { + "node": { + "from": {"address": "0x45b9ad45995577fe"}, + "to": {"address": "0x6394e988297f5ed2"}, + } + }, + {"node": {"from": None, "to": {"address": "0x6394e988297f5ed2"}}}, + ] +} + + +def resolve_test(root, _info, count): + return static_result + + +Account = GraphQLObjectType( + name="Account", + fields={"address": GraphQLField(GraphQLNonNull(GraphQLString))}, +) + + +queryType = GraphQLObjectType( + name="RootQueryType", + fields={ + "test": GraphQLField( + GraphQLObjectType( + name="test", + fields={ + "edges": GraphQLField( + GraphQLList( + GraphQLObjectType( + "example", + fields={ + "node": GraphQLField( + GraphQLObjectType( + name="node", + fields={ + "from": GraphQLField(Account), + "to": GraphQLField(Account), + }, + ) + ) + }, + ) + ) + ) + }, + ), + args={"count": GraphQLArgument(GraphQLInt)}, + resolve=resolve_test, + ), + }, +) + +schema = GraphQLSchema(query=queryType) + + +def test_parse_results_null_mapping(): + """This is a regression test for the issue: + https://github.com/graphql-python/gql/issues/325 + + Most of the parse_results tests are in tests/starwars/test_parse_results.py + """ + + client = Client(schema=schema, parse_results=True) + query = gql( + """query testQ($count: Int) {test(count: $count){ + edges { + node { + from { + address + } + to { + address + } + } + } + } }""" + ) + + assert client.execute(query, variable_values={"count": 2}) == { + "test": static_result + }