8000 simplify schema in defer tests · graphql-python/graphql-core@a8cffbf · GitHub
[go: up one dir, main page]

Skip to content

Commit a8cffbf

Browse files
committed
simplify schema in defer tests
Replicates graphql/graphql-js@75114af
1 parent d73b874 commit a8cffbf

File tree

1 file changed

+83
-86
lines changed

1 file changed

+83
-86
lines changed

tests/execution/test_defer.py

Lines changed: 83 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -28,28 +28,29 @@
2828
GraphQLString,
2929
)
3030

31-
32-
def resolve_null_sync(_obj, _info) -> None:
33-
"""A resolver returning a null value synchronously."""
34-
return
35-
36-
37-
async def resolve_null_async(_obj, _info) -> None:
38-
"""A resolver returning a null value asynchronously."""
39-
return
40-
41-
4231
friend_type = GraphQLObjectType(
4332
"Friend",
4433
{
4534
"id": GraphQLField(GraphQLID),
4635
"name": GraphQLField(GraphQLString),
47-
"asyncNonNullErrorField": GraphQLField(
48-
GraphQLNonNull(GraphQLString), resolve=resolve_null_async
49-
),
36+
"nonNullName": GraphQLField(GraphQLNonNull(GraphQLString)),
37+
},
38+
)
39+
40+
hero_type = GraphQLObjectType(
41+
"Hero",
42+
{
43+
"id": GraphQLField(GraphQLID),
44+
"name": GraphQLField(GraphQLString),
45+
"nonNullName": GraphQLField(GraphQLNonNull(GraphQLString)),
46+
"friends": GraphQLField(GraphQLList(friend_type)),
5047
},
5148
)
5249

50+
query = GraphQLObjectType("Query", {"hero": GraphQLField(hero_type)})
51+
52+
schema = GraphQLSchema(query)
53+
5354

5455
class Friend(NamedTuple):
5556
id: int
@@ -58,57 +59,44 @@ class Friend(NamedTuple):
5859

5960
friends = [Friend(2, "Han"), Friend(3, "Leia"), Friend(4, "C-3PO")]
6061

62+
hero = {"id": 1, "name": "Luke", "friends": friends}
6163

62-
async def resolve_slow(_obj, _info) -> str:
63-
"""Simulate a slow async resolver returning a value."""
64-
await sleep(0)
65-
return "slow"
6664

65+
class Resolvers:
66+
"""Various resolver functions for testing."""
6767

68-
async def resolve_bad(_obj, _info) -> str:
69-
"""Simulate a bad async resolver raising an error."""
70-
raise RuntimeError("bad")
68+
@staticmethod
69+
def null(_info) -> None:
70+
"""A resolver returning a null value synchronously."""
71+
return
7172

73+
@staticmethod
74+
async def null_async(_info) -> None:
75+
"""A resolver returning a null value asynchronously."""
76+
return
7277

73-
async def resolve_friends_async(_obj, _info) -> AsyncGenerator[Friend, None]:
74-
"""A slow async generator yielding the first friend."""
75-
await sleep(0)
76-
yield friends[0]
78+
@staticmethod
79+
async def slow(_info) -> str:
80+
"""Simulate a slow async resolver returning a value."""
81+
await sleep(0)
82+
return "slow"
7783

84+
@staticmethod
85+
def bad(_info) -> str:
86+
"""Simulate a bad resolver raising an error."""
87+
raise RuntimeError("bad")
7888

79-
hero_type = GraphQLObjectType(
80-
"Hero",
81-
{
82-
"id": GraphQLField(GraphQLID),
83-
"name": GraphQLField(GraphQLString),
84-
"slowField": GraphQLField(GraphQLString, resolve=resolve_slow),
85-
"errorField": GraphQLField(GraphQLString, resolve=resolve_bad),
86-
"nonNullErrorField": GraphQLField(
87-
GraphQLNonNull(GraphQLString), resolve=resolve_null_sync
88-
),
89-
"asyncNonNullErrorField": GraphQLField(
90-
GraphQLNonNull(GraphQLString), resolve=resolve_null_async
91-
),
92-
"friends": GraphQLField(
93-
GraphQLList(friend_type), resolve=lambda _obj, _info: friends
94-
),
95-
"asyncFriends": GraphQLField(
96-
GraphQLList(friend_type), resolve=resolve_friends_async
97-
),
98-
},
99-
)
100-
101-
hero = Friend(1, "Luke")
102-
103-
query = GraphQLObjectType(
104-
"Query", {"hero": GraphQLField(hero_type, resolve=lambda _obj, _info: hero)}
105-
)
106-
107-
schema = GraphQLSchema(query)
89+
@staticmethod
90+
async def friends(_info) -> AsyncGenerator[Friend, None]:
91+
"""A slow async generator yielding the first friend."""
92+
await sleep(0)
93+
yield friends[0]
10894

10995

11096
async def complete(document: DocumentNode, root_value: Any = None) -> Any:
111-
result = experimental_execute_incrementally(schema, document, root_value)
97+
result = experimental_execute_incrementally(
98+
schema, document, root_value or {"hero": hero}
99+
)
112100
if is_awaitable(result):
113101
result = await result
114102

@@ -485,24 +473,24 @@ async def can_defer_fragments_with_errors_on_the_top_level_query_field():
485473
}
486474
fragment QueryFragment on Query {
487475
hero {
488-
errorField
476+
name
489477
}
490478
}
491479
"""
492480
)
493-
result = await complete(document)
481+
result = await complete(document, {"hero": {**hero, "name": Resolvers.bad}})
494482

495483
assert result == [
496484
{"data": {}, "hasNext": True},
497485
{
498486
"incremental": [
499487
{
500-
"data": {"hero": {"errorField": None}},
488+
"data": {"hero": {"name": None}},
501489
"errors": [
502490
{
503491
"message": "bad",
504492
"locations": [{"column": 17, "line": 7}],
505-
"path": ["hero", "errorField"],
493+
"path": ["hero", "name"],
506494
}
507495
],
508496
"path": [],
@@ -666,24 +654,24 @@ async def handles_errors_thrown_in_deferred_fragments():
666654
}
667655
}
668656
fragment NameFragment on Hero {
669-
errorField
657+
name
670658
}
671659
"""
672660
)
673-
result = await complete(document)
661+
result = await complete(document, {"hero": {**hero, "name": Resolvers.bad}})
674662

675663
assert result == [
676664
{"data": {"hero": {"id": "1"}}, "hasNext": True},
677665
{
678666
"incremental": [
679667
{
680-
"data": {"errorField": None},
668+
"data": {"name": None},
681669
"path": ["hero"],
682670
"errors": [
683671
{
684672
"message": "bad",
685673
"locations": [{"line": 9, "column": 15}],
686-
"path": ["hero", "errorField"],
674+
"path": ["hero", "name"],
687675
}
688676
],
689677
},
@@ -703,11 +691,13 @@ async def handles_non_nullable_errors_thrown_in_deferred_fragments():
703691
}
704692
}
705693
fragment NameFragment on Hero {
706-
nonNullErrorField
694+
nonNullName
707695
}
708696
"""
709697
)
710-
result = await complete(document)
698+
result = await complete(
699+
document, {"hero": {**hero, "nonNullName": Resolvers. F42D null}}
700+
)
711701

712702
assert result == [
713703
{"data": {"hero": {"id": "1"}}, "hasNext": True},
@@ -719,9 +709,9 @@ async def handles_non_nullable_errors_thrown_in_deferred_fragments():
719709
"errors": [
720710
{
721711
"message": "Cannot return null for non-nullable field"
722-
" Hero.nonNullErrorField.",
712+
" Hero.nonNullName.",
723713
"locations": [{"line": 9, "column": 15}],
724-
"path": ["hero", "nonNullErrorField"],
714+
"path": ["hero", "nonNullName"],
725715
}
726716
],
727717
},
@@ -736,7 +726,7 @@ async def handles_non_nullable_errors_thrown_outside_deferred_fragments():
736726
"""
737727
query HeroNameQuery {
738728
hero {
739-
nonNullErrorField
729+
nonNullName
740730
...NameFragment @defer
741731
}
742732
}
@@ -745,16 +735,18 @@ async def handles_non_nullable_errors_thrown_outside_deferred_fragments():
745735
}
746736
"""
747737
)
748-
result = await complete(document)
738+
result = await complete(
739+
document, {"hero": {**hero, "nonNullName": Resolvers.null}}
740+
)
749741

750742
assert result == {
751743
"data": {"hero": None},
752744
"errors": [
753745
{
754746
"message": "Cannot return null for non-nullable field"
755-
" Hero.nonNullErrorField.",
747+
" Hero.nonNullName.",
756748
"locations": [{"line": 4, "column": 17}],
757-
"path": ["hero", "nonNullErrorField"],
749+
"path": ["hero", "nonNullName"],
758750
}
759751
],
760752
}
@@ -770,11 +762,13 @@ async def handles_async_non_nullable_errors_thrown_in_deferred_fragments():
770762
}
771763
}
772764
fragment NameFragment on Hero {
773-
asyncNonNullErrorField
765+
nonNullName
774766
}
775767
"""
776768
)
777-
result = await complete(document)
769+
result = await complete(
770+
document, {"hero": {**hero, "nonNullName": Resolvers.null_async}}
771+
)
778772

779773
assert result == [
780774
{"data": {"hero": {"id": "1"}}, "hasNext": True},
@@ -786,9 +780,9 @@ async def handles_async_non_nullable_errors_thrown_in_deferred_fragments():
786780
"errors": [
787781
{
788782
"message": "Cannot return null for non-nullable field"
789-
" Hero.asyncNonNullErrorField.",
783+
" Hero.nonNullName.",
790784
"locations": [{"line": 9, "column": 15}],
791-
"path": ["hero", "asyncNonNullErrorField"],
785+
"path": ["hero", "nonNullName"],
792786
}
793787
],
794788
},
@@ -808,7 +802,7 @@ async def returns_payloads_in_correct_order():
808802
}
809803
}
810804
fragment NameFragment on Hero {
811-
slowField
805+
name
812806
friends {
813807
...NestedFragment @defer
814808
}
@@ -818,14 +812,14 @@ async def returns_payloads_in_correct_order():
818812
}
819813
"""
820814
)
821-
result = await complete(document)
815+
result = await complete(document, {"hero": {**hero, "name": Resolvers.slow}})
822816

823817
assert result == [
824818
{"data": {"hero": {"id": "1"}}, "hasNext": True},
825819
{
826820
"incremental": [
827821
{
828-
"data": {"slowField": "slow", "friends": [{}, {}, {}]},
822+
"data": {"name": "slow", "friends": [{}, {}, {}]},
829823
"path": ["hero"],
830824
}
831825
],
@@ -909,8 +903,8 @@ async def filters_deferred_payloads_when_list_item_from_async_iterable_nulled():
909903
"""
910904
query {
911905
hero {
912-
asyncFriends {
913-
asyncNonNullErrorField
906+
friends {
907+
nonNullName
914908
...NameFragment @defer
915909
}
916910
}
@@ -921,16 +915,18 @@ async def filters_deferred_payloads_when_list_item_from_async_iterable_nulled():
921915
"""
922916
)
923917

924-
result = await complete(document)
918+
result = await complete(
919+
document, {"hero": {**hero, "friends": Resolvers.friends}}
920+
)
925921

926922
assert result == {
927-
"data": {"hero": {"asyncFriends": [None]}},
923+
"data": {"hero": {"friends": [None]}},
928924
"errors": [
929925
{
930926
"message": "Cannot return null for non-nullable field"
931-
" Friend.asyncNonNullErrorField.",
927+
" Friend.nonNullName.",
932928
"locations": [{"line": 5, "column": 19}],
933-
"path": ["hero", "asyncFriends", 0, "asyncNonNullErrorField"],
929+
"path": ["hero", "friends", 0, "nonNullName"],
934930
}
935931
],
936932
}
@@ -958,14 +954,15 @@ async def original_execute_function_throws_error_if_deferred_and_not_all_is_sync
958954
document = parse(
959955
"""
960956
query Deferred {
961-
hero { slowField }
957+
hero { name }
962958
... @defer { hero { id } }
963959
}
964960
"""
965961
)
966962

963+
root_value = {"hero": {**hero, "name": Resolvers.slow}}
967964
with pytest.raises(GraphQLError) as exc_info:
968-
await execute(schema, document, {}) # type: ignore
965+
await execute(schema, document, root_value) # type: ignore
969966

970967
assert str(exc_info.value) == (
971968
"Executing this GraphQL operation would unexpectedly produce"

0 commit comments

Comments
 (0)
0