问题描述
我正在使用 QueryItemExtensionInterface
(带有 API 平台、GraphQL)来防止用户查看他们无权访问的对象。在查询集合中,项目按预期从结果中删除,但在查询特定对象 (item_query
) 时,结果是值为 NULL
的对象。
我希望得到“拒绝访问”响应而不是空数据值(就像我在使用 security
api-platform 属性时所做的那样)。当在查询扩展中强制实施安全性(实现 applyToItem()
)时,我是否可以将我的实体配置为以这种方式行事?
解决方法
查询扩展影响查询创建(sql),它不是为了结果“验证”。
您可以使用解析器阶段 - https://api-platform.com/docs/core/graphql/#workflow-of-the-resolvers - 例如
'item_query' => [
'security' => "is_granted('ROLE_USER')",'security_post_denormalize' => 'object != null'
],