strawberry-graphql / strawberry-sqlalchemy

A SQLAlchemy Integration for strawberry-graphql
MIT License
93 stars 27 forks source link

Unsupported column type: `JSON` #22

Closed Bingdom closed 1 year ago

Bingdom commented 1 year ago

Describe the Bug

Using the JSON type as a column in sqlalchemy shows as not supported for strawberry sqlalchemy mapper.

2023-08-08 12:39:13   File "/home/pi/.local/lib/python3.10/site-packages/strawberry_sqlalchemy_mapper/mapper.py", line 584, in convert
2023-08-08 12:39:13     self._handle_columns(mapper, type_, excluded_keys, generated_field_keys)
2023-08-08 12:39:13   File "/home/pi/.local/lib/python3.10/site-packages/strawberry_sqlalchemy_mapper/mapper.py", line 528, in _handle_columns
2023-08-08 12:39:13     type_annotation = self._convert_column_to_strawberry_type(column)
2023-08-08 12:39:13   File "/home/pi/.local/lib/python3.10/site-packages/strawberry_sqlalchemy_mapper/mapper.py", line 279, in _convert_column_to_strawberry_type
2023-08-08 12:39:13     raise UnsupportedColumnType(column.key, column.type)
2023-08-08 12:39:13 strawberry_sqlalchemy_mapper.exc.UnsupportedColumnType: Unsupported column type: `JSON` on column: `readingStart`. Possible fix: exclude this column

However, strawberry does already have a built-in JSON type.

from strawberry.scalars import JSON

And using this works perfectly fine when creating the type manually.

Example code:

from sqlalchemy import Column, JSON, Text
from sqlalchemy.ext.asyncio import AsyncAttrs
from sqlalchemy.orm import DeclarativeBase
from uuid import uuid4

class Base(AsyncAttrs, DeclarativeBase):
    pass

class Test(Base):
    __tablename__ = "test"
    id = Column(Text, primary_key=True)
    json = Column(JSON)

from strawberry_sqlalchemy_mapper import StrawberrySQLAlchemyMapper
_strawberryMapper = StrawberrySQLAlchemyMapper()

@_strawberryMapper.type(Test)
class StrawberryTest:
    pass

_strawberryMapper.finalize()

Upvote & Fund

Fund with Polar

wisdomG commented 1 year ago

Meet the same issue

wisdomG commented 1 year ago

Can customize extra_sqlalchemy_type_to_strawberry_type_map when initialize StrawberrySQLAlchemyMapper to solve this issue. Works well for my case.

from strawberry.scalars import JSON as StrawberryJSON
from sqlalchemy import JSON as SQLAlchemyJSON
_strawberryMapper = StrawberrySQLAlchemyMapper(
extra_sqlalchemy_type_to_strawberry_type_map={
        SQLAlchemyJSON: StrawberryJSON,
    }
)
Ckk3 commented 1 year ago

Hello, I've just submitted a Pull Request to enable native JSON conversion. I'm unsure if this is an actual issue or if the library is intentionally designed to not support scalar types, but I went ahead and made the changes anyway.