graphql-python / graphene-sqlalchemy

Graphene SQLAlchemy integration
http://docs.graphene-python.org/projects/sqlalchemy/en/latest/
MIT License
975 stars 225 forks source link

No UUIDType support #336

Closed calibodhi closed 1 year ago

calibodhi commented 2 years ago

Almost identical to #257 Opening a new issue since I could not find a solution. Any help is much appreciated.


from sqlalchemy_utils import UUIDType

class Item(Base):
    id = Column(binary=False, native=True), primary_key=True, default=uuid4)

from graphene_sqlalchemy import SQLAlchemyObjectType

class ItemNode(SQLAlchemyObjectType):
    class Meta:
        model = Item
Exception: Don't know how to convert the SQLAlchemy field item.id (<class 'sqlalchemy.sql.schema.Column'>)

How can I support UUIDs?

erikwrede commented 2 years ago

It would be interesting to see if this works with the native Postgres.UUID instead of sqlalchemy_utils.UUID, since internal converters exist for that Type (see converters.py line 188). On the other hand, the work-around in #257 should definitely working for IDs then... I'll try to reproduce this once I find the time.

For now, it would be helpful if you could post a traceback of the error for further analysis.

calibodhi commented 2 years ago

@erikwrede thanks! It works with the postgres UUID on a postgres db. I would love to get this working with sqlite3. I will open a separate issue for that, if needed, and change the name back on this since I referenced the other issues.

from uuid import uuid4
from sqlalchemy.dialects.postgresql import UUID

class Item(Base):
   id = Column(UUID(as_uuid=True), primary_key=True, default=uuid4)

from graphene_sqlalchemy import SQLAlchemyObjectType

class ItemNode(SQLAlchemyObjectType):
    class Meta:
        model = Item
erikwrede commented 2 years ago

Sorry for the late reply. I haven't found the time to do any testing earlier. Since you referenced #257, I expected that you already tried adding a custom type converter for UUIDType. This is necessary because SQLAlchemy does not natively support sqlalchemy_utils.UUIDType and does not know how to convert this time into a Graphene-Type for the GraphQL-Schema. You are seeing the error Message Exception: Don't know how to convert the SQLAlchemy field item.id (<class 'sqlalchemy.sql.schema.Column'>) because of that.

Adding a custom type converter is as simple as adding these lines to your code before the model:

from sqlalchemy_utils import UUIDType
from graphene_sqlalchemy.converter import convert_sqlalchemy_type
@convert_sqlalchemy_type.register(UUIDType)
def convert_column_to_string(type, column, registry=None):
    return graphene.String

This is essentially the solution detailed in #257. I've just tested this with your code above on graphene-sqlalchemy==3.0.0b1 and verified that it works! I could not reproduce any of the other problems mentioned in #257. Try it out and let me know if it worked!

Quick side note: To prevent this from happening in most of the cases, I want to add additional type converters to graphene-sqlalchemy 3.0. Tracking in #339.

github-actions[bot] commented 1 year ago

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related topics referencing this issue.