LoginSignup
1
1

More than 3 years have passed since last update.

Laravel GraphQL Lighthouse で Laravel Enum を自動型登録する

Posted at

前提

上記のライブラリはインストール済み

環境

  • PHP 8.0.2
  • Laravel 8.34.0
  • nuwave/lighthouse 5.3.0
  • bensampo/laravel-enum 3.3.0

PHP8.1から言語標準機能としてenumがサポートされるので、今後ライブラリが不要になるかもしれません。
それまではまだしばらくは使われそうなので残します。

LighthouseのEnum型

公式ドキュメントに書いてある通り、Lighthouseは BenSampo/laravel-enum ライブラリに対応してます。

app/Providers/GraphQLServiceProvider.php
class GraphQLServiceProvider extends ServiceProvider
{
    public function boot(TypeRegistry $typeRegistry): void
    {
        // 型の数だけ追加する必要がある
        $typeRegistry->register(
             new LaravelEnumType(UserStatus::class)
        );
    }
}

型登録を手動で行うとコンフリクトしまくっちゃうので、自動的に型登録されるようにします。

Enum定義

普通にEnumクラスを定義します。例としてUserStatus型を作ります。

$ php artisan make:enum UserStatus
app/Enums/UserStatus.php
<?php declare(strict_types=1);

namespace App\Enums;

use BenSampo\Enum\Contracts\LocalizedEnum;
use BenSampo\Enum\Enum;

/**
 * @method INIT()
 * @method ACTIVE()
 * @method LEAVE()
 */
final class UserStatus extends Enum implements LocalizedEnum
{
    const INIT = 'init';
    const ACTIVE = 'active';
    const LEAVE = 'leave';
}

LocalizedEnum を実装しているので、resources/lang/ja/enums.php で翻訳対応してます。

resources/lang/ja/enums.php
<?php declare(strict_types=1);

use App\Enums\UserStatus;

return [
    UserStatus::class => [
        UserStatus::INIT => '仮登録',
        UserStatus::ACTIVE => '有効',
        UserStatus::LEAVE => '退会',
    ],
];

GraphQLServiceProvider

$ php artisan make:provider GraphQLServiceProvider

中身を下記のように書き換えます。

app/Providers/GraphQLServiceProvider.php
<?php declare(strict_types=1);

namespace App\Providers;

use Illuminate\Support\Facades\File;
use Illuminate\Support\ServiceProvider;
use Nuwave\Lighthouse\Exceptions\DefinitionException;
use Nuwave\Lighthouse\Schema\TypeRegistry;
use Nuwave\Lighthouse\Schema\Types\LaravelEnumType;

final class GraphQLServiceProvider extends ServiceProvider
{
    /**
     * @param TypeRegistry $typeRegistry
     * @throws DefinitionException
     */
    public function boot(TypeRegistry $typeRegistry): void
    {
        foreach ($this->getEnumClasses() as $enumClass) {
            $typeRegistry->register(new LaravelEnumType($enumClass));
        }
    }

    /**
     * @return array
     */
    private function getEnumClasses(): array
    {
        $classes = [];

        foreach (File::files(app_path('Enums')) as $file) {
            if ($file->getExtension() === 'php') {
                $classes[] = 'App\\Enums\\' . $file->getBasename('.php');
            }
        }

        return $classes;
    }
}

作製した GraphQLServiceProvider を登録します。

config/app.php
return [
    'providers' => [
        App\Providers\GraphQLServiceProvider::class,
    ],
];

これでEnumクラスの型登録が自動で行われます。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1