Skip to content

fix: declaration of required variable in filters v2 #1136

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
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
6 changes: 5 additions & 1 deletion graphene_django/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,11 @@ class EnumWithDescriptionsType(object):
def description(self):
return named_choices_descriptions[self.name]

return Enum(name, list(named_choices), type=EnumWithDescriptionsType)
if named_choices == []:
# Python 2.7 doesn't handle enums with lists with zero entries, but works okay with empty sets
named_choices = set()

return Enum(name, named_choices, type=EnumWithDescriptionsType)


def generate_enum_name(django_model_meta, field):
Expand Down
24 changes: 18 additions & 6 deletions graphene_django/filter/tests/test_enum_filtering.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,22 @@ def test_filter_enum_field_schema_type(schema):
in schema_str
)

assert (
"""type Query {
allReporters(offset: Int, before: String, after: String, first: Int, last: Int): ReporterTypeConnection
allArticles(offset: Int, before: String, after: String, first: Int, last: Int, lang: ArticleLang, lang_In: [ArticleLang], reporter_AChoice: ReporterAChoice, reporter_AChoice_In: [ReporterAChoice]): ArticleTypeConnection
}"""
in schema_str
filters = {
"offset": "Int",
"before": "String",
"after": "String",
"first": "Int",
"last": "Int",
"lang": "ArticleLang",
"lang_In": "[ArticleLang]",
"reporter_AChoice": "ReporterAChoice",
"reporter_AChoice_In": "[ReporterAChoice]",
}

all_articles_filters = (
schema_str.split(" allArticles(")[1]
.split("): ArticleTypeConnection\n")[0]
.split(", ")
)
for filter_field, gql_type in filters.items():
assert "{}: {}".format(filter_field, gql_type) in all_articles_filters
21 changes: 20 additions & 1 deletion graphene_django/filter/tests/test_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

if DJANGO_FILTER_INSTALLED:
import django_filters
from django_filters import FilterSet, NumberFilter
from django_filters import FilterSet, NumberFilter, OrderingFilter

from graphene_django.filter import (
GlobalIDFilter,
Expand Down Expand Up @@ -1216,3 +1216,22 @@ class Query(ObjectType):
assert result.data == {
"people": {"edges": [{"node": {"name": "Joe"}}, {"node": {"name": "Bob"}},]}
}


def test_only_custom_filters():
class ReporterFilter(FilterSet):
class Meta:
model = Reporter
fields = []

some_filter = OrderingFilter(fields=("name",))

class ReporterFilterNode(DjangoObjectType):
class Meta:
model = Reporter
interfaces = (Node,)
fields = "__all__"
filterset_class = ReporterFilter

field = DjangoFilterConnectionField(ReporterFilterNode)
assert_arguments(field, "some_filter")
3 changes: 1 addition & 2 deletions graphene_django/filter/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def get_filtering_args_from_filterset(filterset_class, type):
registry = type._meta.registry
for name, filter_field in six.iteritems(filterset_class.base_filters):
filter_type = filter_field.lookup_expr
required = filter_field.extra.get("required", False)
field_type = None
form_field = None

Expand All @@ -49,8 +50,6 @@ def get_filtering_args_from_filterset(filterset_class, type):
or isinstance(filter_field, ArrayFilter)
):
# Get the filter field for filters that are no explicitly declared.

required = filter_field.extra.get("required", False)
if filter_type == "isnull":
field = graphene.Boolean(required=required)
else:
Expand Down