[英]Graphene/Django (GraphQL): How to use a query argument in order to exclude nodes matching a specific filter?
I have some video items in a Django/Graphene
backend setup.我在
Django/Graphene
后端设置中有一些视频项目。 Each video item is linked to one owner.每个视频项目都链接到一个所有者。 In a React app, I would like to query via GraphQL all the videos owned by the current user on the one hand and all the videos NOT owned by the current user on the other hand.
在 React 应用程序中,我想一方面通过 GraphQL 查询当前用户拥有的所有视频,另一方面查询当前用户不拥有的所有视频。
I could run the following GraphQl
query and filter on the client side:我可以在客户端运行以下
GraphQl
查询和过滤器:
query AllScenes {
allScenes {
edges {
node {
id,
name,
owner {
name
}
}
}
}
}
I would rather have two queries with filters parameters directly asking relevant data to my backend.我宁愿有两个带有过滤器参数的查询,直接向我的后端询问相关数据。 Something like:
类似的东西:
query AllScenes($ownerName : String!, $exclude: Boolean!) {
allScenes(owner__name: $ownerName, exclude: $exclude) {
edges {
node {
id,
name,
owner {
name
}
}
}
}
}
I would query with ownerName = currentUserName
and exclude = True/False
yet I just cannot retrieve my exclude
argument on my backend side.我会使用
ownerName = currentUserName
和exclude = True/False
查询,但我无法在后端检索我的exclude
参数。 Here is the code I have tried in my schema.py file:这是我在 schema.py 文件中尝试过的代码:
from project.scene_manager.models import Scene
from graphene import ObjectType, relay, Int, String, Field, Boolean, Float
from graphene.contrib.django.filter import DjangoFilterConnectionField
from graphene.contrib.django.types import DjangoNode
from django_filters import FilterSet, CharFilter
class SceneNode(DjangoNode):
class Meta:
model = Scene
class SceneFilter(FilterSet):
owner__name = CharFilter(lookup_type='exact', exclude=exclude)
class Meta:
model = Scene
fields = ['owner__name']
class Query(ObjectType):
scene = relay.NodeField(SceneNode)
all_scenes = DjangoFilterConnectionField(SceneNode, filterset_class=SceneFilter, exclude=Boolean())
def resolve_exclude(self, args, info):
exclude = args.get('exclude')
return exclude
class Meta:
abstract = True
My custom SceneFilter
is used but I do not know how to pass the exclude
arg to it.使用了我的自定义
SceneFilter
,但我不知道如何将exclude
arg 传递给它。 (I do not think that I am making a proper use of the resolver ). (我认为我没有正确使用解析器)。 Any help on that matter would be much appreciated!
对此问题的任何帮助将不胜感激!
Switching to graphene-django 1.0, I have been able to do what I wanted with the following query definition:切换到 graphene-django 1.0,我已经能够使用以下查询定义做我想做的事情:
class Query(AbstractType):
selected_scenes = DjangoFilterConnectionField(SceneNode, exclude=Boolean())
def resolve_selected_scenes(self, args, context, info):
owner__name = args.get('owner__name')
exclude = args.get('exclude')
if exclude:
selected_scenes = Scene.objects.exclude(owner__name=owner__name)
else:
selected_scenes = Scene.objects.filter(owner__name=owner__name)
return selected_scenes
BossGrand proposed an other solution on GitHub BossGrand 在 GitHub 上提出了另一种解决方案
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.