Skip to content

Commit 7a09d29

Browse files
author
Mark Edwards
committed
Define protocols for connection type overrides
1 parent 4c29756 commit 7a09d29

File tree

2 files changed

+65
-17
lines changed

2 files changed

+65
-17
lines changed

Diff for: pyproject.toml

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ packages = [
3131
[tool.poetry.dependencies]
3232
python = "^3.6"
3333
graphql-core = "^3.0"
34+
typing-extensions = "^3.6.2"
3435

3536
[tool.poetry.dev-dependencies]
3637
pytest = "^5.3"

Diff for: src/graphql_relay/connection/arrayconnection.py

+64-17
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import binascii
22
import warnings
3-
from typing import Any, Optional, Sequence
3+
from typing import Any, List, Optional, Sequence
4+
from typing_extensions import Protocol
45

56
from ..utils.base64 import base64, unbase64
67
from .connectiontypes import (
@@ -21,13 +22,59 @@
2122
]
2223

2324

25+
class EdgeType(Protocol):
26+
@property
27+
def node(self) -> Any: ...
28+
@property
29+
def cursor(self) -> ConnectionCursor: ...
30+
31+
32+
class EdgeConstructor(Protocol):
33+
def __call__(self, *, node: Any, cursor: ConnectionCursor) -> EdgeType: ...
34+
35+
36+
class PageInfoType(Protocol):
37+
@property
38+
def startCursor(self) -> Optional[ConnectionCursor]: ...
39+
def endCursor(self) -> Optional[ConnectionCursor]: ...
40+
def hasPreviousPage(self) -> Optional[bool]: ...
41+
def hasNextPage(self) -> Optional[bool]: ...
42+
43+
44+
class PageInfoConstructor(Protocol):
45+
def __call__(
46+
self,
47+
*,
48+
startCursor: Optional[ConnectionCursor],
49+
endCursor: Optional[ConnectionCursor],
50+
hasPreviousPage: Optional[bool],
51+
hasNextPage: Optional[bool],
52+
) -> PageInfoType: ...
53+
54+
55+
class ConnectionType(Protocol):
56+
@property
57+
def edges(self): List[EdgeType]: ...
58+
@property
59+
def pageInfo(self): PageInfoType: ...
60+
61+
62+
class ConnectionConstructor(Protocol):
63+
def __call__(
64+
self,
65+
*,
66+
edges: List[EdgeType],
67+
pageInfo: PageInfoType,
68+
) -> ConnectionType: ...
69+
70+
2471
def connection_from_array(
2572
data: Sequence,
2673
args: ConnectionArguments = None,
27-
connection_type: Any = Connection,
28-
edge_type: Any = Edge,
29-
page_info_type: Any = PageInfo,
30-
) -> Connection:
74+
connection_type: ConnectionConstructor = Connection,
75+
edge_type: EdgeConstructor = Edge,
76+
page_info_type: PageInfoConstructor = PageInfo,
77+
) -> ConnectionType:
3178
"""Create a connection object from a sequence of objects.
3279
3380
Note that different from its JavaScript counterpart which expects an array,
@@ -54,10 +101,10 @@ def connection_from_array(
54101
def connection_from_list(
55102
data: Sequence,
56103
args: ConnectionArguments = None,
57-
connection_type: Any = Connection,
58-
edge_type: Any = Edge,
59-
pageinfo_type: Any = PageInfo,
60-
) -> Connection:
104+
connection_type: ConnectionConstructor = Connection,
105+
edge_type: EdgeConstructor = Edge,
106+
pageinfo_type: PageInfoConstructor = PageInfo,
107+
) -> ConnectionType:
61108
"""Deprecated alias for connection_from_array.
62109
63110
We're now using the JavaScript terminology in Python as well, since list
@@ -84,10 +131,10 @@ def connection_from_array_slice(
84131
slice_start: int = 0,
85132
array_length: int = None,
86133
array_slice_length: int = None,
87-
connection_type: Any = Connection,
88-
edge_type: Any = Edge,
89-
page_info_type: Any = PageInfo,
90-
) -> Connection:
134+
connection_type: ConnectionConstructor = Connection,
135+
edge_type: EdgeConstructor = Edge,
136+
page_info_type: PageInfoConstructor = PageInfo,
137+
) -> ConnectionType:
91138
"""Create a connection object from a slice of the result set.
92139
93140
Note that different from its JavaScript counterpart which expects an array,
@@ -162,13 +209,13 @@ def connection_from_array_slice(
162209
def connection_from_list_slice(
163210
list_slice: Sequence,
164211
args: ConnectionArguments = None,
165-
connection_type: Any = Connection,
166-
edge_type: Any = Edge,
167-
pageinfo_type: Any = PageInfo,
212+
connection_type: ConnectionConstructor = Connection,
213+
edge_type: EdgeConstructor = Edge,
214+
pageinfo_type: PageInfoConstructor = PageInfo,
168215
slice_start=0,
169216
list_length=0,
170217
list_slice_length=None,
171-
) -> Connection:
218+
) -> ConnectionType:
172219
"""Deprecated alias for connection_from_array_slice.
173220
174221
We're now using the JavaScript terminology in Python as well, since list

0 commit comments

Comments
 (0)