【问题标题】:Error in mutation nuwave/lighthouse:^5.0 and input突变 nuwave/lighthouse:^5.0 和输入错误
【发布时间】:2022-01-14 13:15:44
【问题描述】:

我正在使用 nuwave/lighthouse:^5.0,并且我正在尝试为具有 belongsTo 关系的实体创建一个突变。问题是,在我的输入中,我使用 sanitizer 指令从字符串转换为 id,但之后当 Laravel 获取属性时,它会显示类验证错误。另外,我调试了指令代码,它工作正常。

Error
 "errors": [
        {
            "message": "The given data was invalid.",
            "extensions": {
                "validation": {
                    "content_type_id": [
                        "The content type id field is required."
                    ]
                },
                "category": "validation"
            },

Input 
input CreateContentInput {
    content_type: CreateContentTypeBelongsTo!
.....

input CreateContentTypeBelongsTo {
    connect: ID! @typeuuid(model: "App\\ContentType")
    create: CreateContentTypeInput
    update: UpdateContentTypeInput
}

Model
class Content extends Model
{
    protected $rules = [
        'content_type_id' => 'required|integer|is_main_content_type',
    ];

    /**
     * @return BelongsTo
     */
    public function contentType(): BelongsTo
    {
        return $this->belongsTo(ContentType::class);
    }

任何想法将不胜感激

【问题讨论】:

    标签: graphql laravel-7 laravel-lighthouse


    【解决方案1】:

    终于,几天后,我发现了问题。

    错误来自主输入定义:

    input CreateContentInput {
         content_type: CreateContentTypeBelongsTo!
    }
    

    我遵循公司标准,该标准说我们需要始终在 **snake case ** 中使用属性,尽管它们是关系。所以看起来 Lighthouse 总是使用 ** camel case ** 来表示关系。

    解决方案是将 ** rename ** 属性添加到输入中。所以正确的输入应该是:

    input CreateContentInput {
         content_type: CreateContentTypeBelongsTo! @rename (attribute: "contentType")
    }
    

    我希望这可以帮助其他人。

    【讨论】:

      猜你喜欢
      • 2020-09-10
      • 2020-05-11
      • 2020-12-25
      • 2020-11-20
      • 2020-08-18
      • 2020-05-01
      • 2020-03-30
      • 2020-12-15
      • 1970-01-01
      相关资源
      最近更新 更多