Skip to content

Commit

Permalink
Allow to use camel case in order by field (#1054)
Browse files Browse the repository at this point in the history
Fixes #1008
  • Loading branch information
artofhuman authored Dec 23, 2020
1 parent 0e12343 commit a51c2bf
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 3 deletions.
14 changes: 12 additions & 2 deletions graphene_django/filter/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from django.core.exceptions import ValidationError
from graphene.types.argument import to_arguments
from graphene.utils.str_converters import to_snake_case
from ..fields import DjangoConnectionField
from .utils import get_filtering_args_from_filterset, get_filterset_class

Expand Down Expand Up @@ -61,12 +62,21 @@ def filtering_args(self):
def resolve_queryset(
cls, connection, iterable, info, args, filtering_args, filterset_class
):
def filter_kwargs():
kwargs = {}
for k, v in args.items():
if k in filtering_args:
if k == "order_by":
v = to_snake_case(v)
kwargs[k] = v
return kwargs

qs = super(DjangoFilterConnectionField, cls).resolve_queryset(
connection, iterable, info, args
)
filter_kwargs = {k: v for k, v in args.items() if k in filtering_args}

filterset = filterset_class(
data=filter_kwargs, queryset=qs, request=info.context
data=filter_kwargs(), queryset=qs, request=info.context
)
if filterset.form.is_valid():
return filterset.qs
Expand Down
2 changes: 1 addition & 1 deletion graphene_django/filter/tests/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Meta:
model = Reporter
fields = ["first_name", "last_name", "email", "pets"]

order_by = OrderingFilter(fields=("pub_date",))
order_by = OrderingFilter(fields=("first_name",))


class PetFilter(django_filters.FilterSet):
Expand Down
67 changes: 67 additions & 0 deletions graphene_django/filter/tests/test_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -713,6 +713,73 @@ def resolve_all_reporters(self, info, **args):
assert result.data == expected


def test_order_by():
class ReporterType(DjangoObjectType):
class Meta:
model = Reporter
interfaces = (Node,)

class Query(ObjectType):
all_reporters = DjangoFilterConnectionField(
ReporterType, filterset_class=ReporterFilter
)

Reporter.objects.create(first_name="b")
Reporter.objects.create(first_name="a")

schema = Schema(query=Query)
query = """
query NodeFilteringQuery {
allReporters(orderBy: "-firstName") {
edges {
node {
firstName
}
}
}
}
"""
expected = {
"allReporters": {
"edges": [{"node": {"firstName": "b"}}, {"node": {"firstName": "a"}}]
}
}

result = schema.execute(query)
assert not result.errors
assert result.data == expected

query = """
query NodeFilteringQuery {
allReporters(orderBy: "-first_name") {
edges {
node {
firstName
}
}
}
}
"""

result = schema.execute(query)
assert not result.errors
assert result.data == expected

query = """
query NodeFilteringQuery {
allReporters(orderBy: "-firtsnaMe") {
edges {
node {
firstName
}
}
}
}
"""
result = schema.execute(query)
assert result.errors


def test_order_by_is_perserved():
class ReporterType(DjangoObjectType):
class Meta:
Expand Down

0 comments on commit a51c2bf

Please sign in to comment.