question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Default QuerySet for DjangoObjectType has no effect with graphene.List

See original GitHub issue

The documentation describes the use of a default QuerySet on ObjectType level.

The unit test for this functionality makes use of DjangoConnectionField.

Using graphene.List instead ignores the existence of the default QuerySet.

class CurrencyType(DjangoObjectType):
    class Meta:
        model = Currency

    @classmethod
    def get_queryset(cls, queryset, info):
        print(cls)
        print(queryset)
        return queryset.filter(symbol__isnull=False)
all_currencies = graphene.List(CurrencyType)

def resolve_all_currencies(self, info):
    return Currency.objects.all()

The get_queryset method is never called, and the filter is not applied.

Please refer to #698 for some history on this.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:2
  • Comments:13 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
jkimbocommented, Apr 12, 2020

Something that still doesn’t work as I hoped is the following: When Currency from my example is a related field in another table, let’s say Fund, a query for funds does not call get_queryset in the CurrencyType class.

This issue should actually be fixed as part of my PR. I’ll add a test to confirm.

1reaction
porscommented, Apr 12, 2020

Awesome @jkimbo! I have it working now for the example I provided (I don’t need a resolver on the query level here). I think it actually makes sense as it is called the “Default QuerySet”.

Something that still doesn’t work as I hoped is the following: When Currency from my example is a related field in another table, let’s say Fund, a query for funds does not call get_queryset in the CurrencyType class.

If that would be possible it provides an easy way to protect from returning too much data for specific tables (this is especially important for the User table being a related field).

Read more comments on GitHub >

github_iconTop Results From Across the Web

graphene-django custom get_queryset of DjangoObjectType ...
For anyone looking for the answer to this question, it is answered here. Try swapping out List for DjangoConnectionField.
Read more >
Queries & ObjectTypes - Graphene-Python
By default, DjangoObjectType will present all fields on a Model through GraphQL. If you only want a subset of fields to be present,...
Read more >
Django + Graphene: From REST to GraphQL - FullStack Labs
The truth is not only can you use GraphQL on top of your current Django app but ... The DjangoObjectType by default includes...
Read more >
Build a Blog Using Django, Vue, and GraphQL - Real Python
This tutorial is structured as a step-by-step project so you can learn in a ... Import Graphene-Django's DjangoObjectType , your blog models, and...
Read more >
Step-by-step guide to use GraphQL with Django - Bird Eats Bug
As previously said, every query must have a corresponding resolve function, else Graphene will have no idea what to return. To return the ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found