Skip to content

Implement Standalone Convert; Enum Handling; User Edge/Connection Classes; Column Alias Handling #25

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

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
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
11 changes: 11 additions & 0 deletions src/strawberry_sqlalchemy_mapper/mapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@ def __init__(
extra_sqlalchemy_type_to_strawberry_type_map: Optional[
Mapping[Type[TypeEngine], Type[Any]]
] = None,
edge_type: Optional[Type] = None,
connection_type: Optional[Type] = None,
) -> None:
if model_to_type_name is None:
model_to_type_name = self._default_model_to_type_name
Expand All @@ -181,6 +183,9 @@ def __init__(
self._related_type_models = set()
self._related_interface_models = set()

self.edge_type = edge_type
self.connection_type = connection_type

@staticmethod
def _default_model_to_type_name(model: Type[BaseModelType]) -> str:
return model.__name__
Expand Down Expand Up @@ -220,6 +225,8 @@ def _edge_type_for(self, type_name: str) -> Type[Any]:
Get or create a corresponding Edge model for the given type
(to support future pagination)
"""
if self.edge_type is not None:
return self.edge_type
edge_name = f"{type_name}Edge"
if edge_name not in self.edge_types:
self.edge_types[edge_name] = edge_type = strawberry.type(
Expand All @@ -238,6 +245,8 @@ def _connection_type_for(self, type_name: str) -> Type[Any]:
Get or create a corresponding Connection model for the given type
(to support future pagination)
"""
if self.connection_type is not None:
return self.connection_type[ForwardRef(type_name)]
connection_name = f"{type_name}Connection"
if connection_name not in self.connection_types:
self.connection_types[connection_name] = connection_type = strawberry.type(
Expand Down Expand Up @@ -269,6 +278,8 @@ def _convert_column_to_strawberry_type(
"""
if isinstance(column.type, Enum):
type_annotation = column.type.python_type
if not hasattr(column.type, "_enum_definition"):
type_annotation = strawberry.enum(type_annotation)
elif isinstance(column.type, ARRAY):
item_type = self._convert_column_to_strawberry_type(
Column(column.type.item_type, nullable=False)
Expand Down