xpromx / laravel-graphql
Laravel对Facebook的GraphQL的包装
Requires
- php: >=7.0
- folklore/graphql: ^1.1
This package is auto-updated.
Last update: 2024-09-22 16:34:27 UTC
README
本项目基于Folklore\GraphQL包,添加了使集成Laravel更加容易的辅助工具。
更多详情请查看:https://github.com/Folkloreatelier/laravel-graphql
安装
1) Composer
composer require xpromx/laravel-graphql
2) 创建配置文件
在 /config/graphql.php 中检查此仓库中的示例。
3) 编辑 bootstrap/app.php
取消以下行的注释
$app->withFacades(); $app->withEloquent(); $app->register(App\Providers\AppServiceProvider::class); $app->register(App\Providers\AuthServiceProvider::class);
然后在同一文件中添加以下行
$app->configure('graphql'); $app->register(Folklore\GraphQL\LumenServiceProvider::class);
4) 创建GraphQL文件夹
在此文件夹中: /app/GraphQL,包含以下文件夹: /Types 和 /Query
Graphiql
浏览器内的IDE,用于探索GraphQL。
http://project-name.test/graphiql
类型
创建新的类型 = 您的Laravel模型,请查看此仓库中 /Examples/Type 文件夹中的示例。请查看此文档中的自定义类型部分。
在您的 /config/graphql.php 中注册类型。
<?php // app/GraphQL/Type/UserType.php namespace App\GraphQL\Type; use Xpromx\GraphQL\Definition\Type; use Xpromx\GraphQL\Type as BaseType; class UserType extends BaseType { protected $attributes = [ 'name' => 'UserType', 'description' => 'A User', 'model' => \App\User::class // Laravel Model ]; public function fields() { return [ 'id' => [ 'type' => Type::nonNull(Type::string()), 'description' => 'The id of the user' ], 'created_at' => [ 'type' => Type::date(), 'description' => 'When the user was created' ], 'updated_at' => [ 'type' => Type::date(), 'description' => 'When the user was updated' ], ]; } }
查询
创建新的查询 = 您API的端点。请查看此仓库中 /Examples/Query 文件夹中的示例。
在您的 /config/graphql.php 中注册查询。
<?php // app/GraphQL/Query/UserQuery.php namespace App\GraphQL\Query; use Xpromx\GraphQL\Query; use Xpromx\GraphQL\Definition\Type; class UsersQuery extends Query { protected $attributes = [ 'name' => 'UsersQuery', 'description' => 'A Users Query' ]; public function type() { return Type::connection('user'); // UserType } }
查询参数
这些是可以自动应用于查询的过滤器。要使用高级过滤器,您必须注册您的graphql.php配置中的类型。
'types' => [ 'Filter' => 'Xpromx\GraphQL\Filter\FilterType', 'FilterCondition' => 'Xpromx\GraphQL\Filter\FilterConditionEnum', ]
查询示例
users( id: 1, limit: 30, page: 2, hasRelation: 'user', doesntHaveRelation: 'comments', orderBy: 'id DESC', filter: [{field: "email", condition:CONTAINS, value:"@gmail.com"}] ) { nodes { ... } pageInfo { ... } }
过滤器条件
- GT
- GTE
- LT
- LTE
- EQUAL
- CONTAINS
- NOT_CONTAINS
- STARTS_WITH
- ENDS_WTIH
- IN
- NOT_IN
- NOT_EQUAL
- NULL
- NOT_NULL
连接类型
将为选定的类型创建连接,此连接将简化查询并使结果符合https://graphql.net.cn/标准格式。
{
userQuery(page:1, limit:20){
nodes{
id,
first_name
email
...
},
pageInfo{
current_page
total
}
}
}
日期类型
返回格式化为人读日期
'updated_at' => [ 'type' => Type::date(), 'description' => 'When the user was updated' ]
时间类型
返回格式化为人读时间
'duration' => [ 'type' => Type::time(), 'description' => 'Movie duration' ]
多对一类型
返回所选类型的列表,对于关系 OneToMany
// UserType.php 'comments' => Type::hasMany('comment')
一对一类型
对于 OneToOne 关系
// CommentType 'user' => Type::hasOne('user')
元数据类型
当您需要返回JSON对象时,请使用元数据字段
// UserType 'meta' => [ 'type' => Type::meta(), 'description' => 'Extra information about this user' ]
分页信息类型
返回分页字段,此字段自动应用于连接类型,字段如下
{
pageInfo
{
current_page,
next_page,
prev_page,
last_page,
per_page,
total
}
}
日期字段
您可以使用自定义日期字段以默认格式进行格式化,并能够从查询中更改格式。
public function fields() { return [ 'updated_at' => Type::dateField($field='updated_at', $format='M j, Y'), ] }
{
userQuery(page:1, limit:20){
nodes{
id,
first_name
created_at(format:"Y-m-d")
...
},
}
}
时间字段
您可以使用自定义时间字段以默认格式进行格式化,并能够从查询中更改格式。
public function fields() { return [ 'updated_at' => Type::timeField($field='updated_at', $format='H:i'), ] }
{
userQuery(page:1, limit:20){
nodes{
id,
first_name
created_at(format:"H:i:s")
...
},
}
}