Graphql Symfony API Platform Assert\Choice在setter上不起作用

Graphql Symfony API Platform Assert\Choice在setter上不起作用,graphql,symfony4,api-platform.com,Graphql,Symfony4,Api Platform.com,我的应用程序有一个非常标准的用户评论功能,任何访问者都有可能将评论标记为不合适 在这些标记的评论的解决过程中,管理员将决定在不采取任何行动的情况下,编辑或删除评论 CommentReport实体包含Assert\Choice,仅允许上述3个选项 问题是它正在接受任何字符串 在注释实体上,设置如下: .... * graphql={ * "withCustomArgsMutation"={ * "secur

我的应用程序有一个非常标准的用户评论功能,任何访问者都有可能将评论标记为不合适

在这些标记的评论的解决过程中,管理员将决定在不采取任何行动的情况下,编辑或删除评论

CommentReport实体包含Assert\Choice,仅允许上述3个选项

问题是它正在接受任何字符串

在注释实体上,设置如下:

....

 *      graphql={
 *          "withCustomArgsMutation"={
 *              "security"="is_granted('ROLE_ADMIN')",
 *              "mutation"=CommentReputationTypeResolver::class,
 *              "args"={
 *                  "id"={"type"="ID!"},
 *                  "resolutionType"={"type"="String!"},
 *              },
 *              "denormalization_context"={"groups"={"put-resolution"}},
 *              "normalization_context"={"groups"={"get"}}
 *          }, 
 .....

 /**
  * @ORM\OneToMany(targetEntity=CommentReport::class, mappedBy="comment")
  * @Groups({"get", "get-admin", "get-owner", "put-resolution"})
  */
 private $commentReports;
然后,CommentReports实体:

 class CommentReport
{
 const RESOLUTION_TYPES = [
    'no_action',
    'edit',
    'remove',
 ];
 .....

/**
 * @ORM\Column(type="string", length=10, nullable=true)
 * @Assert\NotBlank(
 *      message="commentReport.resolutionType.notBlank",
 *      groups={"put-resolution"}
 * )
 * @Assert\Choice(
 *      choices=CommentReport::RESOLUTION_TYPES,
 *      message="commentReport.resolutionType.choice",
 *      groups={"put-resolution"}
 * )
 * @Groups({"get-admin", "put-resolution"})
*/
private $resolutionType;
然后在反序列化阶段拦截请求,以处理针对该评论所做的每个单独CommentReport的设置:

/Stage/DeserializeStage.php

        if ($this->isAdmin($user) && $resourceClass === 'App\Entity\Comment' && $operationName === 'withCustomArgsMutation') {
        // get all comment reports
        $commentReports = $deserializeObject->getCommentReports();
        foreach($commentReports as $report) {
            $report->setResolutionType($context['args']['input']['resolutionType']);
        }
    }
所有这些工作,但我可以输入任何字符串,它将被接受。 当然,预期的结果是Symfony将对RESOLUTION_TYPES数组之外的任何字符串抛出异常

关于信息,这是触发输入的前端突变:

 `mutation putCommentReport(
    $commentId: ID!
    $resolutionType: String!
    ) {
    withCustomArgsMutationComment(input: {
        id: $commentId
        resolutionType: $resolutionType
    }){
        comment{
            id
        }
    }
}`;

正如@Jeroen van der Laan指出的,操作中缺少验证组注释,如下所示

应用程序\实体\注释

  *      graphql={
  *          "withCustomArgsMutation"={
  *              "security"="is_granted('ROLE_ADMIN')",
  *              "mutation"=CommentReputationTypeResolver::class,
  *              "args"={
  *                  "id"={"type"="ID!"},
  *                  "resolutionType"={"type"="String!"},
  *              },
  *              "normalization_context"={"groups"={"get"}},
  *              "validation_groups"={"put-resolution"}
  *          },
此外,Valid()断言需要添加到Comment上的CommentReport关系中:

/**
 * @ORM\OneToMany(targetEntity=CommentReport::class, mappedBy="comment")
 * @Assert\Valid() 
 * @Groups({"get", "get-admin", "get-owner"})
 */
private $commentReports;

似乎未使用
put resolution
验证组,应该使用该组。序列化组(在
normalization\u context
nonormalization\u context
下配置)与验证组无关。感谢您为我指明了正确的方向。除了
validation\u group
注释外,还需要将
Valid()
断言添加到关系中。