graphql-python / graphene-mongo

Graphene MongoEngine integration
http://graphene-mongo.readthedocs.io/en/latest/
MIT License
288 stars 113 forks source link

How to achieve filtering? #82

Closed anilwarbhe closed 5 years ago

anilwarbhe commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

Hi Irfan,

Thank you so much for your wonderful graphene-mongo-extras. I was just playing with the code and found an issue. Would you please help me with it. I would be really thankful to you.

If I have a info of "Highscore" info=ListField(EmbeddedDocumentField(PlaythruInfo)) insted of info = EmbeddedDocumentField(PlaythruInfo) it thows "AttributeError: type object 'NoneType' has no attribute 'name'" class HighScore(Document): player = StringField() score = IntField() recorded = DateTimeField() info = EmbeddedDocumentField(PlaythruInfo)

use info=ListField(EmbeddedDocumentField(PlaythruInfo))

and it will throw an error.

AttributeError: type object 'MongoengineConnectionField' has no attribute 'name' OR AttributeError: type object 'NoneType' has no attribute 'name'

Please help.

Regards Anil

www.ibizwaresolutions.comhttp://www.ibizwaresolutions.com A Step to Hug the Universe! "The difference between a successful person and others is not a lack of strength, not a lack of knowledge, but rather a lack in will." - Vince Lombardi


From: Irfan Baig notifications@github.com Sent: Monday, April 29, 2019 4:38 AM To: graphql-python/graphene-mongo Cc: anilwarbhe; Author Subject: Re: [graphql-python/graphene-mongo] How to achieve filtering? (#82)

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

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHubhttps://github.com/graphql-python/graphene-mongo/issues/82#issuecomment-487423263, or mute the threadhttps://github.com/notifications/unsubscribe-auth/ALKYDUW4B2WLYBTTCTCVMXLPSYVALANCNFSM4HHLI4WQ.

riverfr0zen commented 5 years ago

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

abawchen commented 5 years ago

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

dscso commented 3 years ago

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 commented 2 years ago

@dscso big thank you for posting your solution!