https://github.com/brausepulver/laravel-eloquent-to-graphql
Generate GraphQL schemas from Eloquent models
https://github.com/brausepulver/laravel-eloquent-to-graphql
artisan-command eloquent eloquent-models eloquent-relationships illuminate laravel laravel-migrations
Last synced: 19 days ago
JSON representation
Generate GraphQL schemas from Eloquent models
- Host: GitHub
- URL: https://github.com/brausepulver/laravel-eloquent-to-graphql
- Owner: brausepulver
- License: mit
- Created: 2022-03-03T18:33:50.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2024-02-29T01:14:07.000Z (about 1 year ago)
- Last Synced: 2025-03-27T19:40:57.829Z (about 1 month ago)
- Topics: artisan-command, eloquent, eloquent-models, eloquent-relationships, illuminate, laravel, laravel-migrations
- Language: PHP
- Homepage:
- Size: 60.5 KB
- Stars: 3
- Watchers: 1
- Forks: 4
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# laravel-eloquent-to-graphql
[](https://github.com/brausepulver/laravel-eloquent-to-graphql/actions)
[](https://packagist.org/packages/brausepulver/laravel-eloquent-to-graphql)
[](https://packagist.org/packages/brausepulver/laravel-eloquent-to-graphql)This package generates a static GraphQL schema from your Eloquent models.
It is suggested to be used with [lighthouse](https://lighthouse-php.com/).
## Setup
1. Install the package:
```
composer require --dev brausepulver/laravel-eloquent-to-graphql
```2. Type-hint your models' relationships:
```php
use Illuminate\Database\Eloquent\Relations\HasMany;class User extends Model
{
public function cuteCats(): HasMany
{
...
}
}
```3. _(Optional)_ Publish the configuration file:
```
php artisan vendor:publish --tag="eloquent_to_graphql.config"
```## Usage
```
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`.## Configuration
### Automatic Updates
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](https://marketplace.visualstudio.com/items?itemName=emeraldwalk.RunOnSave) extension. After installing the extension, you can add the following to `.vscode/settings.json`:
```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.### Custom Type Mappings
If your tables have columns with types unknown to DBAL, the command will let you know. In this case, you have two options:
1. Let the command know which type that is known by DBAL you would like to map the unkown type to. You can do this by adding both to `custom_type_mappings` in `config/eloquent_to_graphql.php`:
```php
'custom_type_mappings' => [
'from-type' => 'to-type'
]
```2. Ignore the columns entirely using the `--exclude-columns` option. You can then later add them by hand.
## TODO
- [ ] Option to automatically update the schema after migrations are run
- [ ] Way to keep changes made by the user when regenerating a schema
- [ ] More customizable type resolution