Skip to content

Commit db3bc19

Browse files
authored
Merge pull request #123 from claradaia/fix-in-filter
Fix 'in' filter argument generation
2 parents 62e784a + c921728 commit db3bc19

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
@@ -113,8 +113,18 @@ def filter_args(self):
113113
if self._type._meta.filter_fields:
114114
for field, filter_collection in self._type._meta.filter_fields.items():
115115
for each in filter_collection:
116-
filter_args[field + "__" + each] = graphene.Argument(
117-
type=getattr(graphene, str(self._type._meta.fields[field].type).replace("!", "")))
116+
filter_type = getattr(
117+
graphene, str(self._type._meta.fields[field].type).replace("!", ""))
118+
119+
# handle special cases
120+
advanced_filter_types = {
121+
'in': graphene.List(filter_type),
122+
'nin': graphene.List(filter_type),
123+
'all': graphene.List(filter_type),
124+
}
125+
126+
filter_type = advanced_filter_types.get(each, filter_type)
127+
filter_args[field + "__" + each] = graphene.Argument(type=filter_type)
118128

119129
return filter_args
120130

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
@@ -1108,3 +1108,42 @@ def resolve_multiple_foos(self, *args, **kwargs):
11081108
schema = graphene.Schema(query=Query)
11091109
result = schema.execute(query)
11101110
assert not result.errors
1111+
1112+
1113+
def test_should_filter_mongoengine_queryset_with_list(fixtures):
1114+
1115+
class Query(graphene.ObjectType):
1116+
players = MongoengineConnectionField(nodes.PlayerNode)
1117+
1118+
query = '''
1119+
query players {
1120+
players(firstName_In: ["Michael", "Magic"]) {
1121+
edges {
1122+
node {
1123+
firstName
1124+
}
1125+
}
1126+
}
1127+
}
1128+
'''
1129+
expected = {
1130+
'players': {
1131+
'edges': [
1132+
{
1133+
'node': {
1134+
'firstName': 'Michael',
1135+
}
1136+
},
1137+
{
1138+
'node': {
1139+
'firstName': 'Magic'
1140+
}
1141+
}
1142+
]
1143+
}
1144+
}
1145+
schema = graphene.Schema(query=Query)
1146+
result = schema.execute(query)
1147+
1148+
assert not result.errors
1149+
assert json.dumps(result.data, sort_keys=True) == json.dumps(expected, sort_keys=True)

0 commit comments

Comments
 (0)