art1415926535 / graphene-sqlalchemy-filter

Filters for Graphene SQLAlchemy integration
https://pypi.org/project/graphene-sqlalchemy-filter/
MIT License
118 stars 34 forks source link

Wrong results returned? #26

Closed JBrVJxsc closed 4 years ago

JBrVJxsc commented 4 years ago

So I have 2 records in schema Foo: id: 1, key: 'foo' id: 2, key: 'foobar'

if I run the below command: query { Foo(filters: { key_like: "%foo%", and: [{ id: "1" }] }) { id key } } it will return: { "data": { "Foo": [ { "id": "1", "key": "foo" } ] } } , which is expected, as both records' key like foo, but only record 1 has the id 1, so only the record 1 was returned;

but if I run the below command:

query { Foo(filters: { key_like: "%foo%", or: [{ id: "2" }] }) { id key } } it will ONLY return: { "data": { "Foo": [ { "id": "2", "key": "foobar" } ] } } Shouldn't both records got returned? I'm not using relay and I'm using the example code (w/o relay) you provide the other day to build the filter set.

art1415926535 commented 4 years ago

The and and or parts must be used like sqlalchemy functions with arguments. The root filter combines all arguments using AND.

Let me explain with examples:

query { Foo(filters: { key_like: "%foo%", and: [{ id: "1" }] }) { id key } }
select * from foo where key like "%foo%" AND id = "1"

query { Foo(filters: { key_like: "%foo%", or: [{ id: "2" }] }) { id key } }
select * from foo where key like "%foo%" AND id = "2"

query { Foo(filters: {or: [{key_like: "%foo%"}, {"id": "2"}]}) { id key } }
select * from foo where key like "%foo%" OR id = "2"

query { Foo(filters: {or: [{key_like: "%foo%"}, {"id": "2", key_like: "%bar%"}]}) { id key } }
select * from foo where key like "%foo%" OR (id = "2" AND key like "%bar%")
JBrVJxsc commented 4 years ago

got it! thank you!