Skip to content

Commit

Permalink
fix: keep converting tuples to strings for composite primary keys in …
Browse files Browse the repository at this point in the history
…relay ID field (#399)
  • Loading branch information
erikwrede authored Dec 4, 2023
1 parent c927ada commit ae4f87c
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 1 deletion.
7 changes: 7 additions & 0 deletions graphene_sqlalchemy/tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -436,3 +436,10 @@ class CustomColumnModel(Base):

id = Column(Integer(), primary_key=True)
custom_col = Column(CustomIntegerColumn)


class CompositePrimaryKeyTestModel(Base):
__tablename__ = "compositekeytestmodel"

first_name = Column(String(30), primary_key=True)
last_name = Column(String(30), primary_key=True)
51 changes: 51 additions & 0 deletions graphene_sqlalchemy/tests/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from graphene import (
Boolean,
DefaultGlobalIDType,
Dynamic,
Field,
Float,
Expand Down Expand Up @@ -42,6 +43,7 @@
from .models import (
Article,
CompositeFullName,
CompositePrimaryKeyTestModel,
Employee,
NonAbstractPerson,
Person,
Expand Down Expand Up @@ -513,6 +515,55 @@ async def resolve_reporter(self, _info):
# Test Custom SQLAlchemyObjectType Implementation


@pytest.mark.asyncio
async def test_composite_id_resolver(session):
"""Test that the correct resolver functions are called"""

composite_reporter = CompositePrimaryKeyTestModel(
first_name="graphql", last_name="foundation"
)

session.add(composite_reporter)
await eventually_await_session(session, "commit")

class CompositePrimaryKeyTestModelType(SQLAlchemyObjectType):
class Meta:
model = CompositePrimaryKeyTestModel
interfaces = (Node,)

class Query(ObjectType):
composite_reporter = Field(CompositePrimaryKeyTestModelType)

async def resolve_composite_reporter(self, _info):
session = utils.get_session(_info.context)
if SQL_VERSION_HIGHER_EQUAL_THAN_1_4 and isinstance(session, AsyncSession):
return (
(await session.scalars(select(CompositePrimaryKeyTestModel)))
.unique()
.first()
)
return session.query(CompositePrimaryKeyTestModel).first()

schema = Schema(query=Query)
result = await schema.execute_async(
"""
query {
compositeReporter {
id
firstName
lastName
}
}
""",
context_value={"session": session},
)

assert not result.errors
assert result.data["compositeReporter"]["id"] == DefaultGlobalIDType.to_global_id(
CompositePrimaryKeyTestModelType, str(("graphql", "foundation"))
)


def test_custom_objecttype_registered():
class CustomSQLAlchemyObjectType(SQLAlchemyObjectType):
class Meta:
Expand Down
2 changes: 1 addition & 1 deletion graphene_sqlalchemy/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,7 @@ async def get_result() -> Any:
def resolve_id(self, info):
# graphene_type = info.parent_type.graphene_type
keys = self.__mapper__.primary_key_from_instance(self)
return tuple(keys) if len(keys) > 1 else keys[0]
return str(tuple(keys)) if len(keys) > 1 else keys[0]

@classmethod
def enum_for_field(cls, field_name):
Expand Down

0 comments on commit ae4f87c

Please sign in to comment.