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 2, 2024
1 parent 3f813d4 commit a1cc52c
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
5 changes: 4 additions & 1 deletion graphene_django/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,9 +251,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
32 changes: 32 additions & 0 deletions graphene_django/tests/test_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -748,3 +748,35 @@ class Query(ObjectType):
match="DjangoConnectionField only accepts DjangoObjectType types as underlying type",
):
Schema(query=Query)

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 a1cc52c

Please sign in to comment.