graphql-python / graphql-python/graphene-django

DjangoObjectType custom get_queryset is not hitting

Open
#1,526 5 comments 5 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
4.4k
Forks
760
PR merge metrics
No merged PRs in 30d

Description

Hey. I was attempting to add a global filter on some DjangoObjectTypes [(from docs)](https://docs.graphene-python.org/projects/django/en/latest/authorization/#global-filtering), but the "get_queryset" method was not hit when I was using graphene.List

I found [this issue](https://github.com/graphql-python/graphene-django/issues/698) 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()

```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.