Graphene/Django (GraphQL): How to use a query argument in order to exclude nodes matching a specific filter?
<p>I have some video items in a Django/Graphene backend setup. Each video item is linked to one owner.
In a React app, I would like to query via GraphQL all the videos owned by the current user on the one hand and all the videos NOT owned by the current user on the other hand.</p>

<p>I could run the following GraphQl query and filter on the client side:</p>

<pre><code>query AllScenes {
  allScenes {
    edges {
      node {
        id,
        name,
        owner {
          name
        }
      }
    }
  }
}
</code></pre>

<p>I would rather have two queries with filters parameters directly asking relevant data to my backend. Something like: </p>

<pre><code>query AllScenes($ownerName : String!, $exclude: Boolean!) {
  allScenes(owner__name: $ownerName, exclude: $exclude) {
    edges {
      node {
        id,
        name,
        owner {
          name
        }
      }
    }
  }
}
</code></pre>

<p>I would query with <code>ownerName = currentUserName</code> and <code>exclude = True/False</code> yet I just cannot retrieve my <code>exclude</code> argument on my backend side. Here is the code I have tried in my schema.py file:</p>

<pre><code>from project.scene_manager.models import Sceneâ¨
from graphene import ObjectType, relay, Int, String, Field, Boolean, Floatâ¨
from graphene.contrib.django.filter import DjangoFilterConnectionField
from graphene.contrib.django.types import DjangoNodeâ¨
from django_filters import FilterSet, CharFilter
â¨â¨â¨

class SceneNode(DjangoNode):
â¨    class Meta:â¨
        model = Scene


â¨â¨â¨class SceneFilter(FilterSet):â¨
    owner__name = CharFilter(lookup_type='exact', exclude=exclude)
â¨
    class Meta:â¨
        model = Sceneâ¨
        fields = ['owner__name']â¨â¨

class Query(ObjectType):â¨â¨
    scene = relay.NodeField(SceneNode)â¨
    all_scenes = DjangoFilterConnectionField(SceneNode, filterset_class=SceneFilter, exclude=Boolean())â¨â¨

    def resolve_exclude(self, args, info):â¨
        exclude = args.get('exclude')â¨
        return excludeâ¨â¨â¨

    class Meta:â¨
        abstract = True
</code></pre>

<p>My custom <code>SceneFilter</code> is used but I do not know how to pass the <code>exclude</code> arg to it. (I do not think that I am making a proper use of the <a href="http://graphene-python.readthedocs.io/en/latest/types/objecttypes/" rel="nofollow">resolver</a>). Any help on that matter would be much appreciated!</p>

<p>Switching to graphene-django 1.0, I have been able to do what I wanted with the following query definition:</p>

<pre><code>class Query(AbstractType):

    selected_scenes = DjangoFilterConnectionField(SceneNode, exclude=Boolean())

    def resolve_selected_scenes(self, args, context, info):
        owner__name = args.get('owner__name')
        exclude = args.get('exclude')
        if exclude:
            selected_scenes = Scene.objects.exclude(owner__name=owner__name)
        else:
            selected_scenes = Scene.objects.filter(owner__name=owner__name)
        return selected_scenes
</code></pre>

<p>BossGrand proposed <a href="https://github.com/graphql-python/graphene/issues/305#issuecomment-250371352" rel="nofollow">an other solution</a> on GitHub</p>

python django graphql graphene-python