Skip to content

Commit

Permalink
Allow using a custom resolver function on DjangoConnectionField
Browse files Browse the repository at this point in the history
  • Loading branch information
mb1511 committed Apr 18, 2024
1 parent 6f21dc7 commit 9e6fc7f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
5 changes: 4 additions & 1 deletion graphene_django/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
37 changes: 36 additions & 1 deletion graphene_django/tests/test_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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"}}]}}

0 comments on commit 9e6fc7f

Please sign in to comment.