Skip to content

association_proxy support #267

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 14 commits into from
Oct 6, 2023
Merged
Show file tree
Hide file tree
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
Prev Previous commit
Next Next commit
fix: throw error when association proxy could not be converted
Signed-off-by: Erik Wrede <erikwrede2@gmail.com>
  • Loading branch information
erikwrede committed Jan 2, 2023
commit b49c63ee9dbe3d0deaf6186b9fa62da2e1ec3a4c
7 changes: 7 additions & 0 deletions graphene_sqlalchemy/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,13 @@ def dynamic_type():
assoc_prop.value_attr,
**field_kwargs,
).get_type()
else:
raise TypeError(
"Unsupported association proxy target type: {} for prop {} on type {}. "
"Please disable the conversion of this field using an ORMField.".format(
type(attr), assoc_prop, obj_type
)
)
# else, not supported

return graphene.Dynamic(dynamic_type)
Expand Down
12 changes: 12 additions & 0 deletions graphene_sqlalchemy/tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,18 @@ def __repr__(self):
return "{} {}".format(self.first_name, self.last_name)


class ProxiedReporter(Base):
__tablename__ = "reporters_error"
id = Column(Integer(), primary_key=True)
first_name = Column(String(30), doc="First name")
last_name = Column(String(30), doc="Last name")
reporter_id = Column(Integer(), ForeignKey("reporters.id"))
reporter = relationship("Reporter", uselist=False)

# This is a hybrid property, we don't support proxies on hybrids yet
composite_prop = association_proxy("reporter", "composite_prop")


class Reporter(Base):
__tablename__ = "reporters"

Expand Down
20 changes: 20 additions & 0 deletions graphene_sqlalchemy/tests/test_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
Article,
CompositeFullName,
Pet,
ProxiedReporter,
Reporter,
ShoppingCart,
ShoppingCartItem,
Expand Down Expand Up @@ -523,6 +524,25 @@ class Meta:
assert dynamic_field.get_type().type.of_type == ArticleType


def test_should_throw_error_association_proxy_unsupported_target():
class ProxiedReporterType(SQLAlchemyObjectType):
class Meta:
model = ProxiedReporter

field = convert_sqlalchemy_association_proxy(
ProxiedReporter,
ProxiedReporter.composite_prop,
ProxiedReporterType,
get_global_registry(),
default_connection_field_factory,
True,
mock_resolver,
)

with pytest.raises(TypeError):
field.get_type()


def test_should_postgresql_uuid_convert():
assert get_field(postgresql.UUID()).type == graphene.UUID

Expand Down