-
-
Notifications
You must be signed in to change notification settings - Fork 140
Cancel remaining fields on exceptions #238
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,16 @@ | |
|
||
from __future__ import annotations | ||
|
||
from asyncio import ensure_future, gather, shield, wait_for | ||
from asyncio import ( | ||
FIRST_EXCEPTION, | ||
CancelledError, | ||
create_task, | ||
ensure_future, | ||
gather, | ||
shield, | ||
wait, | ||
wait_for, | ||
) | ||
from contextlib import suppress | ||
from copy import copy | ||
from typing import ( | ||
|
@@ -459,12 +468,18 @@ async def get_results() -> dict[str, Any]: | |
field = awaitable_fields[0] | ||
results[field] = await results[field] | ||
else: | ||
results.update( | ||
zip( | ||
awaitable_fields, | ||
await gather(*(results[field] for field in awaitable_fields)), | ||
) | ||
) | ||
tasks = {} | ||
for field in awaitable_fields: | ||
tasks[create_task(results[field])] = field # type: ignore[arg-type] | ||
|
||
done, pending = await wait(tasks, return_when=FIRST_EXCEPTION) | ||
if pending: | ||
for task in pending: | ||
task.cancel() | ||
await wait(pending) | ||
|
||
results.update((tasks[task], task.result()) for task in done) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This maintains the previous behaviour of not setting any results if an exception is raised, but I could change this to set the results we do have and then raise the exception. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this behave in-line with the spec regarding partial results? https://spec.graphql.org/draft/#sec-Handling-Execution-Errors |
||
|
||
return results | ||
|
||
return get_results() | ||
|
@@ -538,6 +553,10 @@ async def await_completed() -> Any: | |
try: | ||
return await completed | ||
except Exception as raw_error: | ||
# Before Python 3.8 CancelledError inherits Exception and | ||
# so gets caught here. | ||
if isinstance(raw_error, CancelledError): | ||
raise | ||
self.handle_field_error( | ||
raw_error, | ||
return_type, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The other approach is to allow all fields to finish executing and then raise the exception.