question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to achieve filtering?

See original GitHub issue

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?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
dscsocommented, 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

1reaction
riverfr0zencommented, Apr 28, 2019

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Quick start: Filter data by using an AutoFilter - Microsoft Support
On the Data tab, in the Sort & Filter group, click Filter. The Sort and Filter group on the Data tab · Click...
Read more >
How to Filter in Excel (Easy Tutorial)
Filter · 1. Click any single cell inside a data set. · 2. On the Data tab, in the Sort & Filter group,...
Read more >
Excel 2010: Filtering Data - GCF Global
To add another filter: · Uncheck the boxes next to the data you don't want to view. Check the boxes next to the...
Read more >
How to Achieve Agile Market Research by Filtering Data
First off, our filtering data function allows researchers to reach the correct respondents, with demographics categories that filter through common categories ...
Read more >
Data Filtering Guide - Jotform
In this guide, we walk through the ins and outs of data filtering and make what most consider a complex topic much simpler....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found