How to make an OR/AND filtering
See original GitHub issueHi 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:
- Created 6 years ago
- Reactions:1
- Comments:5 (1 by maintainers)
Top GitHub Comments
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
@mjmonroe Can you tell me please which are your libraries versions?