art1415926535 / graphene-sqlalchemy-filter

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

Change the syntax of a filter #14

Open SBelkaid opened 4 years ago

SBelkaid commented 4 years ago

Is it possible to change the syntax of the filter to: filters: {[column_name]: {[operator]: [value_name]}}?

art1415926535 commented 4 years ago

Not very easy but most likely possible, I think.

You need to override some parts of FilterSet:

art1415926535 commented 4 years ago

This is an interesting task. It is possible that this module can have several types of GraphQL schema.

Pros:

Cons:

Example:

class UserFilter(FilterSet):
    is_admin = graphene.Boolean()

    class Meta:
        model = User
        fields = {'username': ['ne', 'in']}

    @staticmethod
    def is_admin_filter(info, query, value):
        ...
{
  allUsers(filters: {username: {ne: "admin", in: ["user1", "user2"]}, is_admin: true}) {
    edges { node { id } }
  }
}