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.

Custom argument on DjangoObjectType

See original GitHub issue

Hi all 👋🏻,

I’ve seen #333 but still have a question about how to add a custom argument that’d enable a more complex filter on a DjangoObjectType.

Using graphene-django==2.8.0.

class ThingNode(DjangoObjectType):
    class Meta:
        model = Thing
        interfaces = (graphene.relay.Node,)

    @classmethod
    def get_queryset(cls, queryset, info):
        # How do I access my_custom_param here to enable a custom filter?
        if my_custom_param:
            # Heavily modify the queryset here with filter + exclude
            queryset.filter().filter().exclude()
        else:
            # Something else
            queryset.filter().filter()
        return queryset


class Query(graphene.ObjectType):

    things = DjangoFilterConnectionField(ThingNode, my_custom_param=graphene.Boolean())

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:5

github_iconTop GitHub Comments

3reactions
msukmanowskycommented, Jan 17, 2020

Did find a way to support this but it feels like I’m dipping into internals that I shouldn’t be?

class ThingNode(DjangoObjectType):
    class Meta:
        model = Thing
        interfaces = (graphene.relay.Node,)

    @classmethod
    def get_queryset(cls, queryset, info):
        field_asts = info.field_asts
        if field_asts:
            args = field_asts[0].arguments
            my_custom_param = [a for a in args if a.name.value == "myCustomParam"]
            my_custom_param = my_custom_param[0].value.value if my_custom_param else False
        else:
            my_custom_param = False

        if my_custom_param:
            # Heavily modify the queryset here with filter + exclude
            queryset.filter().filter().exclude()
        else:
            # Something else
            queryset.filter().filter()
        return queryset


class Query(graphene.ObjectType):

    things = DjangoFilterConnectionField(ThingNode, my_custom_param=graphene.Boolean())
1reaction
pythonkdzencodecommented, Jun 4, 2021

@msukmanowsky, Thank you! Your answer is the solution i was looking for.

Read more comments on GitHub >

github_iconTop Results From Across the Web

A custom argument in graphene-django - Stack Overflow
How do I create a custom argument in GraphQL with graphene-django? I currently have this configuration in my schema.py: class Post( ...
Read more >
Queries & ObjectTypes - Graphene-Python
This allows you to provide input arguments in your GraphQL server and can be useful for custom queries. import graphene from .models import...
Read more >
Django + Graphene: From REST to GraphQL - FullStack Labs
player/types.py from graphene_django.types import DjangoObjectType from ... Mutation): class Arguments: # The input arguments for this mutation id ...
Read more >
How to use the graphene.Float function in graphene - Snyk
DjangoObjectType ): class Meta(object): model = HasRead class Query(graphene. ... ObjectType): """ Querry for the calendar events and accepts the arguments ...
Read more >
graphene-django-extras - PyPI
Argument (UserInput) class Meta: description = " Graphene traditional ... You must add the directives param with your custom directives to your schema ......
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