[英]Calling filters in django-graphene with GraphQL
I've been following the documentation for Graphene-Python in Django, and have just made it to the section on custom filters. 我一直在关注Django中Graphene-Python的文档,并将其放入自定义过滤器部分。 While a tutorial is provided for how to write custom filters, there isn't a reference on how to call them in GraphiQL. 虽然提供了有关如何编写自定义过滤器的教程,但没有有关如何在GraphiQL中调用它们的参考。 If I have the following example code: 如果我有以下示例代码:
class AnimalNode(DjangoObjectType):
class Meta:
# Assume you have an Animal model defined with the following fields
model = Animal
filter_fields = ['name', 'genus', 'is_domesticated']
interfaces = (relay.Node, )
class AnimalFilter(django_filters.FilterSet):
# Do case-insensitive lookups on 'name'
name = django_filters.CharFilter(lookup_expr=['iexact'])
class Meta:
model = Animal
fields = ['name', 'genus', 'is_domesticated']
@property # make your own filter like this
def qs(self):
return super(EquityFilter, self).qs.filter(id=self.request.user)
class Query(ObjectType):
animal = relay.Node.Field(AnimalNode)
# We specify our custom AnimalFilter using the filterset_class param
all_animals = DjangoFilterConnectionField(AnimalNode,
filterset_class=AnimalFilter)
My question is, what would I need to type in GraphiQL to use this filter ? 我的问题是, 要使用此过滤器 , 我需要在GraphiQL中键入什么? Any help is greatly appreciated. 任何帮助是极大的赞赏。
Inspect the schema in GraphiQL. 在GraphiQL中检查模式。 It should show a root query similar to this one: 它应该显示类似于此查询的根查询:
allAnimals(
before:String,
after:String,
firts:Int,
last:Int,
name:String,
genus:String,
isDomesticated:Boolean
):AnimalNodeConnection
The three filter criteria are exposed as query parameters, so you can use them with a query like this one: 这三个过滤条件作为查询参数公开,因此您可以将其与类似这样的查询一起使用:
query filteredAnimals{
allAnimals(
name:"Big Foot",
genus:"Unknown",
isDomesticated:false
) {
edges {
node {
name
genus
isDomesticated
}
}
}
}
Which will give you a connection with undomesticated animals named "Big Foot" ("big FOOT", "Big foot", etc.) with genus equal to "Unknown". 这将使您与属属“未知”的未驯养动物“大脚”(“大脚”,“大脚”等)建立联系。
Note: Filters declared on the FilterSet
Meta class are named after the type of filtering they do, like name_Icontains
, name_Iexact
. 注意:在FilterSet
Meta类上声明的过滤器以它们执行的过滤类型命名,例如name_Icontains
, name_Iexact
。 Filters declared as FilterSet
fields (name filter in your case) keep their names unmodified, and extend or OVERRIDE filters declared in the FilterSet
Meta class. 声明为过滤器FilterSet
(在你的案件名称过滤器)领域保持自己的名字修改,并扩展或覆盖在声明过滤器FilterSet
元类。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.