refactor(graphql): pass type instances for relations (#1092)

This commit is contained in:
Kyrch
2026-02-09 23:31:55 -03:00
committed by GitHub
parent e99e692694
commit 747d1d8fc3
17 changed files with 41 additions and 52 deletions
@@ -13,11 +13,8 @@ use Rebing\GraphQL\Support\Facades\GraphQL;
class NodeField extends Field implements DisplayableField
{
/**
* @param class-string<EloquentType> $nodeType
*/
public function __construct(
protected string $nodeType,
protected EloquentType $nodeType,
) {
parent::__construct('node', nullable: false);
}
@@ -21,9 +21,9 @@ class BelongsToManyRelation extends Relation
public function __construct(
protected EloquentType $ownerType,
protected string $nodeType,
protected EloquentType $nodeType,
protected string $relationName,
protected PivotType|string|null $pivotType = null,
protected ?PivotType $pivotType = null,
) {
$this->edgeType = new EdgeType($ownerType, $nodeType, $pivotType);
$this->connectionType = new ConnectionType($this->edgeType);
@@ -31,8 +31,6 @@ class BelongsToManyRelation extends Relation
GraphQL::addType($this->connectionType, $this->connectionType->getName());
parent::__construct(new $nodeType, $relationName);
$this->pivotType = class_exists($pivotType ?? '') ? new $pivotType : null;
}
/**
@@ -48,7 +46,7 @@ class BelongsToManyRelation extends Relation
return [
...parent::arguments(),
new SortArgument($this->baseType, $pivotType),
new SortArgument($this->baseType(), $pivotType),
];
}
@@ -12,7 +12,7 @@ class HasManyRelation extends Relation
{
public function type(): Type
{
return Type::nonNull(Type::listOf(Type::nonNull(GraphQL::type($this->baseType->getName()))));
return Type::nonNull(Type::listOf(Type::nonNull(GraphQL::type($this->baseType()->getName()))));
}
/**
@@ -29,7 +29,7 @@ abstract class Relation extends Field
protected bool $asPivot = false;
public function __construct(
protected BaseType|BaseUnion $baseType,
protected BaseType|BaseUnion $relatedType,
protected string $relationName,
) {
parent::__construct($relationName);
@@ -87,7 +87,7 @@ abstract class Relation extends Field
{
$arguments = [];
$type = $this->baseType;
$type = $this->relatedType;
if ($this->paginationType() !== PaginationType::NONE && $type instanceof BaseType) {
$arguments[] = FilterCriteria::getFilters($type)
@@ -112,7 +112,7 @@ abstract class Relation extends Field
*/
public function baseType()
{
return $this->baseType;
return $this->relatedType;
}
/**
+1 -1
View File
@@ -41,7 +41,7 @@ class RoleType extends EloquentType
new CreatedAtField(),
new UpdatedAtField(),
new BelongsToManyRelation($this, PermissionType::class, Role::RELATION_PERMISSIONS),
new BelongsToManyRelation($this, new PermissionType(), Role::RELATION_PERMISSIONS),
];
}
}
@@ -49,10 +49,10 @@ class MeType extends EloquentType
new MorphManyRelation(new NotificationUnion(), User::RELATION_NOTIFICATIONS),
new HasManyRelation(new PlaylistType(), User::RELATION_PLAYLISTS),
new MorphToManyRelation($this, RoleType::class, User::RELATION_ROLES),
new MorphToManyRelation($this, PermissionType::class, User::RELATION_PERMISSIONS),
new BelongsToManyRelation($this, AnimeThemeEntryType::class, 'likedentries'),
new BelongsToManyRelation($this, PlaylistType::class, 'likedplaylists'),
new MorphToManyRelation($this, new RoleType(), User::RELATION_ROLES),
new MorphToManyRelation($this, new PermissionType(), User::RELATION_PERMISSIONS),
new BelongsToManyRelation($this, new AnimeThemeEntryType(), 'likedentries'),
new BelongsToManyRelation($this, new PlaylistType(), 'likedplaylists'),
];
}
+4 -10
View File
@@ -13,15 +13,11 @@ use Illuminate\Support\Stringable;
class EdgeType extends BaseType
{
protected ?PivotType $pivotType;
public function __construct(
protected EloquentType $parentType,
protected string $nodeType,
?string $pivotType = null,
) {
$this->pivotType = class_exists($pivotType ?? '') ? new $pivotType : null;
}
protected EloquentType $nodeType,
protected ?PivotType $pivotType = null,
) {}
/**
* @return array<string,mixed>
@@ -84,10 +80,8 @@ class EdgeType extends BaseType
/**
* Get the node type of the edge.
*
* @return class-string<EloquentType> $nodeType
*/
public function getNodeType(): string
public function getNodeType(): EloquentType
{
return $this->nodeType;
}
@@ -56,7 +56,7 @@ class PlaylistType extends EloquentType
new BelongsToRelation(new UserType(), Playlist::RELATION_USER)
->nonNullable(),
new HasManyRelation(new PlaylistTrackType(), Playlist::RELATION_TRACKS),
new MorphToManyRelation($this, ImageType::class, Playlist::RELATION_IMAGES, ImageableType::class),
new MorphToManyRelation($this, new ImageType(), Playlist::RELATION_IMAGES, new ImageableType()),
];
}
}
@@ -57,8 +57,8 @@ class AnimeThemeEntryType extends EloquentType implements SubmitableType
new BelongsToRelation(new AnimeThemeType(), AnimeThemeEntry::RELATION_THEME)
->nonNullable(),
new BelongsToManyRelation($this, VideoType::class, AnimeThemeEntry::RELATION_VIDEOS, AnimeThemeEntryVideoType::class),
new MorphToManyRelation($this, ExternalResourceType::class, AnimeThemeEntry::RELATION_RESOURCES, ResourceableType::class),
new BelongsToManyRelation($this, new VideoType(), AnimeThemeEntry::RELATION_VIDEOS, new AnimeThemeEntryVideoType()),
new MorphToManyRelation($this, new ExternalResourceType(), AnimeThemeEntry::RELATION_RESOURCES, new ResourceableType()),
];
}
}
+4 -4
View File
@@ -59,10 +59,10 @@ class AnimeType extends EloquentType implements SubmitableType
new HasManyRelation(new AnimeSynonymType(), Anime::RELATION_SYNONYMS),
new HasManyRelation(new AnimeThemeType(), Anime::RELATION_THEMES),
new MorphToManyRelation($this, ExternalResourceType::class, Anime::RELATION_RESOURCES, ResourceableType::class),
new MorphToManyRelation($this, ImageType::class, Anime::RELATION_IMAGES, ImageableType::class),
new BelongsToManyRelation($this, SeriesType::class, Anime::RELATION_SERIES, AnimeSeriesType::class),
new BelongsToManyRelation($this, StudioType::class, Anime::RELATION_STUDIOS, AnimeStudioType::class),
new MorphToManyRelation($this, new ExternalResourceType(), Anime::RELATION_RESOURCES, new ResourceableType()),
new MorphToManyRelation($this, new ImageType(), Anime::RELATION_IMAGES, new ImageableType()),
new BelongsToManyRelation($this, new SeriesType(), Anime::RELATION_SERIES, new AnimeSeriesType()),
new BelongsToManyRelation($this, new StudioType(), Anime::RELATION_STUDIOS, new AnimeStudioType()),
];
}
}
+4 -4
View File
@@ -48,10 +48,10 @@ class ArtistType extends EloquentType implements SubmitableType
new UpdatedAtField(),
new DeletedAtField(),
new BelongsToManyRelation($this, ArtistType::class, Artist::RELATION_GROUPS, ArtistMemberType::class),
new BelongsToManyRelation($this, ArtistType::class, Artist::RELATION_MEMBERS, ArtistMemberType::class),
new MorphToManyRelation($this, ImageType::class, Artist::RELATION_IMAGES, ImageableType::class),
new MorphToManyRelation($this, ExternalResourceType::class, Artist::RELATION_RESOURCES, ResourceableType::class),
new BelongsToManyRelation($this, new ArtistType(), Artist::RELATION_GROUPS, new ArtistMemberType()),
new BelongsToManyRelation($this, new ArtistType(), Artist::RELATION_MEMBERS, new ArtistMemberType()),
new MorphToManyRelation($this, new ImageType(), Artist::RELATION_IMAGES, new ImageableType()),
new MorphToManyRelation($this, new ExternalResourceType(), Artist::RELATION_RESOURCES, new ResourceableType()),
new HasManyRelation(new MembershipType(), Artist::RELATION_GROUPSHIPS),
new HasManyRelation(new MembershipType(), Artist::RELATION_MEMBERSHIPS),
new MorphManyRelation(new PerformanceType(), Artist::RELATION_PERFORMANCES),
@@ -43,10 +43,10 @@ class ExternalResourceType extends EloquentType implements SubmitableType
new UpdatedAtField(),
new DeletedAtField(),
new MorphToManyRelation($this, AnimeType::class, ExternalResource::RELATION_ANIME, ResourceableType::class),
new MorphToManyRelation($this, ArtistType::class, ExternalResource::RELATION_ARTISTS, ResourceableType::class),
new MorphToManyRelation($this, SongType::class, ExternalResource::RELATION_SONGS, ResourceableType::class),
new MorphToManyRelation($this, StudioType::class, ExternalResource::RELATION_STUDIOS, ResourceableType::class),
new MorphToManyRelation($this, new AnimeType(), ExternalResource::RELATION_ANIME, new ResourceableType()),
new MorphToManyRelation($this, new ArtistType(), ExternalResource::RELATION_ARTISTS, new ResourceableType()),
new MorphToManyRelation($this, new SongType(), ExternalResource::RELATION_SONGS, new ResourceableType()),
new MorphToManyRelation($this, new StudioType(), ExternalResource::RELATION_STUDIOS, new ResourceableType()),
];
}
}
+3 -3
View File
@@ -43,9 +43,9 @@ class ImageType extends EloquentType implements SubmitableType
new UpdatedAtField(),
new DeletedAtField(),
new MorphToManyRelation($this, AnimeType::class, Image::RELATION_ANIME, ImageableType::class),
new MorphToManyRelation($this, ArtistType::class, Image::RELATION_ARTISTS, ImageableType::class),
new MorphToManyRelation($this, StudioType::class, Image::RELATION_STUDIOS, ImageableType::class),
new MorphToManyRelation($this, new AnimeType(), Image::RELATION_ANIME, new ImageableType()),
new MorphToManyRelation($this, new ArtistType(), Image::RELATION_ARTISTS, new ImageableType()),
new MorphToManyRelation($this, new StudioType(), Image::RELATION_STUDIOS, new ImageableType()),
];
}
}
+1 -1
View File
@@ -39,7 +39,7 @@ class SeriesType extends EloquentType implements SubmitableType
new UpdatedAtField(),
new DeletedAtField(),
new BelongsToManyRelation($this, AnimeType::class, Series::RELATION_ANIME, AnimeSeriesType::class),
new BelongsToManyRelation($this, new AnimeType(), Series::RELATION_ANIME, new AnimeSeriesType()),
];
}
}
+1 -1
View File
@@ -44,7 +44,7 @@ class SongType extends EloquentType implements SubmitableType
new HasManyRelation(new AnimeThemeType(), Song::RELATION_ANIMETHEMES),
new HasManyRelation(new PerformanceType(), Song::RELATION_PERFORMANCES),
new MorphToManyRelation($this, ExternalResourceType::class, Song::RELATION_RESOURCES, ResourceableType::class),
new MorphToManyRelation($this, new ExternalResourceType(), Song::RELATION_RESOURCES, new ResourceableType()),
];
}
}
+3 -3
View File
@@ -42,9 +42,9 @@ class StudioType extends EloquentType implements SubmitableType
new UpdatedAtField(),
new DeletedAtField(),
new BelongsToManyRelation($this, AnimeType::class, Studio::RELATION_ANIME, AnimeStudioType::class),
new MorphToManyRelation($this, ImageType::class, Studio::RELATION_IMAGES, ImageableType::class),
new MorphToManyRelation($this, ExternalResourceType::class, Studio::RELATION_RESOURCES, ResourceableType::class),
new BelongsToManyRelation($this, new AnimeType(), Studio::RELATION_ANIME, new AnimeStudioType()),
new MorphToManyRelation($this, new ImageType(), Studio::RELATION_IMAGES, new ImageableType()),
new MorphToManyRelation($this, new ExternalResourceType(), Studio::RELATION_RESOURCES, new ResourceableType()),
];
}
}
+1 -1
View File
@@ -77,7 +77,7 @@ class VideoType extends EloquentType implements SubmitableType
new DeletedAtField(),
new BelongsToRelation(new AudioType(), Video::RELATION_AUDIO),
new BelongsToManyRelation($this, AnimeThemeEntryType::class, Video::RELATION_ANIMETHEMEENTRIES, AnimeThemeEntryVideoType::class),
new BelongsToManyRelation($this, new AnimeThemeEntryType(), Video::RELATION_ANIMETHEMEENTRIES, new AnimeThemeEntryVideoType()),
new HasManyRelation(new PlaylistTrackType(), Video::RELATION_TRACKS),
new HasOneRelation(new VideoScriptType(), Video::RELATION_SCRIPT),
];