简体   繁体   English

如何使用Symfony和API平台过滤子对象上的GraphQL?

[英]How to filter GraphQL on subobject with Symfony and API Platform?

Although an experienced developer, I'm fairly new to Symfony/API Platfrom/GraphQL. 尽管是一位经验丰富的开发人员,但我对Symfony / API Platfrom / GraphQL还是很陌生。 My task is simple - add a filter to the Product entity that allows to filter out only active products. 我的任务很简单-向Product实体添加一个过滤器,该过滤器仅允许过滤出有效产品。 Bonus points if the filter is set by default - that is, by default only active products are returned, and you need to add a parameter if you wish to see the inactive products. 如果默认情况下设置了过滤器,则奖励积分-也就是说,默认情况下,仅返回有效产品,如果您希望查看无效产品,则需要添加参数。 But if that's too hard, simply adding a filter will do. 但这太难了,只需添加一个过滤器即可。

OK, so in my Product.php I have (only relevant parts shown): 好,所以在我的Product.php我只显示了相关部分:

use Doctrine\ORM\Mapping as ORM;
use ApiPlatform\Core\Annotation\ApiFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\BooleanFilter;

/**
 * @ApiFilter(BooleanFilter::class, properties={"active" : "exact"})
 */
class Product {
    /**
     * @ORM\Column(type="boolean")
     */
    private $active;
}

Then I try to modify an existing query to filter out only the active products: 然后,我尝试修改现有查询以仅过滤出有效产品:

{
  sellers(id: 5, active: true) {
    edges {
      node {
        categories {
          edges {
            node {
              products (active: true) {
                totalCount
                edges {
                  node {
                    name
                }
              }
            }
          }
        }
      }
    }
  }
}

Buuuut, I get: Buuuut,我得到:

{
  "errors": [
    {
      "message": "Unknown argument \"active\" on field \"products\" of type \"Category\".",
      "category": "graphql",
      "locations": [
        {
          "line": 39,
          "column": 25
        }
      ]
    }
  ]
}

Now, if I change it to querying the products directly: 现在,如果我将其更改为直接查询产品:

{
  products (active: true) {
    totalCount
    edges {
      node {
        name
      }
    }
  }
}

That works! 这样可行!

So, what's going on? 发生什么了? I'd like to make the filter on the original query so it returns all of the other data too, not just plain products. 我想对原始查询进行过滤,以便它也返回所有其他数据,而不仅仅是普通产品。 What am I doing wrong? 我究竟做错了什么? It feels like I'm misunderstanding some fundamental concept. 感觉就像我误解了一些基本概念。

I think it was a pernicious bug which has been resolved in the master and 2.4 branches (no release yet). 我认为这是一个有害的错误,已在master和2.4分支(尚未发布)中解决。 See https://github.com/api-platform/core/pull/2970 for details. 有关详细信息,请参见https://github.com/api-platform/core/pull/2970

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

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