我正在尝试使用Django和Graphene执行一个GraphQL查询。为了使用id查询一个单独的对象,我执行了以下操作:
{
samples(id:"U2FtcGxlU2V0VHlwZToxMjYw") {
edges {
nodes {
name
}
}
}
}而且它工作得很好。当我尝试使用多个id进行查询时,会出现问题,如下所示:
{
samples(id_In:"U2FtcGxlU2V0VHlwZToxMjYw, U2FtcGxlU2V0VHlwZToxMjYx") {
edges {
nodes {
name
}
}
}
} 在后一种情况下,我得到以下错误:
argument should be a bytes-like object or ASCII string, not 'list'这是如何在django-graphene中定义类型和查询的草图
class SampleType(DjangoObjectType):
class Meta:
model = Sample
filter_fields = {
'id': ['exact', 'in'],
}
interfaces = (graphene.relay.Node,)
class Query(object):
samples = DjangoFilterConnectionField(SampleType)
def resolve_sample_sets(self, info, **kwargs):
return Sample.objects.all()发布于 2020-03-21 13:11:17
来自django-graphene的GlobalIDMultipleChoiceFilter解决了这个问题,如果你在字段名中加上" in“。您可以像这样创建过滤器
from django_filters import FilterSet
from graphene_django.filter import GlobalIDMultipleChoiceFilter
class BookFilter(FilterSet):
author = GlobalIDMultipleChoiceFilter()并通过以下方式使用它
{
books(author: ["<GlobalID1>", "<GlobalID2>"]) {
edges {
nodes {
name
}
}
}
}仍然不完美,但对自定义代码的需求已降至最低。
发布于 2019-08-14 06:53:19
我在实现“in”过滤器时也遇到了问题--它现在似乎在graphene-django中实现了错误,并且没有像预期的那样工作。以下是使其工作的步骤:
from base64 import b64decode
def get_pk_from_node_id(node_id: str):
"""Gets pk from node_id"""
model_with_pk = b64decode(node_id).decode('utf-8')
model_name, pk = model_with_pk.split(":")
return pk
class SampleType(DjangoObjectType):
class Meta:
model = Sample
filter_fields = {
'id': ['exact'],
}
interfaces = (graphene.relay.Node,)
class Query(object):
samples = DjangoFilterConnectionField(SampleType, id__in=graphene.List(graphene.ID))
def resolve_samples(self, info, **kwargs):
# filter_field for 'in' seems to not work, this hack works
id__in = kwargs.get('id__in')
if id__in:
node_ids = kwargs.pop('id__in')
pk_list = [get_pk_from_node_id(node_id) for node_id in node_ids]
return Sample._default_manager.filter(id__in=pk_list)
return Sample._default_manager.all()这将允许您使用以下api调用过滤器。注意在签名中使用实际的数组(我认为这是一个比发送逗号分隔的值字符串更好的API )。这个解决方案仍然允许您将其他过滤器添加到请求中,并且它们将正确地链接在一起。
{
samples(id_In: ["U2FtcGxlU2V0VHlwZToxMjYw", "U2FtcGxlU2V0VHlwZToxMjYx"]) {
edges {
nodes {
name
}
}
}
} 发布于 2020-05-03 11:58:35
您可以很容易地使用过滤器,只需将其与您的节点放在一起即可。
class ReportFileFilter(FilterSet):
id = GlobalIDMultipleChoiceFilter()然后在您的查询中使用-
class Query(graphene.ObjectType):
all_report_files = DjangoFilterConnectionField(ReportFileNode, filterset_class=ReportFileFilter)这是graphql django的中继实现。
https://stackoverflow.com/questions/54218505
复制相似问题