Fix 1061: DjangoListField should not cache queries (#1063)

* 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
This commit is contained in:
Jason Kraus 2020-12-22 20:10:28 -08:00 committed by GitHub
parent 454b74052e
commit 7b35695067
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 8 deletions

View File

@ -43,16 +43,16 @@ class DjangoListField(Field):
def model(self): def model(self):
return self._underlying_type._meta.model return self._underlying_type._meta.model
def get_default_queryset(self): def get_manager(self):
return self.model._default_manager.get_queryset() return self.model._default_manager
@staticmethod @staticmethod
def list_resolver( 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)) queryset = maybe_queryset(resolver(root, info, **args))
if queryset is None: if queryset is None:
queryset = default_queryset queryset = maybe_queryset(default_manager)
if isinstance(queryset, QuerySet): if isinstance(queryset, QuerySet):
# Pass queryset to the DjangoObjectType get_queryset method # Pass queryset to the DjangoObjectType get_queryset method
@ -66,10 +66,7 @@ class DjangoListField(Field):
_type = _type.of_type _type = _type.of_type
django_object_type = _type.of_type.of_type django_object_type = _type.of_type.of_type
return partial( return partial(
self.list_resolver, self.list_resolver, django_object_type, parent_resolver, self.get_manager(),
django_object_type,
parent_resolver,
self.get_default_queryset(),
) )

View File

@ -75,6 +75,39 @@ class TestDjangoListField:
"reporters": [{"firstName": "Tara"}, {"firstName": "Debra"}] "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): def test_override_resolver(self):
class Reporter(DjangoObjectType): class Reporter(DjangoObjectType):
class Meta: class Meta: