Skip to content

Commit

Permalink
locate async iterator errors to the collection
Browse files Browse the repository at this point in the history
  • Loading branch information
Cito committed Apr 9, 2024
1 parent ddfe2bf commit e7f3b01
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 21 deletions.
26 changes: 11 additions & 15 deletions src/graphql/execution/execute.py
Original file line number Diff line number Diff line change
Expand Up @@ -1275,15 +1275,9 @@ async def complete_async_iterator_value(
except StopAsyncIteration:
break
except Exception as raw_error:
self.handle_field_error(
raw_error,
item_type,
field_group,
item_path,
incremental_data_record,
)
completed_results.append(None)
break
raise located_error(
raw_error, field_group, path.as_list()
) from raw_error
if complete_list_item_value(
value,
completed_results,
Expand Down Expand Up @@ -1897,27 +1891,28 @@ async def execute_stream_async_iterator_item(
info: GraphQLResolveInfo,
item_type: GraphQLOutputType,
incremental_data_record: StreamItemsRecord,
path: Path,
item_path: Path,
) -> Any:
"""Execute stream iterator item."""
if async_iterator in self._canceled_iterators:
raise StopAsyncIteration
try:
item = await anext(async_iterator)
except StopAsyncIteration as raw_error:
incremental_data_record.set_is_completed_async_iterator()
raise StopAsyncIteration from raw_error
except Exception as raw_error:
raise located_error(raw_error, field_group, path.as_list()) from raw_error
try:
completed_item = self.complete_value(
item_type, field_group, info, item_path, item, incremental_data_record
)

return (
await completed_item
if self.is_awaitable(completed_item)
else completed_item
)

except StopAsyncIteration as raw_error:
incremental_data_record.set_is_completed_async_iterator()
raise StopAsyncIteration from raw_error

except Exception as raw_error:
self.handle_field_error(
raw_error, item_type, field_group, item_path, incremental_data_record
Expand Down Expand Up @@ -1952,6 +1947,7 @@ async def execute_stream_async_iterator(
info,
item_type,
incremental_data_record,
path,
item_path,
)
except StopAsyncIteration:
Expand Down
4 changes: 2 additions & 2 deletions tests/execution/test_lists.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,8 @@ async def list_field():
raise RuntimeError("bad")

assert await _complete(list_field()) == (
{"listField": ["two", "4", None]},
[{"message": "bad", "locations": [(1, 3)], "path": ["listField", 2]}],
{"listField": None},
[{"message": "bad", "locations": [(1, 3)], "path": ["listField"]}],
)

@pytest.mark.asyncio()
Expand Down
8 changes: 4 additions & 4 deletions tests/execution/test_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -893,10 +893,10 @@ async def friend_list(_info):
{
"message": "bad",
"locations": [{"line": 3, "column": 15}],
"path": ["friendList", 1],
"path": ["friendList"],
}
],
"data": {"friendList": [{"name": "Luke", "id": "1"}, None]},
"data": {"friendList": None},
}

@pytest.mark.asyncio()
Expand Down Expand Up @@ -929,13 +929,13 @@ async def friend_list(_info):
{
"incremental": [
{
"items": [None],
"items": None,
"path": ["friendList", 1],
"errors": [
{
"message": "bad",
"locations": [{"line": 3, "column": 15}],
"path": ["friendList", 1],
"path": ["friendList"],
},
],
},
Expand Down

0 comments on commit e7f3b01

Please sign in to comment.