1
1
import sys
2
2
from collections .abc import Iterable
3
- from typing import Any , Optional
3
+ from typing import Annotated , Any , Optional
4
4
from typing_extensions import Self
5
5
6
6
import pytest
7
7
8
8
import strawberry
9
9
from strawberry .permission import BasePermission
10
- from strawberry .relay import Connection , Node
11
- from strawberry .relay .types import ListConnection
10
+ from strawberry .relay import Connection , Node , PageInfo , to_base64
11
+ from strawberry .relay .types import Edge , ListConnection
12
12
from strawberry .schema .config import StrawberryConfig
13
13
14
14
@@ -25,7 +25,7 @@ def resolve_nodes(
25
25
26
26
27
27
@strawberry .type
28
- class UserConnection (Connection [User ]):
28
+ class EmptyUserConnection (Connection [User ]):
29
29
@classmethod
30
30
def resolve_connection (
31
31
cls ,
@@ -42,6 +42,33 @@ def resolve_connection(
42
42
return None
43
43
44
44
45
+ @strawberry .type
46
+ class UserConnection (Connection [User ]):
47
+ @classmethod
48
+ def resolve_connection (
49
+ cls ,
50
+ nodes : Iterable [User ],
51
+ * ,
52
+ info : Any ,
53
+ after : Optional [str ] = None ,
54
+ before : Optional [str ] = None ,
55
+ first : Optional [int ] = None ,
56
+ last : Optional [int ] = None ,
57
+ max_results : Optional [int ] = None ,
58
+ ** kwargs : Any ,
59
+ ) -> Optional [Self ]:
60
+ user_node_id = to_base64 (User , "1" )
61
+ return cls (
62
+ page_info = PageInfo (
63
+ has_next_page = False ,
64
+ has_previous_page = False ,
65
+ start_cursor = None ,
66
+ end_cursor = None ,
67
+ ),
68
+ edges = [Edge (cursor = user_node_id , node = User (id = user_node_id ))],
69
+ )
70
+
71
+
45
72
class TestPermission (BasePermission ):
46
73
message = "Not allowed"
47
74
@@ -52,7 +79,7 @@ def has_permission(self, source, info, **kwargs: Any):
52
79
def test_nullable_connection_with_optional ():
53
80
@strawberry .type
54
81
class Query :
55
- @strawberry .relay .connection (Optional [UserConnection ])
82
+ @strawberry .relay .connection (Optional [EmptyUserConnection ])
56
83
def users (self ) -> Optional [list [User ]]:
57
84
return None
58
85
@@ -74,14 +101,77 @@ def users(self) -> Optional[list[User]]:
74
101
assert not result .errors
75
102
76
103
104
+ def test_lazy_connection ():
105
+ @strawberry .type
106
+ class Query :
107
+ @strawberry .relay .connection (
108
+ Optional [
109
+ Annotated [
110
+ "UserConnection" , strawberry .lazy ("tests.relay.test_connection" )
111
+ ]
112
+ ]
113
+ )
114
+ def users (self ) -> Optional [list [User ]]:
115
+ return None
116
+
117
+ schema = strawberry .Schema (query = Query )
118
+ query = """
119
+ query {
120
+ users {
121
+ edges {
122
+ node {
123
+ name
124
+ }
125
+ }
126
+ }
127
+ }
128
+ """
129
+
130
+ result = schema .execute_sync (query )
131
+ assert result .data == {"users" : {"edges" : [{"node" : {"name" : "John" }}]}}
132
+ assert not result .errors
133
+
134
+
135
+ def test_lazy_optional_connection ():
136
+ @strawberry .type
137
+ class Query :
138
+ @strawberry .relay .connection (
139
+ Optional [
140
+ Annotated [
141
+ "EmptyUserConnection" ,
142
+ strawberry .lazy ("tests.relay.test_connection" ),
143
+ ]
144
+ ]
145
+ )
146
+ def users (self ) -> Optional [list [User ]]:
147
+ return None
148
+
149
+ schema = strawberry .Schema (query = Query )
150
+ query = """
151
+ query {
152
+ users {
153
+ edges {
154
+ node {
155
+ name
156
+ }
157
+ }
158
+ }
159
+ }
160
+ """
161
+
162
+ result = schema .execute_sync (query )
163
+ assert result .data == {"users" : None }
164
+ assert not result .errors
165
+
166
+
77
167
@pytest .mark .skipif (
78
168
sys .version_info < (3 , 10 ),
79
169
reason = "pipe syntax for union is only available on python 3.10+" ,
80
170
)
81
171
def test_nullable_connection_with_pipe ():
82
172
@strawberry .type
83
173
class Query :
84
- @strawberry .relay .connection (UserConnection | None )
174
+ @strawberry .relay .connection (EmptyUserConnection | None )
85
175
def users (self ) -> list [User ] | None :
86
176
return None
87
177
@@ -107,7 +197,7 @@ def test_nullable_connection_with_permission():
107
197
@strawberry .type
108
198
class Query :
109
199
@strawberry .relay .connection (
110
- Optional [UserConnection ], permission_classes = [TestPermission ]
200
+ Optional [EmptyUserConnection ], permission_classes = [TestPermission ]
111
201
)
112
202
def users (self ) -> Optional [list [User ]]: # pragma: no cover
113
203
pytest .fail ("Should not have been called..." )
0 commit comments