From 9e6fc7fc8282bc09ff6037f45c4e6d9d3c09ccfa Mon Sep 17 00:00:00 2001 From: Matt Date: Tue, 2 Apr 2024 09:17:54 +0100 Subject: [PATCH] Allow using a custom resolver function on DjangoConnectionField --- graphene_django/fields.py | 5 +++- graphene_django/tests/test_fields.py | 37 +++++++++++++++++++++++++++- 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/graphene_django/fields.py b/graphene_django/fields.py index 1bbe1f3d..2348c1b5 100644 --- a/graphene_django/fields.py +++ b/graphene_django/fields.py @@ -245,9 +245,12 @@ def connection_resolver( return on_resolve(iterable) def wrap_resolve(self, parent_resolver): + # skip the wrap_resolve method on ConnectionField as it's already + # wrapping the connection_resolver, so we don't want to do it twice + resolver = super(ConnectionField, self).wrap_resolve(parent_resolver) return partial( self.connection_resolver, - parent_resolver, + resolver, self.connection_type, self.get_manager(), self.get_queryset_resolver(), diff --git a/graphene_django/tests/test_fields.py b/graphene_django/tests/test_fields.py index 0f5b79a0..c6ef2b4a 100644 --- a/graphene_django/tests/test_fields.py +++ b/graphene_django/tests/test_fields.py @@ -5,8 +5,9 @@ from django.db.models import Count, Prefetch from graphene import List, NonNull, ObjectType, Schema, String +from graphene.relay import Node -from ..fields import DjangoListField +from ..fields import DjangoConnectionField, DjangoListField from ..types import DjangoObjectType from .models import ( Article as ArticleModel, @@ -716,3 +717,37 @@ def resolve_articles(root, info): r'SELECT .* FROM "tests_film" INNER JOIN "tests_film_reporters" .* LEFT OUTER JOIN "tests_filmdetails"', captured.captured_queries[1]["sql"], ) + + +class TestDjangoConnectionField: + def test_can_use_custom_resolver(self): + def resolve_some_people(*args, **kwargs): + return PersonModel.objects.filter(name="Bob") + + class PersonType(DjangoObjectType): + class Meta: + model = PersonModel + interfaces = (Node,) + + class Query(ObjectType): + people = DjangoConnectionField(PersonType, resolver=resolve_some_people) + + schema = Schema(query=Query) + PersonModel.objects.create(name="Bob") + PersonModel.objects.create(name="Alice") + + query = """ + query { + people { + edges { + node { + name + } + } + } + } + """ + + result = schema.execute(query) + assert not result.errors + assert result.data == {"people": {"edges": [{"node": {"name": "Bob"}}]}}