Skip to content

Commit

Permalink
Fix parse_results with fragments (#446)
Browse files Browse the repository at this point in the history
* Fix issue #445
  • Loading branch information
leszekhanusz authored Nov 14, 2023
1 parent 87fac0f commit ff6352b
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
9 changes: 6 additions & 3 deletions gql/utilities/parse_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand Down Expand Up @@ -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:

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

0 comments on commit ff6352b

Please sign in to comment.