Skip to content
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

Fix parse_results with fragments #446

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
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
Loading