Graphene and SQLAlchemy: derivate from a base object type

264 Views Asked by At

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?

1

There are 1 best solutions below

1
On BEST ANSWER

Please read Composing Mapped Hierarchies with Mixins which seems to describe exactly the scenario and solution to what you are looking for.

However, from the fact that you have foreign keys to user.id from other two tables hints me an inheritance which can be implemented using Mapping Class Inheritance Hierarchies, whereas the strategy will depend on how your actual tables on the database level are designed.