graphene icon indicating copy to clipboard operation
graphene copied to clipboard

DjangoObjectType custom get_queryset is not hitting

Open sfbaker7 opened this issue 5 years ago • 4 comments

Hey. I was attempting to add a global filter on some DjangoObjectTypes (from docs), but the "get_queryset" method was not hit when I was using graphene.List

I found this issue where DjangoConnectionFields were suggested and I tried that and got it to work as long as I included interfaces = (Node, ).

However, I am pretty hesitant to use this solution because I don't know what the underlying RC was as to why my original get_querysets method wasn't hit and I can't find docs on DjangoConnectionTypes / why I need interfaces for connection to work (if this is some graphQL thing, apologies for not knowing).

If possible, is there a way to filter while using graphene.list? if not, do you mind pointing me in the right direction? Thanks

Here is my code by the way with the commented out sections my original impl:

from graphene_django import DjangoObjectType
from graphql import GraphQLError
from app.models import Transaction
from graphene import Node
from graphene_django.fields import DjangoConnectionField


class TransactionNode(DjangoObjectType):
    class Meta:
        model = Transaction
        interfaces = (Node,)

    @classmethod
    def get_queryset(cls, queryset, info):
        if info.context.user.is_anonymous:
            raise GraphQLError("Login Required")
        user = info.context.user
        return queryset.filter(user=user)


class TransactionQuery:
    transactions = DjangoConnectionField(TransactionNode)
    # transactions = graphene.List(TransactionNode)

    @staticmethod
    def resolve_transactions(parent, info):
        return Transaction.objects.all()

sfbaker7 avatar May 24 '20 04:05 sfbaker7

I am also running into this issue, if someone has found a solution, I would greatly appreciate it!

jjoocceeee avatar Jun 16 '20 19:06 jjoocceeee

This appears to have gone stale, however the use-case for custom queryset handlers on an object level is apparent. Has anyone given it a shot yet?

mathieusteele avatar May 06 '21 02:05 mathieusteele

This would definitely be nice to have

alexlambson avatar Apr 07 '22 23:04 alexlambson

If the method is never executed it should be removed from the docs.

The solution is to change the plain List type to a DjangoListLField https://github.com/graphql-python/graphene-django/issues/925#issuecomment-612605155

danihodovic avatar Apr 02 '24 14:04 danihodovic