graphene
graphene copied to clipboard
DjangoObjectType custom get_queryset is not hitting
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()
I am also running into this issue, if someone has found a solution, I would greatly appreciate it!
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?
This would definitely be nice to have
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