Sqlalchemy graphene deeply nested schemas

456 Views Asked by At

i.m new to graphene

I'm triyng to recieve information from 3 models: two models connect (User and Classes) has many-to-many relationships, one model (Lessons) connect as many-to-one with Classes model.

my goal is to have possibility to show information about Class model as a list inside Class after User data. But i only could show data beetwen User and Classes. But needed to list all data User -> Classes -> Lessons. How it cold be done?

here what it must look

my models are:

table = Table('table', Base.metadata,
    Column('left', Integer, ForeignKey('left.id')),
    Column('right', Integer, ForeignKey('right.id'))
)

class User(Base):
    __tablename__ = 'right'
    id = Column(Integer, primary_key=True)
    name = Column(String(255))
    lessons = relationship("Class",
                    secondary=table,
                    backref="user")

class Classes(Base):
    __tablename__ = 'left'
    id = Column(Integer, primary_key=True)
    name = Column(String(255))
    lessons = relationship('User', backref='classes')



class Lessons(Base):
    __tablename__ = 'lessons'
    id = Column(Integer, primary_key=True)
    name = Column(String(255))
    class_id= Column(Integer, ForeignKey('class.id'))

My schemas and query are:

class UserSchema(SQLAlchemyObjectType):
    class Meta:
        model = User
        interfaces = (relay.Node, )


class ClassesSchema(SQLAlchemyObjectType):
    class Meta:
        model = Contract
        interfaces = (relay.Node, )

class LessonsSchema(SQLAlchemyObjectType):
    class Meta:
        model = Plan
        interfaces = (relay.Node,)


class Query(graphene.ObjectType):
    node = relay.Node.Field()
    # Allows sorting over multiple columns, by default over the primary key
    all = SQLAlchemyConnectionField(UserSchema)
    # Disable sorting over this field
    all_classes = SQLAlchemyConnectionField(ClassesSchema)

    all_lessons = SQLAlchemyConnectionField(LessonsSchema)

schema = graphene.Schema(query=Query)

the output mow is:

{
  "data": {
    "all": {
      "edges": [
        {
          "node": {
            "id": "TWFpbmNoZW1hOjE=",
            "name": "User 1",
            "Classes": {
              "edges": [
                {
                  "node": {
                    "id": "Q29udHJhY3RTY2hlbWE6Mg==",
                    "name": "First class",
                    "lessons": {
                      "edges": []
                    }
                  }
                }
              ]
            }
          }
        },

Main problem is that Lessons list is empty, but if i query Lessons it show information, and it is not empty.

0

There are 0 best solutions below