在 API Platform 中,可以使用 exists 过滤器检查某个资源是否存在关联资源。在 GraphQL API 中,可以使用以下参数来设置 exists 过滤器:
query {
books(
filter: {
authorExists: true # 设置 exists 过滤器
}
) {
edges {
node {
id
title
}
}
}
}
上述 GraphQL 查询中的 filter 参数指定了要过滤的字段(这里为 author),以及要使用的过滤器(这里为 authorExists)。设置 authorExists 参数为 true,以表示要检查 book 资源是否存在 author 资源。
要在 API Platform 中启用 exists 过滤器,请在您的资源类定义中添加 @ApiFilter(ExistsFilter::class) 注释:
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\ExistsFilter;
use ApiPlatform\Core\Annotation\ApiFilter;
/**
* @ApiFilter(ExistsFilter::class, properties={"author"})
*/
class Book {
// ...
}
上述 PHP 代码使用 properties 参数指定要启用 exists 过滤器的字段名(这里为 author)。这将使 API Platform 自动为此资源启用 exists 过滤器。