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

Fail to build the schema when using joined tables inheritance

Open pierregm opened this issue 5 years ago • 1 comments

Hello, I have a rather complex SQLAlchemy schema, where some models inherit from others through the joined table inheritance scheme and where some tables have relationships to some inherited class tables. The build_schema function fails to return a GraphQL schema, usually with a KeyError.

It looks like the base.__subclasses__ function (called in schema.build_schema) returns only the parent classes, and not the inherited classes.

You may want to try something like that instead

    for model in base._decl_class_registry.values():
        try:
            table = model.__tablename__
        except AttributeError:
            continue
        build_queries(model, objects, queries, inputs)
        build_mutations(model, objects, mutations, inputs)

pierregm avatar Oct 02 '20 12:10 pierregm

@pierregm can you provide a schema that reproduces the error you're seeing?

I tried with the following schema provided in the SQLAlchemy documentation on this use case and I wasn't able to reproduce the error (though I can see that the inherited classes are not part of the resulting GraphQL schema):

class Company(Base):
    __tablename__ = 'company'
    id = Column(Integer, primary_key=True)
    name = Column(String(50))
    managers = relationship("Manager", back_populates="company")

class Employee(Base):
    __tablename__ = 'employee'
    id = Column(Integer, primary_key=True)
    name = Column(String(50))
    type = Column(String(50))

    __mapper_args__ = {
        'polymorphic_identity':'employee',
        'polymorphic_on':type
    }


class Manager(Employee):
    manager_name = Column(String(30))

    company_id = Column(ForeignKey('company.id'))
    company = relationship("Company", back_populates="managers")

    __mapper_args__ = {
        'polymorphic_identity':'manager',
    }


class Engineer(Employee):
    engineer_info = Column(String(50))

    __mapper_args__ = {
        'polymorphic_identity':'engineer'
    }

gzzo avatar Apr 09 '21 22:04 gzzo