graphene-django add additional argument for DjangoFilterConnectionField and pass it to get_queryset

933 views Asked by At

I have added an additional argument called user_type to disposal_request query, and I want to use this argument to add another filter but when I passed it to get_queryset function. I get the following error: get_queryset() missing 1 required positional argument: 'user_type' I'm not sure how to pass the argument properly.

import graphene
import graphene_django
from app.disposer.models import DisposalRequest
from graphene_django.filter import DjangoFilterConnectionField
from graphql_jwt.decorators import login_required


class UserType(graphene.Enum):
    as_collector = "as_collector"
    as_disposer = "as_disposer"


class DisposalRequestsType(graphene_django.DjangoObjectType):
    latitude = graphene.String()
    longitude = graphene.String()

    class Meta:
        model = DisposalRequest
        exclude = ("location",)
        filter_fields = [
            "uuid",
            "disposal_status",
        ]

        interfaces = (graphene.Node,)
        convert_choices_to_enum = False

    @classmethod
    @login_required
    def get_queryset(cls, queryset, info, user_type):
        user = info.context.user
        if user_type == UserType.as_collector:
            print("as_collector")
            return queryset.filter(collector=user)
        elif user_type == UserType.as_disposer:
            print("as_disposer")
            return queryset.filter(disposer=user)

   def resolve_latitude(self, _):
        return self.location.x

    def resolve_longitude(self, _):
        return self.location.y

class DisposalQueries(graphene.ObjectType):
    disposal_requests = DjangoFilterConnectionField(
        DisposalRequestsType, user_type=UserType()
    )
1

There are 1 answers

0
Ark Medes On

I ended up putting the additional filter to resolve_ function and pass **kwargs for additional fields added by DjangoFilterConnectionField

import graphene
import graphene_django
from app.disposer.models import DisposalRequest
from graphene_django.filter import DjangoFilterConnectionField
from graphql_jwt.decorators import login_required


class UserType(graphene.Enum):
    as_collector = "as_collector"
    as_disposer = "as_disposer"


class DisposalRequestsType(graphene_django.DjangoObjectType):
    latitude = graphene.String()
    longitude = graphene.String()

    class Meta:
        model = DisposalRequest
        exclude = ("location",)
        filter_fields = [
            "uuid",
            "disposal_status",
        ]

        interfaces = (graphene.Node,)
        convert_choices_to_enum = False

    def resolve_latitude(self, _):
        return self.location.x

    def resolve_longitude(self, _):
        return self.location.y


class DisposalQueries(graphene.ObjectType):
    disposal_requests = DjangoFilterConnectionField(
        DisposalRequestsType, user_type=UserType()
    )

    @staticmethod
    @login_required
    def resolve_disposal_requests(_, info, user_type, **__):
        user = info.context.user
        if user_type == UserType.as_collector:
            return DisposalRequest.objects.filter(collector=user).all()
        elif user_type == UserType.as_disposer:
            return DisposalRequest.objects.filter(disposer=user).all()