Skip to content

Commit c11580c

Browse files
committed
connectiontypes: inline types into connection.py
Replicates graphql/graphql-relay-js@fc62ade
1 parent db2277d commit c11580c

File tree

4 files changed

+119
-121
lines changed

4 files changed

+119
-121
lines changed

src/graphql_relay/__init__.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,18 @@
33
# The graphql-relay and graphql-relay-js version info
44
from .version import version, version_info, version_js, version_info_js
55

6-
# Types for creating connection types in the schema
7-
from .connection.connectiontypes import (
8-
Connection,
9-
ConnectionArguments,
10-
ConnectionCursor,
11-
Edge,
12-
PageInfo,
13-
)
14-
15-
# Helpers for creating connection types in the schema
6+
# Types and helpers for creating connection types in the schema
167
from .connection.connection import (
178
backward_connection_args,
189
connection_args,
1910
connection_definitions,
2011
forward_connection_args,
12+
Connection,
13+
ConnectionArguments,
14+
ConnectionCursor,
15+
Edge,
2116
GraphQLConnectionDefinitions,
17+
PageInfo,
2218
)
2319

2420
# Helpers for creating connections from arrays

src/graphql_relay/connection/arrayconnection.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from typing_extensions import Protocol # type: ignore
99

1010
from ..utils.base64 import base64, unbase64
11-
from .connectiontypes import (
11+
from .connection import (
1212
Connection,
1313
ConnectionArguments,
1414
ConnectionConstructor,

src/graphql_relay/connection/connection.py

Lines changed: 112 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Any, NamedTuple
1+
from typing import Any, Dict, List, NamedTuple, Optional
22

33
from graphql.type import (
44
GraphQLArgument,
@@ -15,12 +15,22 @@
1515
Thunk,
1616
)
1717

18+
try:
19+
from typing import Protocol
20+
except ImportError: # Python < 3.8
21+
from typing_extensions import Protocol # type: ignore
22+
1823
__all__ = [
1924
"connection_definitions",
2025
"forward_connection_args",
2126
"backward_connection_args",
2227
"connection_args",
28+
"Connection",
29+
"ConnectionArguments",
30+
"ConnectionCursor",
31+
"Edge",
2332
"GraphQLConnectionDefinitions",
33+
"PageInfo",
2434
]
2535

2636

@@ -52,6 +62,22 @@ def resolve_maybe_thunk(thing_or_thunk: Thunk) -> Any:
5262
return thing_or_thunk() if callable(thing_or_thunk) else thing_or_thunk
5363

5464

65+
"""A type alias for cursors in this implementation."""
66+
ConnectionCursor = str
67+
68+
69+
"""A type describing the arguments a connection field receives in GraphQL.
70+
71+
The following kinds of arguments are expected (all optional):
72+
73+
before: ConnectionCursor
74+
after: ConnectionCursor
75+
first: int
76+
last: int
77+
"""
78+
ConnectionArguments = Dict[str, Any]
79+
80+
5581
def connection_definitions(
5682
node_type: GraphQLObjectType,
5783
name: str = None,
@@ -104,6 +130,91 @@ def connection_definitions(
104130
return GraphQLConnectionDefinitions(edge_type, connection_type)
105131

106132

133+
class PageInfoType(Protocol):
134+
@property
135+
def startCursor(self) -> Optional[ConnectionCursor]:
136+
...
137+
138+
def endCursor(self) -> Optional[ConnectionCursor]:
139+
...
140+
141+
def hasPreviousPage(self) -> bool:
142+
...
143+
144+
def hasNextPage(self) -> bool:
145+
...
146+
147+
148+
class PageInfoConstructor(Protocol):
149+
def __call__(
150+
self,
151+
*,
152+
startCursor: Optional[ConnectionCursor],
153+
endCursor: Optional[ConnectionCursor],
154+
hasPreviousPage: bool,
155+
hasNextPage: bool,
156+
) -> PageInfoType:
157+
...
158+
159+
160+
class PageInfo(NamedTuple):
161+
"""A type designed to be exposed as `PageInfo` over GraphQL."""
162+
163+
startCursor: Optional[ConnectionCursor]
164+
endCursor: Optional[ConnectionCursor]
165+
hasPreviousPage: bool
166+
hasNextPage: bool
167+
168+
169+
class EdgeType(Protocol):
170+
@property
171+
def node(self) -> Any:
172+
...
173+
174+
@property
175+
def cursor(self) -> ConnectionCursor:
176+
...
177+
178+
179+
class EdgeConstructor(Protocol):
180+
def __call__(self, *, node: Any, cursor: ConnectionCursor) -> EdgeType:
181+
...
182+
183+
184+
class Edge(NamedTuple):
185+
"""A type designed to be exposed as a `Edge` over GraphQL."""
186+
187+
node: Any
188+
cursor: ConnectionCursor
189+
190+
191+
class ConnectionType(Protocol):
192+
@property
193+
def edges(self):
194+
List[EdgeType]: ...
195+
196+
@property
197+
def pageInfo(self):
198+
PageInfoType: ...
199+
200+
201+
class ConnectionConstructor(Protocol):
202+
def __call__(
203+
self,
204+
*,
205+
edges: List[EdgeType],
206+
pageInfo: PageInfoType,
207+
) -> ConnectionType:
208+
...
209+
210+
211+
class Connection(NamedTuple):
212+
"""A type designed to be exposed as a `Connection` over GraphQL."""
213+
214+
edges: List[Edge]
215+
pageInfo: PageInfo
216+
217+
107218
# The common page info type used by all connections.
108219
page_info_type = GraphQLObjectType(
109220
"PageInfo",

src/graphql_relay/connection/connectiontypes.py

Lines changed: 0 additions & 109 deletions
This file was deleted.

0 commit comments

Comments
 (0)