小编ilm*_*rez的帖子

如何使用graphene-django通过GraphQL中的id列表过滤查询?

我正在尝试使用 Django 和 Graphene 执行 GraphQL 查询。要使用 id 查询单个对象,我执行了以下操作:

{
  samples(id:"U2FtcGxlU2V0VHlwZToxMjYw") {
    edges {
      nodes {
        name
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

它工作正常。当我尝试使用多个 id 进行查询时出现问题,如下所示:

{
  samples(id_In:"U2FtcGxlU2V0VHlwZToxMjYw, U2FtcGxlU2V0VHlwZToxMjYx") {
    edges {
      nodes {
        name
      }
    }
  }
} 
Run Code Online (Sandbox Code Playgroud)

在后一种情况下,我收到以下错误:

argument should be a bytes-like object or ASCII string, not 'list'
Run Code Online (Sandbox Code Playgroud)

这是如何定义类型和查询的草图 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()
Run Code Online (Sandbox Code Playgroud)

django-filter graphene-python graphql-python

7
推荐指数
2
解决办法
3825
查看次数