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

How to achieve filtering? #82

Closed
anilwarbhe opened this issue Apr 21, 2019 · 6 comments
Closed

How to achieve filtering? #82

anilwarbhe opened this issue Apr 21, 2019 · 6 comments

Comments

@anilwarbhe
Copy link

Hi,

It would be good if graphene mongo had support for filters. For example
query { allPersons(filter: { age_gt: 18 }) { firstName lastName } }
Is there any other way to achieve this or fire a query like thin in graphene-mongo?

@riverfr0zen
Copy link
Contributor

I've been working on filtering stuff with graphene-mongo. It's very early work (wasn't planning to put it up till later), so full disclaimers, etc. etc. but you can check it out here (python 3 only):

https://github.com/riverfr0zen/graphene-mongo-extras

Since there isn't much documentation yet, I also put up a repo with an example flask app that shows you how the queries might look:
https://github.com/riverfr0zen/graphene-mongo-extras-examples

@anilwarbhe
Copy link
Author

anilwarbhe commented May 12, 2019 via email

@riverfr0zen
Copy link
Contributor

@anilwarbhe I will respond to the issue you created on that repository when I get a chance.

@abawchen
Copy link
Collaborator

@anilwarbhe @riverfr0zen : Close this one and let's move the discussion on https://github.com/riverfr0zen/graphene-mongo-extras later.

@dscso
Copy link

dscso commented Feb 23, 2021

Hello, I have had the same issue and I solved it by creating a filter class like this (It even got pagination 😱):
models.py

class Product(db.DynamicDocument):
    name = db.StringField(required=True)
    tags = db.ListField(db.StringField())
    ...

And the schema

class ProductSchema(MongoengineObjectType):
    class Meta:
        description = "Products"
        model = models.Product
        connection_class = ExtendedConnection
        interfaces = (Node,)


class ProductFilter(graphene.InputObjectType):
    name = graphene.String()
    name__icontains = graphene.String(name="name_icontains")
    tags__in = graphene.List(graphene.String, name="tags_in", description="Product has to have ONE of the provided tags")
    tags__all = graphene.List(graphene.String, name="tags_all", description="Product has to have all provided tags")


class QueryProductsSchema(graphene.ObjectType):
    total_count = graphene.Int()
    total_pages = graphene.Int()
    products = graphene.List(ProductSchema)


class Query(graphene.ObjectType):
    class Meta():
        description = "Root Query"

    query_products = graphene.Field(QueryProductsSchema, first=graphene.Int(), offset=graphene.Int(), filters=ProductFilter())

    def resolve_query_products(self, info, **kwargs):
        query = models.Product.objects(**kwargs.get("filters", {}))
        pagination = query.paginate(kwargs.get("offset", 1), kwargs.get("first", 20))

        return QueryProductsSchema(total_count=pagination.total, total_pages=pagination.pages,
                                   products=pagination.items)

Example Query:

{
  queryProducts(first: 2, offset: 1, filters: {
    tags_in: ["ww"],
    name_icontains: "hähnchen"
  }) {
    totalCount
    totalPages
    products {
      name
      tags
    }
  }
}

To define what field should be querried like what you have to name the fields in the ProductFilter class according to your needs (http://docs.mongoengine.org/guide/querying.html)
Maybe a bit late I know but maybe it helps someone

@vkresch
Copy link

vkresch commented Feb 6, 2022

@dscso big thank you for posting your solution!

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

5 participants