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 make an OR/AND filtering

See original GitHub issue

Hi Guys! I’m new using this library, and I have a question.

How can I join filters with and/or for the same filter for example, I’m using django-cities and I’ll make a query that is like this in django ORM :

Q(kind="PPLC") | Q(kind="PPLA")

but when I try to do something like this in graphene, I do not know how I should do it, I found someone that shows how to and/or in graphql but that way does not work, the query was something like this translated to what I need :

{ cities(filter: { "OR" : [ { "kind": "PPLC"}, { "kind": "PPLA" } ] }){ <body> } }

Also I tried to do a IN filter with django-filters agains what the documentation says:

filter_fields = { 'country__name' : ['iexact', 'icontains'], 'kind': ['in', 'iexact', 'icontains'] }

but when I try to query, like this : { cities(kind_In: ["PPLC", "PPLA"] } it shows that Graphene is waiting kind__In be a String, but I do the same as a string, and it doesn’t filter to what I need (actually with args as “[‘PPLC’,‘PPLA’]”. and data comes empty)

Can anyone help me 😃 would be appreciated.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
mjmonroecommented, Sep 19, 2017

We have the same use case. GraphQl is not a query language and does not natively support nested queries. The graphene.InputObjectType can be used to create your own JSON format that supports nested queries. The code then reads the JSON and translates it into django Q objects. We are using MongoEngine’s Q objects, which are based on django’s.

In our case we have a filter input which is a self referencing graphene.InputObjectType. Self referencing because you can nest the query as deep as you want. Each filter level represents a single nesting level. The linkage between a resolver and a query is broken because a single InputObjectType can be used to query any field in the database. By breaking the linkage, we allow for abritary ‘AND’/‘OR’ combinations of any depth with any field in the database. For instance, a field can be queried on, but never returned by GraphQl. The search model is completely decoupled from the GraphQl domain model. Now that they are separate, they can evolve separately. Different views of the data can be generated and returned by GraphQl, all based on the same query.

Example Filter InputObjectType

Input Description
op Enum: Operator to apply at current level
content [Filter]: List of filters to apply underneath current level. Only valid for logic operators ‘AND’, ‘OR’, ‘MATCH’. This is self referencing, filters contain lists of more filters.
field String: Field to apply operator on. Not valid for ‘AND’ and ‘OR’ operators.
value [String]: Value is what a field is compared to, << field >><< op >><< value >>. Not valid for logic operators.
negate_op Boolean: Negate the operator by adding a programatic ‘not’
query ExampleFourNesting {
    result(filter: {
        op: OR,
        content: [{
                op: AND,
                content: [{
                        op: eq,
                        value: ["4"],
                        field: "some_field"
                    }, {
                        op: gt,
                        value: 16,
                        field: "another_field"
                    }

                ]
            }, {
                op: exact,
                value: "some_value",
                field: "another_field"
            }
        ]
    }) {
        hits {
           stuff
        }
        pagination {
            count
            pages
            page
            size
        }
    }
}
0reactions
julianmojicommented, Apr 13, 2022

@mjmonroe Can you tell me please which are your libraries versions?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Filter - Orange Data Mining
The Filter widget filters cells or genes by the number of positive measurements ... This allows us to have leaner data sets, which...
Read more >
How to Create the Orange & Teal Filter Using Adobe Lightroom
1. Open Adobe Lightroom · 2. open the develop module · 3. Turn your red hues to orange and your Blue Hues to...
Read more >
Data Filters (filter) - Orange Data Mining Library
Table implements the filter on the sparse data so that it returns all rows for which all class values are defined, even if...
Read more >
Filtering (filter) — Orange Documentation v2.7.8
Filters select subsets of instances. They are most typically used to select data instances from a table, for example to drop all instances...
Read more >
Teal and Orange filters in Preview app
Teal and Orange filters in Preview app · 1. Upload your photos in Preview app · 2. Open the editing tools · 3....
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