Skip to content

Keep original queryset on DjangoFilterConnectionField #816

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 2 commits into from
Nov 29, 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
7 changes: 4 additions & 3 deletions graphene_django/filter/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,11 @@ def filtering_args(self):
def resolve_queryset(
cls, connection, iterable, info, args, filtering_args, filterset_class
):
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}
return filterset_class(
data=filter_kwargs, queryset=iterable, request=info.context
).qs
return filterset_class(data=filter_kwargs, queryset=qs, request=info.context).qs

def get_queryset_resolver(self):
return partial(
Expand Down
38 changes: 38 additions & 0 deletions graphene_django/filter/tests/test_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -756,6 +756,44 @@ def resolve_all_reporters(self, info, **args):
assert result.data == expected


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

@classmethod
def get_queryset(cls, queryset, info):
return queryset.filter(first_name="b")

class Query(ObjectType):
all_reporters = DjangoFilterConnectionField(
ReporterType, reverse_order=Boolean()
)

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

schema = Schema(query=Query)
query = """
query NodeFilteringQuery {
allReporters(first: 10) {
edges {
node {
firstName
}
}
}
}
"""
expected = {"allReporters": {"edges": [{"node": {"firstName": "b"}}]}}

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


def test_integer_field_filter_type():
class PetType(DjangoObjectType):
class Meta:
Expand Down