2016-11-02 2 views
6

저는 GraphQL API를 빌드 할 때 graphen-django를 사용합니다. 이 API를 성공적으로 만들었지 만 응답을 필터링하기위한 인수를 전달할 수 없습니다.graphene-django - 필터링 방법?

이 내 models.py입니다 : 이것은 내 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 

입니다 : 모델에서 graphene_django 수입 DjangoObjectType 에서 수입 그래 핀 가져올 응용 프로그램

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) 

urls.py :

urlpatterns = [ 
    url(r'^', include(router.urls)), 
    url(r'^admin/', admin.site.urls), 
    url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')), 
    url(r'^api-token-auth/', authviews.obtain_auth_token), 
    url(r'^graphql', GraphQLView.as_view(graphiql=True)), 
] 

자세히 알 수 있듯이 REST API도 있습니다.

settings.py이 포함

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

나는이를 수행 https://github.com/graphql-python/graphene-django

나는이 resquest 보낼 때 :이 응답을 가지고

{ 
    applications { 
    name 
    } 
} 

을 :

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

그래서 작동합니다!

하지만이 같은 인수를 전달하려고하면 내가

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

을 놓친 무엇 :

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

을 나는이 반응을? 아니면 내가 뭔가 잘못한거야? 이것은 내 대답이다 http://docs.graphene-python.org/projects/django/en/latest/tutorial.html

:

답변

7

난에 대한 해결책 덕분에 찾았습니다. 장고 필터 (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) 

그런 다음,이 패키지를 잃어버린 : 내 schema.py 편집 할 수 있습니다. 장고 필터는 DjangoFilterConnectionField에서 사용됩니다.

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

및 응답은 다음과 같습니다 :

지금은이 작업을 수행 할 수 있습니다

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