Skip to content

Commit b877e10

Browse files
committed
Await in parallel
1 parent 438aedc commit b877e10

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

graphql/execution/execute.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from asyncio import gather
12
from inspect import isawaitable
23
from typing import (
34
Any,
@@ -427,6 +428,7 @@ def execute_fields(
427428
)
428429
if result is not INVALID:
429430
results[response_name] = result
431+
430432
if not is_async and isawaitable(result):
431433
is_async = True
432434

@@ -437,11 +439,18 @@ def execute_fields(
437439
# Otherwise, results is a map from field name to the result of
438440
# resolving that field, which is possibly a coroutine object.
439441
# Return a coroutine object that will yield this same map, but with
440-
# any coroutines awaited and replaced with the values they yielded.
442+
# any coroutines awaited in parallel and replaced with the values they
443+
# yielded.
441444
async def get_results():
445+
async def await_kv(key, value):
446+
return key, await value if isawaitable(value) else value
447+
442448
return {
443-
key: await value if isawaitable(value) else value
444-
for key, value in results.items()
449+
key: value
450+
for key, value in await gather(
451+
*(await_kv(key, value) for key, value in results.items()),
452+
return_exceptions=True,
453+
)
445454
}
446455

447456
return get_results()

0 commit comments

Comments
 (0)