feat(graphql): handle insert song (#914)

This commit is contained in:
Kyrch
2025-08-06 14:35:54 -03:00
committed by GitHub
parent 1b5f42e601
commit 5501526a1d
9 changed files with 72 additions and 10 deletions
+4 -2
View File
@@ -5,17 +5,19 @@ declare(strict_types=1);
namespace App\Enums\Models\Wiki;
use App\Concerns\Enums\LocalizesName;
use App\GraphQL\Attributes\Hidden;
use Filament\Support\Contracts\HasLabel;
use GraphQL\Type\Definition\Description;
enum ThemeType: int implements HasLabel
{
use LocalizesName;
#[Description('Opening')]
case OP = 0;
#[Description('Ending')]
case ED = 1;
#[Hidden]
#[Description("Insert Song\n\nNote: Not retrieved by default, include it in the type_in argument to do so.")]
case IN = 2;
}
@@ -53,10 +53,9 @@ class AnimeYearsController extends BaseController
return [
AnimeYearSeasonSeasonField::FIELD => $seasonNested,
'seasonLocalized' => $seasonNested->localize(),
'ids' => Anime::query()->where(Anime::ATTRIBUTE_SEASON, $seasonNested)->where(Anime::ATTRIBUTE_YEAR, $yearNested)->pluck(Anime::ATTRIBUTE_ID),
'year' => $yearNested,
];
})
->values()->all(),
})->values()->all(),
];
})
->values()
@@ -69,6 +68,7 @@ class AnimeYearsController extends BaseController
public function applyBuilder(array $root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo): Builder
{
return Anime::query()
->whereIn(Anime::ATTRIBUTE_ID, Arr::get($root, 'ids'));
->where(Anime::ATTRIBUTE_SEASON, Arr::get($root, AnimeYearSeasonSeasonField::FIELD)->value)
->where(Anime::ATTRIBUTE_YEAR, Arr::get($root, 'year'));
}
}
@@ -9,6 +9,9 @@ use App\Contracts\GraphQL\Fields\RequiredOnCreation;
use App\Contracts\GraphQL\Fields\UpdatableField;
use App\Enums\Models\Wiki\ThemeType;
use App\GraphQL\Definition\Fields\EnumField;
use App\GraphQL\Support\Directives\Filters\EqFilterDirective;
use App\GraphQL\Support\Directives\Filters\InFilterDirective;
use App\GraphQL\Support\Directives\Filters\NotInFilterDirective;
use App\Models\Wiki\Anime\AnimeTheme;
use Illuminate\Validation\Rules\Enum;
@@ -27,6 +30,20 @@ class AnimeThemeTypeField extends EnumField implements CreatableField, RequiredO
return 'The type of the sequence';
}
/**
* The directives available for this filter.
*
* @return FilterDirective[]
*/
public function filterDirectives(): array
{
return [
new EqFilterDirective($this),
new InFilterDirective($this, '[OP, ED]'),
new NotInFilterDirective($this),
];
}
/**
* Set the creation validation rules for the field.
*
@@ -0,0 +1,24 @@
<?php
declare(strict_types=1);
namespace App\GraphQL\Middleware;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Context;
class SetServingGraphQL
{
/**
* Handle an incoming request.
*
* @param Closure(Request): mixed $next
*/
public function handle(Request $request, Closure $next): mixed
{
Context::add('serving-graphql', true);
return $next($request);
}
}
+12
View File
@@ -15,6 +15,7 @@ class Argument implements Stringable
use ResolvesDirectives;
protected bool $required = false;
protected mixed $defaultValue = null;
/**
* @var array<string, array>
@@ -48,6 +49,16 @@ class Argument implements Stringable
return $this;
}
/**
* Append a default value to the argument.
*/
public function withDefaultValue(mixed $value): static
{
$this->defaultValue = $value;
return $this;
}
/**
* Get the resolved directives as a string.
*/
@@ -67,6 +78,7 @@ class Argument implements Stringable
->append(': ')
->append(is_string($type) ? $type : $type->__toString())
->when($this->required, fn (SupportStringable $string) => $string->append('!'))
->when($this->defaultValue, fn (SupportStringable $string) => $string->append(" = {$this->defaultValue}"))
->append(' ')
->append($this->getResolvedDirectives())
->__toString();
@@ -17,6 +17,7 @@ abstract readonly class FilterDirective
public function __construct(
protected Field $field,
protected ?string $defaultValue = null,
) {
$this->type = $field->type();
}
@@ -19,6 +19,7 @@ final readonly class InFilterDirective extends FilterDirective
'in' => [
'key' => $this->field->getColumn(),
],
]);
])
->withDefaultValue($this->defaultValue);
}
}
+5 -3
View File
@@ -14,7 +14,6 @@ use App\Events\Wiki\Anime\Theme\ThemeDeleted;
use App\Events\Wiki\Anime\Theme\ThemeDeleting;
use App\Events\Wiki\Anime\Theme\ThemeRestored;
use App\Events\Wiki\Anime\Theme\ThemeUpdated;
use App\Http\Api\Schema\Schema;
use App\Http\Api\Schema\Wiki\Anime\ThemeSchema;
use App\Models\BaseModel;
use App\Models\Wiki\Anime;
@@ -29,6 +28,7 @@ use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Context;
use Illuminate\Support\Str;
/**
@@ -78,13 +78,15 @@ class AnimeTheme extends BaseModel implements InteractsWithSchema, SoftDeletable
final public const RELATION_VIDEOS = 'animethemeentries.videos';
/**
* The "booting" method of the model.
* The "boot" method of the model.
*/
protected static function boot(): void
{
parent::boot();
static::addGlobalScope(new WithoutInsertSongScope);
if (! Context::get('serving-graphql')) {
static::addGlobalScope(new WithoutInsertSongScope);
}
}
/**
+3
View File
@@ -34,6 +34,9 @@ return [
* make sure to return spec-compliant responses in case an error is thrown.
*/
'middleware' => [
// Set the serving context to graphql.
App\GraphQL\Middleware\SetServingGraphQL::class,
// Rate limiting GraphQL to prevent abuse.
'throttle:graphql',