Skip to content

Commit 97109d9

Browse files
committed
[graphql] Added enums settings (enums defined inside will be registered automatically).
1 parent 6a24dc4 commit 97109d9

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

config/config.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,15 @@
3535
// empty
3636
],
3737
],
38+
39+
/**
40+
* These enums will be registered automatically. You can use key to specify
41+
* enum name.
42+
*
43+
* @see \LastDragon_ru\LaraASP\Core\Enum
44+
* @see \LastDragon_ru\LaraASP\Eloquent\Enum
45+
*/
46+
'enums' => [
47+
// empty
48+
],
3849
];

src/Provider.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,16 @@
77
use Illuminate\Support\ServiceProvider;
88
use LastDragon_ru\LaraASP\Core\Concerns\ProviderWithConfig;
99
use LastDragon_ru\LaraASP\Core\Concerns\ProviderWithTranslations;
10+
use LastDragon_ru\LaraASP\GraphQL\Helpers\EnumHelper;
1011
use LastDragon_ru\LaraASP\GraphQL\SearchBy\Directive\SearchByDirective;
1112
use LastDragon_ru\LaraASP\GraphQL\SortBy\Directive\SortByDirective;
1213
use Nuwave\Lighthouse\Events\RegisterDirectiveNamespaces;
14+
use Nuwave\Lighthouse\Schema\TypeRegistry;
1315

1416
use function array_slice;
1517
use function explode;
1618
use function implode;
19+
use function is_int;
1720

1821
class Provider extends ServiceProvider {
1922
use ProviderWithConfig;
@@ -23,6 +26,7 @@ public function boot(Dispatcher $dispatcher): void {
2326
$this->bootConfig([
2427
'search_by.scalars',
2528
'search_by.aliases',
29+
'enums',
2630
]);
2731
$this->bootDirectives($dispatcher);
2832
}
@@ -31,6 +35,7 @@ public function register(): void {
3135
parent::register();
3236

3337
$this->registerDirectives();
38+
$this->registerEnums();
3439
}
3540

3641
protected function bootDirectives(Dispatcher $dispatcher): void {
@@ -60,6 +65,19 @@ protected function registerDirectives(): void {
6065
});
6166
}
6267

68+
protected function registerEnums(): void {
69+
$registry = $this->app->make(TypeRegistry::class);
70+
$enums = (array) $this->app->make(Repository::class)->get("{$this->getName()}.enums");
71+
72+
foreach ($enums as $name => $enum) {
73+
if (is_int($name)) {
74+
$name = null;
75+
}
76+
77+
$registry->register(EnumHelper::getType($enum, $name));
78+
}
79+
}
80+
6381
protected function getName(): string {
6482
return Package::Name;
6583
}

0 commit comments

Comments
 (0)