Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

graphene-sqlalchemy-filter not working in nested graphQL query. #35

Open
bhavnish07 opened this issue Oct 12, 2020 · 4 comments
Open

graphene-sqlalchemy-filter not working in nested graphQL query. #35

bhavnish07 opened this issue Oct 12, 2020 · 4 comments

Comments

@bhavnish07
Copy link

bhavnish07 commented Oct 12, 2020

I am having issues in implementing graphene-sqlalchemy-filter in nested graphQL query. It is working as expected with normal graphQL query but when implemented in nested query the filter seems to not work.

I have two models:

  • Group model
  • Task model
class GroupModel(Base):
    __tablename__ = "groups"
    id = Column(Integer, primary_key=True)
    name = Column(Text, index=True)
    tasks = relationship(
        "TaskModel",
        backref=backref("circle", lazy='bulk'),
        lazy='bulk',
        cascade="all, delete-orphan",
    )
class TaskModel(Base):
    __tablename__ = "tasks"
    id = Column(Integer, primary_key=True)
    group_id = Column(Integer, ForeignKey("groups.id"), nullable=False)
    title = Column(Text, nullable=False)
    done = Column(Boolean, nullable=False, index=True)
    categories = Column(
        Text,
        default="General",
    )

#The code for filtering :

class GroupTodoFilter(FilterSet):
    class Meta:
        model = TaskModel
        fields = {
            'title': ['eq'],
            'done': ['eq'],
            'categories': ['eq'],
        }


class GroupFilterableConnectionField(FilterableConnectionField):
    filters = {TaskModel: GroupTaskFilter()}


class TaskFilter(SQLAlchemyObjectType):
    class Meta:
        model = TaskModel
        interfaces = (relay.Node, )
        connection_field_factory = GroupFilterableConnectionField.factory


class TaskFilterConnection_1(Connection):
    class Meta:
        node = TaskFilter


class GroupFilterNode(SQLAlchemyObjectType):
    class Meta:
        model = GroupModel
        interfaces = (relay.Node, )
        connection_field_factory = GroupFilterableConnectionField.factory


class GroupFilterConnection(Connection):
    class Meta:
        node = GroupFilterNode

`

in schema:

all_task = GroupFilterableConnectionField(TaskFilterConnection_1)
  all_group_filters = GroupFilterableConnectionField(
     GroupFilterConnection)
filter_group = relay.Node.Field(GroupFilterNode)

Queries

query{
  allGroupFilters{
    edges{
      node{
        tasks(filters:{done:true}){
          edges{
            node{
              id
              groupId
              categories
              done
              title
            }
          }
        }
      }
    }
  }
}

query{
  allTask (filters:{done:false}){
    edges{
      node{
        id
        title
        groupId
        categories
        done
      }
    }
  }
}
query($id: ID!) {
  filterGroup (id: $id) {
    tasks (filters:
       {categories:"Health"}
    )
      {
      edges {
        node {
          id
          group {
            id
          }
          title
          done
          categories
        }
      }
    }
  }
}`

Task query is working alright but filterGroup and allFilterGroup query have no filtering affect.

@Baddhizm
Copy link

Try this code:

class GroupFilter(FilterSet):
    class Meta:
        model = GroupModel
        fields = {
            # some fields
        }


class TaskFilter(FilterSet):
    class Meta:
        model = TaskModel
        fields = {
            # some fields
        }


class CustomFilterableConnectionField(FilterableConnectionField):
    filters = {
        TaskModel: TaskFilter(), 
        GroupModel: GroupFilter()
    }


class TaskNode(SQLAlchemyObjectType):
    class Meta:
        model = TaskModel
        interfaces = (Node,)
        connection_field_factory = CustomFilterableConnectionField.factory


class GroupNode(SQLAlchemyObjectType):
    class Meta:
        model = GroupModel
        interfaces = (Node,)
        connection_field_factory = CustomFilterableConnectionField.factory


class TaskConnection(Connection):
    class Meta:
        node = TaskNode


class GroupConnection:
    class Meta:
        node = GroupNode


class Query(ObjectType):
    all_tasks = CustomFilterableConnectionField(TaskConnection)
    all_groups = CustomFilterableConnectionField(GroupConnection)

@bhavnish07
Copy link
Author

bhavnish07 commented Oct 13, 2020

Hi!
We actually want to do query on groups and filter tasks fields from it (groups and tasks are related in one to many relationship) . Others queries were just for testing the filter.

  • Filters are working for groups and tasks independently but when we try to have a filter on groups and try to filter tasks field from it. It seems to not work.

Filters are not working for the below query:

query($id: ID!) {
  filterGroup (id: $id) {
    tasks  (filters:
       { categories:"Health"}
    )
      {
      edges {
        node {
          id
          group {
            id
          }
          title
          done
          categories
        }
      }
    }
  }
}

It seems like nested filters are not working.

@art1415926535
Copy link
Owner

Hi @bhavnish07! I have reproduced your example here https://github.com/art1415926535/graphene-sqlalchemy-filter/tree/issue_35/examples/groups_and_tasks.

Everything seems to work fine.

image
image

@docelic
Copy link

docelic commented Oct 14, 2020

Nice effort @art1415926535 , thank you in the name of all interested in this module!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants