From c127aec75b33617b8eb10c84fb0733654ce70bf3 Mon Sep 17 00:00:00 2001 From: Florian Zimmermann Date: Thu, 4 Jul 2024 12:24:30 +0200 Subject: [PATCH] bugfix: let DjangoConnectionField call its resolver function that is, the one specified using DjangoConnectionField(..., resolver=some_func) --- graphene_django/fields.py | 2 +- graphene_django/tests/test_query.py | 47 +++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/graphene_django/fields.py b/graphene_django/fields.py index 1bbe1f3..9e9f457 100644 --- a/graphene_django/fields.py +++ b/graphene_django/fields.py @@ -247,7 +247,7 @@ class DjangoConnectionField(ConnectionField): def wrap_resolve(self, parent_resolver): return partial( self.connection_resolver, - parent_resolver, + self.resolver or parent_resolver, self.connection_type, self.get_manager(), self.get_queryset_resolver(), diff --git a/graphene_django/tests/test_query.py b/graphene_django/tests/test_query.py index 0e0a31c..85ea145 100644 --- a/graphene_django/tests/test_query.py +++ b/graphene_django/tests/test_query.py @@ -1,5 +1,6 @@ import base64 import datetime +from unittest.mock import ANY, Mock import pytest from django.db import models @@ -2000,6 +2001,52 @@ def test_connection_should_succeed_if_last_higher_than_number_of_objects(): assert result.data == expected +def test_connection_should_call_resolver_function(): + resolver_mock = Mock( + name="resolver", + return_value=[ + Reporter(first_name="Some", last_name="One"), + Reporter(first_name="John", last_name="Doe"), + ], + ) + + class ReporterType(DjangoObjectType): + class Meta: + model = Reporter + fields = "__all__" + interfaces = [Node] + + class Query(graphene.ObjectType): + reporters = DjangoConnectionField(ReporterType, resolver=resolver_mock) + + schema = graphene.Schema(query=Query) + result = schema.execute( + """ + query { + reporters { + edges { + node { + firstName + lastName + } + } + } + } + """ + ) + + resolver_mock.assert_called_once_with(None, ANY) + assert not result.errors + assert result.data == { + "reporters": { + "edges": [ + {"node": {"firstName": "Some", "lastName": "One"}}, + {"node": {"firstName": "John", "lastName": "Doe"}}, + ], + }, + } + + def test_should_query_nullable_foreign_key(): class PetType(DjangoObjectType): class Meta: