Skip to content

Commit c994812

Browse files
committed
Await in parallel
1 parent b877e10 commit c994812

File tree

4 files changed

+274
-248
lines changed

4 files changed

+274
-248
lines changed

graphql/execution/execute.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,6 @@ def execute_fields(
428428
)
429429
if result is not INVALID:
430430
results[response_name] = result
431-
432431
if not is_async and isawaitable(result):
433432
is_async = True
434433

@@ -445,13 +444,11 @@ async def get_results():
445444
async def await_kv(key, value):
446445
return key, await value if isawaitable(value) else value
447446

448-
return {
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-
)
454-
}
447+
pairs = await gather(
448+
*(await_kv(key, value) for key, value in results.items())
449+
)
450+
451+
return dict(pairs)
455452

456453
return get_results()
457454

tests/execution/test_executor.py

Lines changed: 67 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
GraphQLResolveInfo,
2121
ResponsePath,
2222
)
23+
from .util import compare_query_results_unordered
2324

2425

2526
def describe_execute_handles_basic_execution_tasks():
@@ -417,67 +418,70 @@ async def asyncReturnErrorWithExtensions(self, _info):
417418
)
418419
)
419420

420-
assert await execute(schema, ast, Data()) == (
421-
{
422-
"syncOk": "sync ok",
423-
"syncError": None,
424-
"syncRawError": None,
425-
"syncReturnError": None,
426-
"syncReturnErrorList": ["sync0", None, "sync2", None],
427-
"asyncOk": "async ok",
428-
"asyncError": None,
429-
"asyncRawError": None,
430-
"asyncReturnError": None,
431-
"asyncReturnErrorWithExtensions": None,
432-
},
433-
[
434-
{
435-
"message": "Error getting syncError",
436-
"locations": [(3, 15)],
437-
"path": ["syncError"],
438-
},
439-
{
440-
"message": "Error getting syncRawError",
441-
"locations": [(4, 15)],
442-
"path": ["syncRawError"],
443-
},
444-
{
445-
"message": "Error getting syncReturnError",
446-
"locations": [(5, 15)],
447-
"path": ["syncReturnError"],
448-
},
449-
{
450-
"message": "Error getting syncReturnErrorList1",
451-
"locations": [(6, 15)],
452-
"path": ["syncReturnErrorList", 1],
453-
},
454-
{
455-
"message": "Error getting syncReturnErrorList3",
456-
"locations": [(6, 15)],
457-
"path": ["syncReturnErrorList", 3],
458-
},
459-
{
460-
"message": "Error getting asyncError",
461-
"locations": [(8, 15)],
462-
"path": ["asyncError"],
463-
},
464-
{
465-
"message": "Error getting asyncRawError",
466-
"locations": [(9, 15)],
467-
"path": ["asyncRawError"],
468-
},
469-
{
470-
"message": "Error getting asyncReturnError",
471-
"locations": [(10, 15)],
472-
"path": ["asyncReturnError"],
473-
},
421+
compare_query_results_unordered(
422+
await execute(schema, ast, Data()),
423+
(
474424
{
475-
"message": "Error getting asyncReturnErrorWithExtensions",
476-
"locations": [(11, 15)],
477-
"path": ["asyncReturnErrorWithExtensions"],
478-
"extensions": {"foo": "bar"},
425+
"syncOk": "sync ok",
426+
"syncError": None,
427+
"syncRawError": None,
428+
"syncReturnError": None,
429+
"syncReturnErrorList": ["sync0", None, "sync2", None],
430+
"asyncOk": "async ok",
431+
"asyncError": None,
432+
"asyncRawError": None,
433+
"asyncReturnError": None,
434+
"asyncReturnErrorWithExtensions": None,
479435
},
480-
],
436+
[
437+
{
438+
"message": "Error getting syncError",
439+
"locations": [(3, 15)],
440+
"path": ["syncError"],
441+
},
442+
{
443+
"message": "Error getting syncRawError",
444+
"locations": [(4, 15)],
445+
"path": ["syncRawError"],
446+
},
447+
{
448+
"message": "Error getting syncReturnError",
449+
"locations": [(5, 15)],
450+
"path": ["syncReturnError"],
451+
},
452+
{
453+
"message": "Error getting syncReturnErrorList1",
454+
"locations": [(6, 15)],
455+
"path": ["syncReturnErrorList", 1],
456+
},
457+
{
458+
"message": "Error getting syncReturnErrorList3",
459+
"locations": [(6, 15)],
460+
"path": ["syncReturnErrorList", 3],
461+
},
462+
{
463+
"message": "Error getting asyncError",
464+
"locations": [(8, 15)],
465+
"path": ["asyncError"],
466+
},
467+
{
468+
"message": "Error getting asyncRawError",
469+
"locations": [(9, 15)],
470+
"path": ["asyncRawError"],
471+
},
472+
{
473+
"message": "Error getting asyncReturnError",
474+
"locations": [(10, 15)],
475+
"path": ["asyncReturnError"],
476+
},
477+
{
478+
"message": "Error getting asyncReturnErrorWithExtensions",
479+
"locations": [(11, 15)],
480+
"path": ["asyncReturnErrorWithExtensions"],
481+
"extensions": {"foo": "bar"},
482+
},
483+
],
484+
),
481485
)
482486

483487
def full_response_path_is_included_for_non_nullable_fields():
@@ -893,19 +897,15 @@ async def f(*args):
893897
{
894898
"foo": GraphQLField(GraphQLBoolean, resolve=f),
895899
"bar": GraphQLField(GraphQLBoolean, resolve=f),
896-
}
900+
},
897901
)
898902
)
899903

900-
query = '{foo, bar}'
904+
query = "{foo, bar}"
901905
ast = parse(query)
902906

903907
res = await asyncio.wait_for(
904-
execute(schema, ast),
905-
1.0, # don't wait forever for the test to fail
908+
execute(schema, ast), 1.0 # don't wait forever for the test to fail
906909
)
907910

908-
assert res == (
909-
{"foo": True, "bar": True},
910-
None,
911-
)
911+
assert res == ({"foo": True, "bar": True}, None)

0 commit comments

Comments
 (0)