Skip to content

Fix parsing of None with parse_results=True #326

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
May 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 4 additions & 9 deletions gql/utilities/parse_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand All @@ -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

Expand Down
98 changes: 98 additions & 0 deletions tests/custom_scalars/test_parse_results.py
Original file line number Diff line number Diff line change
@@ -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
}