art1415926535 / graphene-sqlalchemy-filter

Filters for Graphene SQLAlchemy integration
https://pypi.org/project/graphene-sqlalchemy-filter/
MIT License
118 stars 34 forks source link

Nested fields filters on self referenced entities causing an SQLAlchemy error #11

Closed alonroth closed 4 years ago

alonroth commented 4 years ago

First of all thanks for the awesome library! :)

I have a system with this sort of hierarchy:

Chapter.py

class Chapter(db.Model):
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    name = db.Column(db.String, nullable=False)
    chapter_id = db.Column(ForeignKey('go_tab_chapters.id'))
    sub_chapters = relationship('Chapter', backref=backref('chapter', remote_side=[id]),
                                primaryjoin=id == chapter_id)
    checklists = relationship(Checklist, backref=backref('chapter'), uselist=True)

Checklist.py

class Checklist(db.Model):
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    name = db.Column(db.String, nullable=False)
    chapter_id = db.Column(ForeignKey('go_tab_chapters.id'))

This is my GraphQL entities:

class ChapterFilter(FilterSet):
    class Meta:
        model = Chapter
        fields = {'name': [...]}

class ChecklistFilter(FilterSet):
    class Meta:
        model = Checklist
        fields = {
            'name': [...]
        }

class CustomField(FilterableConnectionField):
    filters = {
        Chapter: ChapterFilter(),
        Checklist: ChecklistFilter()
    }

class ChapterNode(SQLAlchemyObjectType):
    class Meta:
        model = Chapter
        interfaces = (graphene.relay.Node,)
        connection_field_factory = CustomField.factory

class ChapterConnection(Connection):
    class Meta:
        node = ChapterNode

class ChecklistNode(SQLAlchemyObjectType):
    class Meta:
        model = Checklist
        interfaces = (graphene.relay.Node,)
        connection_field_factory = CustomField.factory

class ChecklistConnection(Connection):
    class Meta:
        node = ChecklistNode

When I'm trying to run the following query:

{
  chapters (filters:{
    name: "abc"
  }){
    edges{
      node{
        name
        checklists (filters:{
          name: "abc"
        }){
          edges{
            node{
              name
            }
          }
        }
        subChapters{
          edges{
            node{
              name
            }
          }
        }
      }
    }
  }
}

I get the following error: graphql.error.located_error.GraphQLLocatedError: Can't construct a join from Mapper|Chapter|chapters to Mapper|Chapter|chapters, they are the same entity

When I comment the following line the query works but not the nested filter:

class ChapterNode(SQLAlchemyObjectType):
    class Meta:
        model = Chapter
        interfaces = (graphene.relay.Node,)
        # connection_field_factory = CustomField.factory

@art1415926535 , Can you please help?

Many thanks!

art1415926535 commented 4 years ago

Fixed in version 1.10.2.