brausepulver / laravel-eloquent-to-graphql
自动将Eloquent模型转换为GraphQL
v1.0.3
2024-02-26 23:35 UTC
Requires
- doctrine/dbal: ^3.3
- illuminate/support: >=9.1
Requires (Dev)
- ext-pdo_sqlite: *
- orchestra/testbench: ^8.21
- phpunit/phpunit: ^9.5
Suggests
- nuwave/lighthouse: Uses the generated directives
README
此包可以从您的Eloquent模型生成静态GraphQL模式。
建议与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
这将生成一个文件在graphql/generated.graphql
中包含所有模型的模式。
有几种选项可以自定义生成模式的方式。请使用
php artisan e2gql --help
.
配置
自动更新
要自动在您的Eloquent模型更改时更新模式,您可以使用带有--force
选项的命令在您选择的IDE中注册,这将禁用所有提示。
如果您使用VSCode,可以使用Run on Save扩展来实现。安装扩展后,您可以向.vscode/settings.json
添加以下内容
"emeraldwalk.runonsave": { "commands": [ { "match": "app/Models/.*\\.php", "isAsync": true, "cmd": "php artisan e2gql --force" } ] }
在标准项目结构中,这将在Eloquent模型更改时更新您的模式。
自定义类型映射
如果您的表中有DBAL未知类型的列,命令将通知您。在这种情况下,您有两个选项
- 让命令知道您要将未知类型映射到的DBAL已知类型。您可以通过在
config/eloquent_to_graphql.php
中的custom_type_mappings
中添加两者来实现。
'custom_type_mappings' => [ 'from-type' => 'to-type' ]
- 使用
--exclude-columns
选项完全忽略列。然后您可以稍后手动添加它们。
TODO
- 在运行迁移后自动更新模式的选项
- 保留在重新生成模式时用户所做的更改的方法
- 更可定制的类型解析