Skip to content

Define protocols for connection type overrides #33

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ packages = [
[tool.poetry.dependencies]
python = "^3.6"
graphql-core = "^3.0"
typing-extensions = { version = "^3.6.2", python = "<3.8" }

[tool.poetry.dev-dependencies]
pytest = "^5.3"
Expand Down
5 changes: 4 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: Implementation :: PyPy",
],
install_requires=["graphql-core>=3.0.0"],
install_requires=[
"graphql-core>=3.0.0",
"typing-extensions>=3.6.2,<4; python_version < '3.8'",
],
python_requires=">=3.6,<4",
packages=find_packages("src"),
package_dir={"": "src"},
Expand Down
36 changes: 20 additions & 16 deletions src/graphql_relay/connection/arrayconnection.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@
from .connectiontypes import (
Connection,
ConnectionArguments,
ConnectionConstructor,
ConnectionCursor,
ConnectionType,
Edge,
EdgeConstructor,
PageInfo,
PageInfoConstructor,
)

__all__ = [
Expand All @@ -24,10 +28,10 @@
def connection_from_array(
data: Sequence,
args: ConnectionArguments = None,
connection_type: Any = Connection,
edge_type: Any = Edge,
page_info_type: Any = PageInfo,
) -> Connection:
connection_type: ConnectionConstructor = Connection,
edge_type: EdgeConstructor = Edge,
page_info_type: PageInfoConstructor = PageInfo,
) -> ConnectionType:
"""Create a connection object from a sequence of objects.

Note that different from its JavaScript counterpart which expects an array,
Expand All @@ -54,10 +58,10 @@ def connection_from_array(
def connection_from_list(
data: Sequence,
args: ConnectionArguments = None,
connection_type: Any = Connection,
edge_type: Any = Edge,
pageinfo_type: Any = PageInfo,
) -> Connection:
connection_type: ConnectionConstructor = Connection,
edge_type: EdgeConstructor = Edge,
pageinfo_type: PageInfoConstructor = PageInfo,
) -> ConnectionType:
"""Deprecated alias for connection_from_array.

We're now using the JavaScript terminology in Python as well, since list
Expand All @@ -84,10 +88,10 @@ def connection_from_array_slice(
slice_start: int = 0,
array_length: int = None,
array_slice_length: int = None,
connection_type: Any = Connection,
edge_type: Any = Edge,
page_info_type: Any = PageInfo,
) -> Connection:
connection_type: ConnectionConstructor = Connection,
edge_type: EdgeConstructor = Edge,
page_info_type: PageInfoConstructor = PageInfo,
) -> ConnectionType:
"""Create a connection object from a slice of the result set.

Note that different from its JavaScript counterpart which expects an array,
Expand Down Expand Up @@ -162,13 +166,13 @@ def connection_from_array_slice(
def connection_from_list_slice(
list_slice: Sequence,
args: ConnectionArguments = None,
connection_type: Any = Connection,
edge_type: Any = Edge,
pageinfo_type: Any = PageInfo,
connection_type: ConnectionConstructor = Connection,
edge_type: EdgeConstructor = Edge,
pageinfo_type: PageInfoConstructor = PageInfo,
slice_start=0,
list_length=0,
list_slice_length=None,
) -> Connection:
) -> ConnectionType:
"""Deprecated alias for connection_from_array_slice.

We're now using the JavaScript terminology in Python as well, since list
Expand Down
50 changes: 50 additions & 0 deletions src/graphql_relay/connection/connectiontypes.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
from typing import Any, Dict, List, NamedTuple, Optional
try:
from typing import Protocol
except ImportError:
from typing_extensions import Protocol # type: ignore

__all__ = ["Connection", "ConnectionArguments", "ConnectionCursor", "Edge", "PageInfo"]

Expand All @@ -7,6 +11,25 @@
ConnectionCursor = str


class PageInfoType(Protocol):
@property
def startCursor(self) -> Optional[ConnectionCursor]: ...
def endCursor(self) -> Optional[ConnectionCursor]: ...
def hasPreviousPage(self) -> Optional[bool]: ...
def hasNextPage(self) -> Optional[bool]: ...


class PageInfoConstructor(Protocol):
def __call__(
self,
*,
startCursor: Optional[ConnectionCursor],
endCursor: Optional[ConnectionCursor],
hasPreviousPage: Optional[bool],
hasNextPage: Optional[bool],
) -> PageInfoType: ...


class PageInfo(NamedTuple):
"""A type designed to be exposed as `PageInfo` over GraphQL."""

Expand All @@ -16,13 +39,40 @@ class PageInfo(NamedTuple):
hasNextPage: Optional[bool]


class EdgeType(Protocol):
@property
def node(self) -> Any: ...
@property
def cursor(self) -> ConnectionCursor: ...


class EdgeConstructor(Protocol):
def __call__(self, *, node: Any, cursor: ConnectionCursor) -> EdgeType: ...


class Edge(NamedTuple):
"""A type designed to be exposed as a `Edge` over GraphQL."""

node: Any
cursor: ConnectionCursor


class ConnectionType(Protocol):
@property
def edges(self): List[EdgeType]: ...
@property
def pageInfo(self): PageInfoType: ...


class ConnectionConstructor(Protocol):
def __call__(
self,
*,
edges: List[EdgeType],
pageInfo: PageInfoType,
) -> ConnectionType: ...


class Connection(NamedTuple):
"""A type designed to be exposed as a `Connection` over GraphQL."""

Expand Down