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

using interface with polymorphic models

Open kimutaiRop opened this issue 4 years ago • 0 comments

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

kimutaiRop avatar Jul 19 '21 12:07 kimutaiRop