Skip to content
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

fix: keep converting tuples to strings for composite primary keys in relay ID field #399

Merged
merged 1 commit into from
Dec 4, 2023
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
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