简体   繁体   English

使用 GraphQL 和石墨烯进行多级过滤

[英]Filter at multiple levels with GraphQL and Graphene

I'm using Django and Graphene and have multiple levels I'd like to filter on.我正在使用 Django 和 Graphene 并且有多个我想过滤的级别。 But I can't get past either "Unknown operation named \"undefined\"."但我无法通过“名为 \"undefined\" 的未知操作”。 or getting ALL objects at each level of the hierarchy (ie: ALL jobDetails for all jobs listed for EACH job).或在层次结构的每个级别获取所有对象(即:为每个作业列出的所有作业的所有作业详细信息)。

I'm trying to do this query:我正在尝试执行此查询:

query {
  allPushes(revision: "1ef73669e8fccac35b650ff81df1b575a39a0fd5") {
    edges {
      node {
        revision
        author
        jobs (result: "testfailed") {
          edges {
            node {
              result
              jobDetails (url_Iendswith: "errorsummary.log") {
                edges {
                  node {
                    url
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

In Django, jobDetails has a foreign key to jobs , which has a foreign key to pushes在 Django 中, jobDetails有一个外键jobs ,它有一个外键来pushes

My first attempt was to setup my nodes:我的第一次尝试是设置我的节点:

class JobDetailNode(DjangoObjectType):
    class Meta:
        model = JobDetail
        filter_fields = {
            'url': ('exact', 'icontains', 'iendswith')
        }
        interfaces = (relay.Node, )


class JobNode(DjangoObjectType):
    class Meta:
        model = Job
        filter_fields = ('result', 'tier')
        interfaces = (relay.Node, )

    job_details = DjangoFilterConnectionField(JobDetailNode)


class PushNode(DjangoObjectType):
    class Meta:
        model = Push
        filter_fields = ('revision', )
        interfaces = (relay.Node, )

    jobs = DjangoFilterConnectionField(JobNode)

But this returns, as I said, all jobDetails at for EACH job, not just the jobDetails that belong to that job.但是,正如我所说,这会返回每个作业的所有jobDetails ,而不仅仅是属于该作业的 jobDetails。

But if I remove those DjangoFilterConnectionField fields, then I can't filter at each level;但是,如果我删除了那些DjangoFilterConnectionField字段,那么我就无法在每个级别进行过滤; just the first.只是第一个。

My Query looks like this:我的查询如下所示:

class Query(ObjectType):
    all_pushes = DjangoFilterConnectionField(PushNode)
    all_jobs = DjangoFilterConnectionField(JobNode)
    all_job_details = DjangoFilterConnectionField(JobDetailNode)

    def resolve_all_pushes(self, args):
        return Push.objects.filter(**args)

    def resolve_all_jobs(self, args):
        return Job.objects.filter(**args)

    def resolve_all_job_details(self, args):
        return JobDetail.objects.filter(**args)

Any suggestions on how to setup filtering at multiple levels like this?关于如何在多个级别设置过滤的任何建议? Thanks in advance!!提前致谢!!

This is a bug in graphene-django. 这是graphene-django中的错误。 It was fixed in version 1.3. 它已在1.3版中修复。 Changelog 更新日志

Regards. 问候。

I'm not sure if the same person asked the question on graphene github page, but it looks almost, if not exactly the same.我不确定是否同一个人在石墨烯 github页面上提出了这个问题,但它看起来几乎,如果不完全相同的话。 Apart from the fact mentioned bug existed, you still need some improvement in your code.除了存在提到的错误之外,您仍然需要对代码进行一些改进。

In order to make it work as you described (getting only related JobDetail for each Job ) you have to define a resolver inside JobNode :为了使它像您描述的那样工作(只为每个Job获取相关的JobDetail ),您必须在JobNode中定义一个解析器:

def resolve_job_details(self, args, context, info):
    return JobDetail.objects.filter(job=self, **args)

Of course you need also Job resolver inside PushNode :当然,您还需要PushNode内的Job解析器:

def resolve_jobs(self, args, context, info):
    return Job.objects.filter(push=self, **args)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM