Skip to content

Commit

Permalink
Fix 1061: DjangoListField should not cache queries (#1063)
Browse files Browse the repository at this point in the history
* fix( DjangoListField ): test that default functionality should resolve/call queryset at view time, first attempt at solution

* fix( DjangoListField ): DjangoListField defines get_manager just like DjangoConnectionField for a better variable name default_manager instead of default_queryset

* fix: apply specific black formatting
  • Loading branch information
zbyte64 authored Dec 23, 2020
1 parent 454b740 commit 7b35695
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 8 deletions.
13 changes: 5 additions & 8 deletions graphene_django/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,16 @@ def _underlying_type(self):
def model(self):
return self._underlying_type._meta.model

def get_default_queryset(self):
return self.model._default_manager.get_queryset()
def get_manager(self):
return self.model._default_manager

@staticmethod
def list_resolver(
django_object_type, resolver, default_queryset, root, info, **args
django_object_type, resolver, default_manager, root, info, **args
):
queryset = maybe_queryset(resolver(root, info, **args))
if queryset is None:
queryset = default_queryset
queryset = maybe_queryset(default_manager)

if isinstance(queryset, QuerySet):
# Pass queryset to the DjangoObjectType get_queryset method
Expand All @@ -66,10 +66,7 @@ def get_resolver(self, parent_resolver):
_type = _type.of_type
django_object_type = _type.of_type.of_type
return partial(
self.list_resolver,
django_object_type,
parent_resolver,
self.get_default_queryset(),
self.list_resolver, django_object_type, parent_resolver, self.get_manager(),
)


Expand Down
33 changes: 33 additions & 0 deletions graphene_django/tests/test_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,39 @@ class Query(ObjectType):
"reporters": [{"firstName": "Tara"}, {"firstName": "Debra"}]
}

def test_list_field_queryset_is_not_cached(self):
class Reporter(DjangoObjectType):
class Meta:
model = ReporterModel
fields = ("first_name",)

class Query(ObjectType):
reporters = DjangoListField(Reporter)

schema = Schema(query=Query)

query = """
query {
reporters {
firstName
}
}
"""

result = schema.execute(query)
assert not result.errors
assert result.data == {"reporters": []}

ReporterModel.objects.create(first_name="Tara", last_name="West")
ReporterModel.objects.create(first_name="Debra", last_name="Payne")

result = schema.execute(query)

assert not result.errors
assert result.data == {
"reporters": [{"firstName": "Tara"}, {"firstName": "Debra"}]
}

def test_override_resolver(self):
class Reporter(DjangoObjectType):
class Meta:
Expand Down

0 comments on commit 7b35695

Please sign in to comment.