graphene-django - How to filter?

If you're in my case and don't want to use Relay, you can also handle filtering directly in you resolvers using Django orm filtering. Example here: Filter graphql query in django


Little addition to Adrien Answer. If you want to perform different operation while filtering like contains and exact match then edit your schema.py

class ApplicationNode(DjangoObjectType):
    class Meta:
        model = Application
        # Provide more complex lookup types
        filter_fields = {
            'name': ['exact', 'icontains', 'istartswith']
        }
        interfaces = (relay.Node, )

and you can write the query like this

  query {
  allApplications(name_Icontains: "test") {
    edges {
      node {
        id,
        name
      }
    }
  }
}

I've found a solution thanks to: https://docs.graphene-python.org/projects/django/en/latest/

This is my answer. I have edit my schema.py:

import graphene
from graphene import relay, AbstractType, ObjectType
from graphene_django import DjangoObjectType
from graphene_django.filter import DjangoFilterConnectionField
from models import Application

class ApplicationNode(DjangoObjectType):
    class Meta:
        model = Application
        filter_fields = ['name', 'sonarQube_URL']
        interfaces = (relay.Node, )

class Query(ObjectType):
    application = relay.Node.Field(ApplicationNode)
    all_applications = DjangoFilterConnectionField(ApplicationNode)

schema = graphene.Schema(query=Query)

Then, it was missing a package: django-filter (https://github.com/carltongibson/django-filter/tree/master). Django-filter is used by DjangoFilterConnectionField.

Now I can do this:

query {
  allApplications(name: "Foo") {
    edges {
      node {
        name
      }
    }
  }
}

and the response will be:

{
  "data": {
    "allApplications": {
      "edges": [
        {
          "node": {
            "name": "Foo"
          }
        }
      ]
    }
  }
}