Automatically turn Eloquent models into GraphQL
brausepulver/laravel-eloquent-to-graphql is a Laravel package for automatically turn eloquent models into graphql.
It currently has 3 GitHub stars and 344 downloads on Packagist (latest version v1.0.3).
Install it with composer require brausepulver/laravel-eloquent-to-graphql.
Discover more Laravel packages by brausepulver
or browse all Laravel packages to compare alternatives.
Last updated
This package generates a static GraphQL schema from your Eloquent models.
It is suggested to be used with lighthouse.
composer require --dev brausepulver/laravel-eloquent-to-graphql
use Illuminate\Database\Eloquent\Relations\HasMany;
class User extends Model
{
public function cuteCats(): HasMany
{
...
}
}
php artisan vendor:publish --tag="eloquent_to_graphql.config"
php artisan e2gql
This will generate a schema for all your models in a single file at graphql/generated.graphql.
There are several options for customizing the way the schema is generated. Please have a look at them with
php artisan e2gql --help.
To automatically update the schema every time one of your Eloquent models changes, you can register the command in your IDE of choice with the --force option, which disables all prompts.
If you are using VSCode, this can be achieved using the Run on Save extension. After installing the extension, you can add the following to .vscode/settings.json:
"emeraldwalk.runonsave": {
"commands": [
{
"match": "app/Models/.*\\.php",
"isAsync": true,
"cmd": "php artisan e2gql --force"
}
]
}
With standard project structure, this will update your schema every time an Eloquent model changes.
If your tables have columns with types unknown to DBAL, the command will let you know. In this case, you have two options:
custom_type_mappings in config/eloquent_to_graphql.php: 'custom_type_mappings' => [
'from-type' => 'to-type'
]
--exclude-columns option. You can then later add them by hand.