When combining the @strawberry.input and @strawberry_sqlalchemy_mapper.type decorators i was able to declare a valid strawberry input type.
However, i came into a problem trying to define which fields (generated by strawberry_sqlalchemy_mapper.type from the model) are optional (without overriding their previous strawberry_sqlalchemy_mapper declaration).
Could be useful to make a strawberry_sqlalchemy_mapper.input decorator that implements this (perhaps by checking 'SQLAlchemy.Column.nullable' property, or simply by listing them) and wraps both @strawberry.input and @strawberry_sqlalchemy_mapper.type.
EDIT
After examining the source code i see the mapper does take optional into account, it just does not reflect that when working with the @strawberry.input decorator (raises __init__() missing # required keyword-only argument(s)
)
possible use:
# models.py
from sqlalchemy import Column, Integer, String
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
class User(Base):
__tablename__ = "user"
username= Column(String, primary_key=True)
password_hash = Column(String, nullable=False)
first_name = Column(String, nullable=False)
last_name = Column(String, nullable=True)
# elsewhere
# ...
from strawberry_sqlalchemy_mapper import StrawberrySQLAlchemyMapper
strawberry_sqlalchemy_mapper = StrawberrySQLAlchemyMapper()
@strawberry_sqlalchemy_mapper.input(models.User)
class UserInput:
__exclude__ = ["password_hash"]
__optional__ = ["last_name"] # or read nullable attribute
password : str
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too