Tuesday, 20 April 2021

Graphene and SQLAlchemy: derivate from a base object type

I am building an app using Flask, Graphene, SQLAlchemy and Graphene-SQLAlchemy in which I implemented the following models:

class User(db.Model):
    __tablename__ = "user"
    id = Column(Integer, primary_key=True)
    name = Column(Text, nullable=False)
    email = Column(Text, nullable=False)

class BusinessUser(db.Model):
    __tablename__ = "bis_user"
    id = Column(Integer, ForeignKey("user.id"), primary_key=True)
    vatNumber = Column(Text, nullable=False)

class LVUser(db.Model):
    __tablename__ = "lv_user"
    id = Column(Integer, ForeignKey("user.id"), primary_key=True)
    lv_num = Column(Integer, nullable=False)

My goal would be to have three GraphQL types defined as such:

type User {
    id : ID
    name : String
    email : String
}

type BusinessUser {
    id : ID
    name : String
    email : String
    vatNumber : String
}

type LVUser {
    id : ID
    name : String
    email : String
    lvNum : Integer
}

But I'm struggling to find an efficient and elegant manner of doing so. Is there any way to have, let's say a "base" object type User and create BusinessUser and LVUser types on the top of my base type whilst providing the extra fields?



from Graphene and SQLAlchemy: derivate from a base object type

No comments:

Post a Comment