graphql-python / graphene-sqlalchemy

Graphene SQLAlchemy integration
http://docs.graphene-python.org/projects/sqlalchemy/en/latest/
MIT License
975 stars 225 forks source link

using interface with polymorphic models #313

Closed kimutaiRop closed 1 year ago

kimutaiRop commented 2 years ago

I have some polymorphic models


class A(Base):
    name = Column()

class B(A):
    for_b = Column()

class C(A):
    for_c = Column()

I then made graphql object for them like this

class Ainterface(Interface):
    name = String()

    class Meta:
        interfaces = (Node,)

class AType(SQLAlchemyObjectType):
    class Meta:
        interfaces = (Ainterface)
        model = A

class BType(SQLAlchemyObjectType):
    class Meta:
        interfaces = (Ainterface)
        model = B

class CType(SQLAlchemyObjectType):
    class Meta:
        interfaces = (Ainterface)
        model = C

class AllObjects(Union):

    @classmethod
    def resolve_type(cls, instance, info):
        if isinstance(instance, A):
            return AType
        if isinstance(instance, B):
            return BType
        if isinstance(instance, C):
            return CType
    class Meta:
       types = (AType, BType,CType)

what hurts is the fact that even though name is shared in all the children of class A i cannot do something like this

{
    objects: {
            name
        ... on BType {
            for_b
        }
        ... on CType {
            for_c
        }
    }
}

to query the data, is there a way that I can work this to be able to query

erikwrede commented 1 year ago

This was implemented in #365 thanks to @polgfred! Please check out the new documentation 🙂

github-actions[bot] commented 1 year ago

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related topics referencing this issue.