Files
animethemes-server/app/GraphQL/Definition/Mutations/User/UnlikeMutation.php
T

106 lines
2.3 KiB
PHP

<?php
declare(strict_types=1);
namespace App\GraphQL\Definition\Mutations\User;
use App\Contracts\GraphQL\Fields\DeletableField;
use App\GraphQL\Attributes\UseField;
use App\GraphQL\Controllers\User\LikeController;
use App\GraphQL\Definition\Fields\Field;
use App\GraphQL\Definition\Mutations\BaseMutation;
use App\GraphQL\Definition\Types\User\LikeType;
use App\GraphQL\Definition\Unions\LikedUnion;
use App\Models\User\Like;
use GraphQL\Type\Definition\Type;
/**
* Class LikeMutation.
*/
#[UseField(LikeController::class, 'destroy')]
class UnlikeMutation extends BaseMutation
{
/**
* Create a new mutation instance.
*/
public function __construct()
{
parent::__construct('unlike');
}
/**
* The description of the mutation.
*
* @return string
*/
public function description(): string
{
return 'Unlike a model';
}
/**
* Get the arguments for the unlike mutation.
*
* @return string[]
*/
public function arguments(): array
{
$type = new LikeType();
return $this->resolveBindArgument($type->fields(), false);
}
/**
* The directives of the mutation.
*
* @return array<string, array>
*/
public function directives(): array
{
return [
'canModel' => [
'ability' => 'delete',
'injectArgs' => true,
'model' => Like::class,
],
...parent::directives(),
];
}
/**
* Get the rules for the create mutation.
*
* @param array<string, mixed> $args
* @return array<string, array>
*/
public function rules(array $args): array
{
$type = new LikeType();
return collect($type->fields())
->filter(fn (Field $field) => $field instanceof DeletableField)
->mapWithKeys(fn (Field&DeletableField $field) => [$field->getColumn() => $field->getDeleteRules($args)])
->toArray();
}
/**
* The base return type of the mutation.
*
* @return Type
*/
public function baseType(): Type
{
return new LikedUnion();
}
/**
* The type returned by the field.
*
* @return Type
*/
public function getType(): Type
{
return Type::nonNull($this->baseType());
}
}