21π
β
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"
}
}
]
}
}
}
π€Nevenoe
6π
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
π€elachere
- [Django]-Error: upstream prematurely closed connection while reading response header from upstream [uWSGI/Django/NGINX]
- [Django]-Django {% if forloop.first %} question
- [Django]-How do you join two tables on a foreign key field using django ORM?
4π
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
}
}
}
}
π€monofal
- [Django]-Django set DateTimeField to database server's current time
- [Django]-How to generate list of response messages in Django REST Swagger?
- [Django]-Django templates: Get current URL in another language
Source:stackexchange.com