Skip to content

Commit 30045e3

Browse files
committed
fixes 'in' filtering
1 parent d8b9c03 commit 30045e3

File tree

3 files changed

+52
-3
lines changed

3 files changed

+52
-3
lines changed

graphene_mongo/fields.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,18 @@ def filter_args(self):
110110
if self._type._meta.filter_fields:
111111
for field, filter_collection in self._type._meta.filter_fields.items():
112112
for each in filter_collection:
113-
filter_args[field + "__" + each] = graphene.Argument(
114-
type=getattr(graphene, str(self._type._meta.fields[field].type).replace("!", "")))
113+
filter_type = getattr(
114+
graphene, str(self._type._meta.fields[field].type).replace("!", ""))
115+
116+
# handle special cases
117+
advanced_filter_types = {
118+
'in': graphene.List(filter_type),
119+
'nin': graphene.List(filter_type),
120+
'all': graphene.List(filter_type),
121+
}
122+
123+
filter_type = advanced_filter_types.get(each, filter_type)
124+
filter_args[field + "__" + each] = graphene.Argument(type=filter_type)
115125

116126
return filter_args
117127

graphene_mongo/tests/nodes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class Meta:
4343
model = models.Player
4444
interfaces = (Node, )
4545
filter_fields = {
46-
'first_name': ['istartswith']}
46+
'first_name': ['istartswith', 'in']}
4747

4848

4949
class ReporterNode(MongoengineObjectType):

graphene_mongo/tests/test_relay_query.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1077,3 +1077,42 @@ class Query(graphene.ObjectType):
10771077

10781078
assert not result.errors
10791079
assert json.dumps(result.data, sort_keys=True) == json.dumps(expected, sort_keys=True)
1080+
1081+
1082+
def test_should_filter_mongoengine_queryset_with_list(fixtures):
1083+
1084+
class Query(graphene.ObjectType):
1085+
players = MongoengineConnectionField(nodes.PlayerNode)
1086+
1087+
query = '''
1088+
query players {
1089+
players(firstName_In: ["Michael", "Magic"]) {
1090+
edges {
1091+
node {
1092+
firstName
1093+
}
1094+
}
1095+
}
1096+
}
1097+
'''
1098+
expected = {
1099+
'players': {
1100+
'edges': [
1101+
{
1102+
'node': {
1103+
'firstName': 'Michael',
1104+
}
1105+
},
1106+
{
1107+
'node': {
1108+
'firstName': 'Magic'
1109+
}
1110+
}
1111+
]
1112+
}
1113+
}
1114+
schema = graphene.Schema(query=Query)
1115+
result = schema.execute(query)
1116+
1117+
assert not result.errors
1118+
assert json.dumps(result.data, sort_keys=True) == json.dumps(expected, sort_keys=True)

0 commit comments

Comments
 (0)