Lighthouse plugin for spatie/laravel-settings
A nice way to make Lighthouse receive and update typed settings from the laravel-settings plugin.
composer require roboroads/lighthouse-laravel-settings
After installing you might want to re-generate the ide-helper files:
php artisan lighthouse:ide-helper
First, create your settings as normal. Let's say we have the following settings file
class GeneralSettings extends Settings
{
public string $name;
public string $url;
public string $description;
public static function group(): string
{
return 'general';
}
}
Use @settings to tell Lighthouse to get settings using the same name as the type:
type GeneralSettings {
name: String!
url: String!
description: String!
}
type Query {
generalSettings: GeneralSettings! @settings
}
Note: if your type does not match the pattern
App\Settings\TypeName
(in this caseApp\Settings\GeneralSettings
) you can use@settings(class: "\\Path\\To\\YourSettings")
.
Use @settings to tell Lighthouse to update settings using the same name as the type:
type GeneralSettings {
name: String!
url: String!
description: String!
}
type Mutation {
updateGeneralSettings(
name: String!
url: String!
description: String!
): GeneralSettings! @settings
}
Note: Except for @can, you can use @rules, @trim, etc. like you can when updating models.
Since Lighthouse really wants to connect @can to an actual Model, you have to use @canSettings to use the ability to authorize the user for settings.
type Mutation {
updateGeneralSettings(
name: String!
url: String!
description: String!
): GeneralSettings! @settings @canSettings(ability: "editSettings")
}
//Defining a gate
Gate::define('editSettings', function (User $user, string $settingsClass) {
return $user->isAdmin();
});
If you want to tweak this plugin you can publish config/laravel-settings.php
with:
php artisan vendor:publish --provider="Roboroads\LighthouseSettings\ServiceProvider"
Change the default namespace this package looks in when searching for settings.