strawberry-sqlalchemy icon indicating copy to clipboard operation
strawberry-sqlalchemy copied to clipboard

input type and optional fields

Open omerkawaz opened this issue 11 months ago • 0 comments

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

Upvote & Fund

  • We're using Polar.sh so you can upvote and help fund this issue.
  • We receive the funding once the issue is completed & confirmed by you.
  • Thank you in advance for helping prioritize & fund our backlog.
Fund with Polar

omerkawaz avatar Mar 03 '24 10:03 omerkawaz