mirror of
https://github.com/AnimeThemes/animethemes-server.git
synced 2026-07-11 01:24:46 +02:00
feat(graphql): trashed filter (#956)
This commit is contained in:
@@ -4,8 +4,10 @@ declare(strict_types=1);
|
||||
|
||||
namespace App\Concerns\Actions\GraphQL;
|
||||
|
||||
use App\Enums\GraphQL\TrashedFilter;
|
||||
use App\GraphQL\Schema\Types\BaseType;
|
||||
use App\GraphQL\Schema\Unions\BaseUnion;
|
||||
use App\GraphQL\Support\Argument\TrashedArgument;
|
||||
use App\GraphQL\Support\Filter\Filter;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Support\Arr;
|
||||
@@ -19,6 +21,18 @@ trait FiltersModels
|
||||
return $builder;
|
||||
}
|
||||
|
||||
$trashed = Arr::get($args, TrashedArgument::ARGUMENT);
|
||||
|
||||
match ($trashed) {
|
||||
/** @phpstan-ignore-next-line */
|
||||
TrashedFilter::WITH => $builder->withTrashed(),
|
||||
/** @phpstan-ignore-next-line */
|
||||
TrashedFilter::WITHOUT => $builder->withoutTrashed(),
|
||||
/** @phpstan-ignore-next-line */
|
||||
TrashedFilter::ONLY => $builder->onlyTrashed(),
|
||||
default => null,
|
||||
};
|
||||
|
||||
$resolvers = Filter::getValueWithResolvers($type);
|
||||
|
||||
foreach ($args as $arg => $value) {
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Enums\GraphQL;
|
||||
|
||||
use GraphQL\Type\Definition\Description;
|
||||
|
||||
enum TrashedFilter
|
||||
{
|
||||
#[Description('Include soft-deleted models in the result set')]
|
||||
case WITH;
|
||||
|
||||
#[Description('Exclude soft-deleted models; only return active models')]
|
||||
case WITHOUT;
|
||||
|
||||
#[Description('Return only soft-deleted (trashed) models')]
|
||||
case ONLY;
|
||||
}
|
||||
@@ -8,9 +8,12 @@ use App\GraphQL\Middleware\ResolveBindableArgs;
|
||||
use App\GraphQL\Schema\Fields\Base\DeletedAtField;
|
||||
use App\GraphQL\Schema\Queries\BaseQuery;
|
||||
use App\GraphQL\Schema\Types\EloquentType;
|
||||
use App\GraphQL\Support\Argument\Argument;
|
||||
use App\GraphQL\Support\Argument\TrashedArgument;
|
||||
use Exception;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Arr;
|
||||
use RuntimeException;
|
||||
|
||||
abstract class EloquentQuery extends BaseQuery
|
||||
@@ -61,17 +64,19 @@ abstract class EloquentQuery extends BaseQuery
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the return model is trashable.
|
||||
* The arguments of the class resolve as customs class helper.
|
||||
*
|
||||
* @return Argument[]
|
||||
*/
|
||||
protected function isTrashable(): bool
|
||||
public function arguments(): array
|
||||
{
|
||||
$baseType = $this->baseRebingType();
|
||||
$arguments = parent::arguments();
|
||||
|
||||
if ($baseType instanceof EloquentType) {
|
||||
return in_array(new DeletedAtField(), $baseType->fieldClasses());
|
||||
if (in_array(new DeletedAtField(), $this->baseRebingType()->fieldClasses())) {
|
||||
$arguments[] = new TrashedArgument();
|
||||
}
|
||||
|
||||
return false;
|
||||
return Arr::flatten($arguments);
|
||||
}
|
||||
|
||||
protected function query(Builder $builder, array $args): Builder
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\GraphQL\Support\Argument;
|
||||
|
||||
use App\Enums\GraphQL\TrashedFilter;
|
||||
use Rebing\GraphQL\Support\Facades\GraphQL;
|
||||
|
||||
class TrashedArgument extends Argument
|
||||
{
|
||||
final public const ARGUMENT = 'trashed';
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct(self::ARGUMENT, GraphQL::type(class_basename(TrashedFilter::class)));
|
||||
|
||||
$this->withDefaultValue(TrashedFilter::WITHOUT);
|
||||
}
|
||||
}
|
||||
@@ -7,11 +7,13 @@ namespace App\GraphQL\Support\Relations;
|
||||
use App\Concerns\Actions\GraphQL\FiltersModels;
|
||||
use App\Concerns\GraphQL\ResolvesArguments;
|
||||
use App\Enums\GraphQL\PaginationType;
|
||||
use App\GraphQL\Schema\Fields\Base\DeletedAtField;
|
||||
use App\GraphQL\Schema\Types\BaseType;
|
||||
use App\GraphQL\Schema\Unions\BaseUnion;
|
||||
use App\GraphQL\Support\Argument\Argument;
|
||||
use App\GraphQL\Support\Argument\FirstArgument;
|
||||
use App\GraphQL\Support\Argument\PageArgument;
|
||||
use App\GraphQL\Support\Argument\TrashedArgument;
|
||||
use GraphQL\Type\Definition\Type;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Pagination\LengthAwarePaginator;
|
||||
@@ -96,6 +98,10 @@ abstract class Relation
|
||||
}
|
||||
}
|
||||
|
||||
if ($type instanceof BaseType && in_array(new DeletedAtField(), $type->fieldClasses())) {
|
||||
$arguments[] = new TrashedArgument();
|
||||
}
|
||||
|
||||
return Arr::flatten($arguments);
|
||||
}
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ declare(strict_types=1);
|
||||
namespace App\Providers;
|
||||
|
||||
use App\Enums\GraphQL\SortDirection;
|
||||
use App\Enums\GraphQL\TrashedFilter;
|
||||
use App\Enums\Models\List\ExternalEntryWatchStatus;
|
||||
use App\Enums\Models\List\ExternalProfileSite;
|
||||
use App\Enums\Models\List\ExternalProfileVisibility;
|
||||
@@ -31,6 +32,7 @@ class GraphQLServiceProvider extends ServiceProvider
|
||||
|
||||
protected function bootEnums(): void
|
||||
{
|
||||
GraphQL::addType(new EnumType(TrashedFilter::class));
|
||||
GraphQL::addType(new EnumType(SortDirection::class));
|
||||
GraphQL::addType(new EnumType(ExternalEntryWatchStatus::class));
|
||||
GraphQL::addType(new EnumType(ExternalProfileSite::class));
|
||||
|
||||
Reference in New Issue
Block a user