Skip to content

Basic custom query filters #103

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion graphene_mongo/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def registry(self):
def args(self):
return to_arguments(
self._base_args or OrderedDict(),
dict(self.field_args, **self.reference_args)
dict(dict(self.field_args, **self.reference_args), **self.filter_args)
)

@args.setter
Expand Down Expand Up @@ -104,6 +104,17 @@ def get_type(v):
def field_args(self):
return self._field_args(self.fields.items())

@property
def filter_args(self):
filter_args = dict()
if self._type._meta.filter_fields:
for field, filter_collection in self._type._meta.filter_fields.items():
for each in filter_collection:
filter_args[field + "__" + each] = graphene.Argument(
type=getattr(graphene, str(self._type._meta.fields[field].type).replace("!", "")))

return filter_args

@property
def reference_args(self):

Expand Down
2 changes: 2 additions & 0 deletions graphene_mongo/tests/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ class PlayerNode(MongoengineObjectType):
class Meta:
model = models.Player
interfaces = (Node, )
filter_fields = {
'first_name': ['istartswith']}


class ReporterNode(MongoengineObjectType):
Expand Down
1 change: 1 addition & 0 deletions graphene_mongo/tests/test_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,3 +330,4 @@ class Meta:
Reporter._fields['generic_reference'], registry.get_global_registry())
assert isinstance(generic_reference_field, graphene.Field)
assert isinstance(generic_reference_field.type(), graphene.Union)
assert generic_reference_field.type()._meta.types == (A, E)
43 changes: 41 additions & 2 deletions graphene_mongo/tests/test_relay_query.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import base64
import os
import json
import pytest

import base64
import graphene

from graphene.relay import Node
Expand Down Expand Up @@ -1038,3 +1038,42 @@ class Query(graphene.ObjectType):
result = schema.execute(query)
assert not result.errors
assert result.data == expected


def test_should_filter_mongoengine_queryset(fixtures):

class Query(graphene.ObjectType):
players = MongoengineConnectionField(nodes.PlayerNode)

query = '''
query players {
players(firstName_Istartswith: "M") {
edges {
node {
firstName
}
}
}
}
'''
expected = {
'players': {
'edges': [
{
'node': {
'firstName': 'Michael',
}
},
{
'node': {
'firstName': 'Magic'
}
}
]
}
}
schema = graphene.Schema(query=Query)
result = schema.execute(query)

assert not result.errors
assert json.dumps(result.data, sort_keys=True) == json.dumps(expected, sort_keys=True)