Skip to content

Commit 9374eca

Browse files
committed
Add test for mixing sync/async resolvers
Replicates graphql/graphql-js@4a82557
1 parent 657f508 commit 9374eca

File tree

1 file changed

+55
-6
lines changed

1 file changed

+55
-6
lines changed

tests/execution/test_executor.py

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
import asyncio
2-
from typing import cast, Any, Awaitable, Optional
3-
4-
from pytest import mark, raises
2+
from typing import Any, Awaitable, Optional, cast
53

64
from graphql.error import GraphQLError
75
from graphql.execution import execute, execute_sync
8-
from graphql.language import parse, FieldNode, OperationDefinitionNode
9-
from graphql.pyutils import inspect, Undefined
6+
from graphql.language import FieldNode, OperationDefinitionNode, parse
7+
from graphql.pyutils import Undefined, inspect
108
from graphql.type import (
119
GraphQLArgument,
1210
GraphQLBoolean,
@@ -17,12 +15,13 @@
1715
GraphQLNonNull,
1816
GraphQLObjectType,
1917
GraphQLResolveInfo,
20-
GraphQLSchema,
2118
GraphQLScalarType,
19+
GraphQLSchema,
2220
GraphQLString,
2321
GraphQLUnionType,
2422
ResponsePath,
2523
)
24+
from pytest import mark, raises
2625

2726

2827
def describe_execute_handles_basic_execution_tasks():
@@ -567,6 +566,56 @@ async def asyncReturnErrorWithExtensions(self, _info):
567566
],
568567
)
569568

569+
def handles_sync_errors_combined_with_async_ones():
570+
is_async_resolver_finished = False
571+
572+
async def async_resolver(_obj, _info):
573+
nonlocal is_async_resolver_finished
574+
sleep = asyncio.sleep
575+
sleep(0)
576+
sleep(0)
577+
sleep(0)
578+
is_async_resolver_finished = True
579+
580+
schema = GraphQLSchema(
581+
GraphQLObjectType(
582+
"Query",
583+
{
584+
"syncNullError": GraphQLField(
585+
GraphQLNonNull(GraphQLString), resolve=lambda _obj, _info: None
586+
),
587+
"asyncNullError": GraphQLField(
588+
GraphQLNonNull(GraphQLString), resolve=async_resolver
589+
),
590+
},
591+
)
592+
)
593+
594+
document = parse(
595+
"""
596+
{
597+
asyncNullError
598+
syncNullError
599+
}
600+
"""
601+
)
602+
603+
result = execute(schema, document)
604+
605+
assert is_async_resolver_finished is False
606+
607+
assert result == (
608+
None,
609+
[
610+
{
611+
"message": "Cannot return null"
612+
" for non-nullable field Query.syncNullError.",
613+
"locations": [(4, 15)],
614+
"path": ["syncNullError"],
615+
}
616+
],
617+
)
618+
570619
def full_response_path_is_included_for_non_nullable_fields():
571620
def resolve_ok(*_args):
572621
return {}

0 commit comments

Comments
 (0)