diff --git a/gql/utilities/parse_result.py b/gql/utilities/parse_result.py index ede627ae..02355425 100644 --- a/gql/utilities/parse_result.py +++ b/gql/utilities/parse_result.py @@ -193,7 +193,7 @@ def enter_field( # Key not found in result. # Should never happen in theory with a correct GraphQL backend # Silently ignoring this field - log.debug(f"Key {name} not found in result --> REMOVE") + log.debug(f" Key {name} not found in result --> REMOVE") return REMOVE log.debug(f" result_value={result_value}") @@ -232,8 +232,11 @@ def enter_field( ) # Get parent SelectionSet node - new_node = ancestors[-1] - assert isinstance(new_node, SelectionSetNode) + selection_set_node = ancestors[-1] + assert isinstance(selection_set_node, SelectionSetNode) + + # Keep only the current node in a new selection set node + new_node = SelectionSetNode(selections=[node]) for item in result_value: diff --git a/tests/starwars/test_parse_results.py b/tests/starwars/test_parse_results.py index 23073839..e8f3f8d4 100644 --- a/tests/starwars/test_parse_results.py +++ b/tests/starwars/test_parse_results.py @@ -37,6 +37,42 @@ def test_hero_name_and_friends_query(): assert result == parsed_result +def test_hero_name_and_friends_query_with_fragment(): + """Testing for issue #445""" + + query = gql( + """ + query HeroNameAndFriendsQuery { + hero { + ...HeroSummary + friends { + name + } + } + } + fragment HeroSummary on Character { + id + name + } + """ + ) + result = { + "hero": { + "id": "2001", + "friends": [ + {"name": "Luke Skywalker"}, + {"name": "Han Solo"}, + {"name": "Leia Organa"}, + ], + "name": "R2-D2", + } + } + + parsed_result = parse_result(StarWarsSchema, query, result) + + assert result == parsed_result + + def test_key_not_found_in_result(): query = gql(