2016-11-02 16 views
6

GraphQL API oluşturmak için graphen-django kullanıyorum. Bu API'yi başarıyla oluşturdum, ancak yanıtımı filtrelemek için bir argüman gönderemiyorum.graphene-django - Filtreleme nasıl yapılır?

Bu benim models.py geçerli: Bu benim schema.py

from django.db import models 

class Application(models.Model): 
    name = models.CharField("nom", unique=True, max_length=255) 
    sonarQube_URL = models.CharField("Url SonarQube", max_length=255, blank=True, null=True) 

    def __unicode__(self): 
    return self.name 

geçerli: modellerden graphene_django ithalat DjangoObjectType den ithalat grafin içe Uygulama

class Applications(DjangoObjectType): 
    class Meta: 
     model = Application 

class Query(graphene.ObjectType): 
    applications = graphene.List(Applications) 

    @graphene.resolve_only_args 
    def resolve_applications(self): 
     return Application.objects.all() 


schema = graphene.Schema(query=Query) 

Benim urls.py:Görebildiğiniz gibi bir REST API'm var.

Benim settings.py bu içerir:

GRAPHENE = { 
    'SCHEMA': 'tibco.schema.schema' 
} 

Bunu izleyin: https://github.com/graphql-python/graphene-django

Bu resquest gönderdiğinizde: Ben bu yanıtı var

{ 
    applications { 
    name 
    } 
} 

:

{ 
    "data": { 
    "applications": [ 
     { 
     "name": "foo" 
     }, 
     { 
     "name": "bar" 
     } 
    ] 
    } 
} 

Yani, işler!

Ama hiç böyle bir argüman iletilmesi çalıştığınızda i

{ 
    "errors": [ 
    { 
     "message": "Unknown argument \"name\" on field \"applications\" of type \"Query\".", 
     "locations": [ 
     { 
      "column": 16, 
      "line": 2 
     } 
     ] 
    } 
    ] 
} 

kaçırmış Ne:

{ 
    applications(name: "foo") { 
    name 
    id 
    } 
} 

ben bu yanıtı var? Ya da belki yanlış bir şey yapıyorum? Bu benim cevaptır http://docs.graphene-python.org/projects/django/en/latest/tutorial.html

:

cevap

7

ben bir çözüm sayesinde buldum. django-filtre (https://github.com/carltongibson/django-filter/tree/master):

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) 

Ardından, bir paket eksikti: Benim schema.py düzenleme var. Django filtresi, DjangoFilterConnectionField tarafından kullanılır.

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

ve tepki olacaktır:

Şimdi bunu yapabilirsiniz

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