mirror of
https://github.com/graphql-python/graphene-django.git
synced 2025-01-31 03:34:13 +03:00
Keep original queryset on DjangoFilterConnectionField (#816)
* Keep original queryset on DjangoFilterConnectionField The PR #796 broke DjangoFilterConnectionField making it always get the raw queryset from the model to apply the filters in it. This makes sure that the DjangoObjectType's .get_queryset is called, keeping any filtering it might have made. * Add regression test
This commit is contained in:
parent
e82a2d75c6
commit
7e7f18ee0e
|
@ -55,10 +55,11 @@ class DjangoFilterConnectionField(DjangoConnectionField):
|
|||
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(
|
||||
|
|
|
@ -756,6 +756,44 @@ def test_annotation_with_only():
|
|||
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:
|
||||
|
|
Loading…
Reference in New Issue
Block a user