decowood / laravel-eloquent-to-graphql
自动将Eloquent模型转换为GraphQL
v1.0.4
2024-08-26 07:17 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
This package is auto-updated.
Last update: 2024-09-26 07:33:26 UTC
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
选项的命令,该选项禁用所有提示。
如果您使用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
- 在运行迁移后自动更新模式的选择
- 在重新生成模式时保留用户所做的更改的方式
- 更可定制的类型解析