From 69457cffdf2041b69fc46ca9fdfc6ae131fd51c9 Mon Sep 17 00:00:00 2001 From: Niall Date: Mon, 6 Mar 2017 18:13:40 +0000 Subject: [PATCH] Attempt fix. Breaks tests --- graphene_django/fields.py | 4 ++-- graphene_django/filter/fields.py | 27 ++++++++++++++++++++++----- graphene_django/tests/test_query.py | 13 +++++-------- 3 files changed, 29 insertions(+), 15 deletions(-) diff --git a/graphene_django/fields.py b/graphene_django/fields.py index 3e7c378..9ba999c 100644 --- a/graphene_django/fields.py +++ b/graphene_django/fields.py @@ -53,8 +53,8 @@ class DjangoConnectionField(ConnectionField): iterable = default_manager iterable = maybe_queryset(iterable) if isinstance(iterable, QuerySet): - if iterable is not default_manager: - iterable = maybe_queryset(default_manager) + if default_manager is not None and iterable is not default_manager: + iterable &= maybe_queryset(default_manager) _len = iterable.count() else: _len = len(iterable) diff --git a/graphene_django/filter/fields.py b/graphene_django/filter/fields.py index 363e1d9..1b2c1c8 100644 --- a/graphene_django/filter/fields.py +++ b/graphene_django/filter/fields.py @@ -1,6 +1,8 @@ from collections import OrderedDict from functools import partial +from django.db.models.query import QuerySet + # from graphene.relay import is_node from graphene.types.argument import to_arguments from ..fields import DjangoConnectionField @@ -44,15 +46,30 @@ class DjangoFilterConnectionField(DjangoConnectionField): def filtering_args(self): return get_filtering_args_from_filterset(self.filterset_class, self.node_type) + # @staticmethod + # def connection_resolver(resolver, connection, default_manager, filterset_class, filtering_args, + # root, args, context, info): + # filter_kwargs = {k: v for k, v in args.items() if k in filtering_args} + # qs = filterset_class( + # data=filter_kwargs, + # queryset=default_manager.get_queryset() + # ).qs + # return DjangoConnectionField.connection_resolver(resolver, connection, qs, root, args, context, info) + @staticmethod def connection_resolver(resolver, connection, default_manager, filterset_class, filtering_args, root, args, context, info): filter_kwargs = {k: v for k, v in args.items() if k in filtering_args} - qs = filterset_class( - data=filter_kwargs, - queryset=default_manager.get_queryset() - ).qs - return DjangoConnectionField.connection_resolver(resolver, connection, qs, root, args, context, info) + + def new_resolver(root, args, context, info): + qs = resolver(root, args, context, info) + if qs is None or not isinstance(qs, QuerySet): + qs = default_manager.get_queryset() + qs = filterset_class(data=filter_kwargs, queryset=qs).qs + + return qs + + return DjangoConnectionField.connection_resolver(new_resolver, connection, None, root, args, context, info) def get_resolver(self, parent_resolver): return partial(self.connection_resolver, parent_resolver, self.type, self.get_manager(), diff --git a/graphene_django/tests/test_query.py b/graphene_django/tests/test_query.py index 1f4809b..6d2f8c8 100644 --- a/graphene_django/tests/test_query.py +++ b/graphene_django/tests/test_query.py @@ -382,7 +382,7 @@ def test_should_query_node_multiple_filtering(): class Meta: model = Article interfaces = (Node, ) - filter_fields = ('lang', 'tag') + filter_fields = ('lang', 'headline') class Query(graphene.ObjectType): all_reporters = DjangoConnectionField(ReporterType) @@ -398,24 +398,21 @@ def test_should_query_node_multiple_filtering(): pub_date=datetime.date.today(), reporter=r, editor=r, - lang='es', - tag='one' + lang='es' ) Article.objects.create( headline='Article Node 2', pub_date=datetime.date.today(), reporter=r, editor=r, - lang='en', - tag='two' + lang='en' ) Article.objects.create( headline='Article Node 3', pub_date=datetime.date.today(), reporter=r, editor=r, - lang='en', - tag='three' + lang='en' ) schema = graphene.Schema(query=Query) @@ -425,7 +422,7 @@ def test_should_query_node_multiple_filtering(): edges { node { id - articles(lang: "es", tag: "two") { + articles(lang: "es", headline: "Article Node 2") { edges { node { id