0

I am using nuwave/lighthouse:^5.0, and I am trying to create a mutation for an entity which have a belongsTo relationship. The thing is that in my input I am using a sanitizer directive to transform from string to id, but after that when Laravel gets the properties, it shows errors with the validation of the class. In addition, I debug the directive code and it works correctly.

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);
    }

Any idea will be appreciated

1 Answers1

0

Finally, after some days, I found the issue.

The error come from the main Input definition:

input CreateContentInput {
     content_type: CreateContentTypeBelongsTo!
}

I was following a company standard that says that we need to use the properties always in ** snake case ** although they are relationships. So looks like Lighthouse uses always ** camel case ** for relationships.

The solution was add the ** rename ** property to the input. So the right input should be:

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

I hope this could help someone else.