Allow using a custom resolver function on DjangoConnectionField

This commit is contained in:
Matt 2024-04-02 09:17:54 +01:00
parent 6f21dc7a94
commit 9e6fc7fc82
2 changed files with 40 additions and 2 deletions

View File

@ -245,9 +245,12 @@ class DjangoConnectionField(ConnectionField):
return on_resolve(iterable) return on_resolve(iterable)
def wrap_resolve(self, parent_resolver): 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( return partial(
self.connection_resolver, self.connection_resolver,
parent_resolver, resolver,
self.connection_type, self.connection_type,
self.get_manager(), self.get_manager(),
self.get_queryset_resolver(), self.get_queryset_resolver(),

View File

@ -5,8 +5,9 @@ import pytest
from django.db.models import Count, Prefetch from django.db.models import Count, Prefetch
from graphene import List, NonNull, ObjectType, Schema, String 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 ..types import DjangoObjectType
from .models import ( from .models import (
Article as ArticleModel, Article as ArticleModel,
@ -716,3 +717,37 @@ class TestDjangoListField:
r'SELECT .* FROM "tests_film" INNER JOIN "tests_film_reporters" .* LEFT OUTER JOIN "tests_filmdetails"', r'SELECT .* FROM "tests_film" INNER JOIN "tests_film_reporters" .* LEFT OUTER JOIN "tests_filmdetails"',
captured.captured_queries[1]["sql"], 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"}}]}}