Python 石墨烯django-如何过滤?
我使用GraphenDjango构建GraphQLAPI。 我已经成功地创建了这个API,但是我不能传递一个参数来过滤我的响应 这是我的型号。py:Python 石墨烯django-如何过滤?,python,django,graphql,graphene-python,Python,Django,Graphql,Graphene Python,我使用GraphenDjango构建GraphQLAPI。 我已经成功地创建了这个API,但是我不能传递一个参数来过滤我的响应 这是我的型号。py: from django.db import models class Application(models.Model): name = models.CharField("nom", unique=True, max_length=255) sonarQube_URL = models.CharField("Url SonarQ
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
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)),
]
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)
这是我的模式。py:
进口石墨烯
从graphene_django导入DjangObjectType
从模型导入应用程序
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)
我的URL.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
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)),
]
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)
如您所见,我还有一个RESTAPI
Mysettings.py包含以下内容:
GRAPHENE = {
'SCHEMA': 'tibco.schema.schema'
}
我谨此陈辞:
当我发送此请求时:
{
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
}
}
我有以下回应:
{
"data": {
"applications": [
{
"name": "foo"
},
{
"name": "bar"
}
]
}
}
{
"errors": [
{
"message": "Unknown argument \"name\" on field \"applications\" of type \"Query\".",
"locations": [
{
"column": 16,
"line": 2
}
]
}
]
}
我错过了什么?或者我做错了什么?多亏了以下几点,我找到了解决方案: 这是我的答案。我已编辑了我的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
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)),
]
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)
然后,它丢失了一个包:django filter()。
Django筛选器由DjangoFilterConnectionField使用
现在我可以做到:
query {
allApplications(name: "Foo") {
edges {
node {
name
}
}
}
}
答复如下:
{
"data": {
"allApplications": {
"edges": [
{
"node": {
"name": "Foo"
}
}
]
}
}
}
如果您在我的情况下不想使用中继,您也可以使用Django orm过滤直接在解析器中处理过滤。这里的例子:对阿德里安的答案增加很少。如果要在过滤时执行不同的操作,如包含和精确匹配,请编辑您的schema.py
class ApplicationNode(DjangoObjectType):
class Meta:
model = Application
# Provide more complex lookup types
filter_fields = {
'name': ['exact', 'icontains', 'istartswith']
}
interfaces = (relay.Node, )
您可以这样编写查询
query {
allApplications(name_Icontains: "test") {
edges {
node {
id,
name
}
}
}
}
在另一个通用的声音
DjangoListField