from __future__ import annotations
from typing import TYPE_CHECKING
from strawberry.field import StrawberryField
from strawberry_django.filters import StrawberryDjangoFieldFilters
from strawberry_django.resolvers import django_resolver
if TYPE_CHECKING:
from typing import Optional
from django.db.models import Model
class StrawberryDjangoCountFieldBase:
def get_queryset(self, queryset, info, **kwargs):
return queryset
class StrawberryDjangoCount(
StrawberryDjangoFieldFilters,
StrawberryDjangoCountFieldBase,
StrawberryField
):
is_list = True
def __init__(self, django_model: type[Model], *args, **kwargs):
self.django_model = django_model
super().__init__(*args, **kwargs)
@django_resolver
def get_result(self, source, info, args, kwargs):
return self.resolver(info=info, source=source, *args, **kwargs)
def resolver(self, *args, **kwargs):
return super().get_queryset(
queryset=self.django_model.objects.all(), *args, **kwargs).count()
if TYPE_CHECKING:
# .eg
import strawberry
import strawberry_django
from django.contrib.contenttypes.models import ContentType
from strawberry_django import auto
@strawberry_django.filters.filter(ContentType, lookups=True)
class ContentTypeFilter:
id: auto
app_label: auto
model: auto
@strawberry.type
class Query:
ContentTypesCount: int = StrawberryDjangoCount(
ContentType, filters=ContentTypeFilter)
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too