sonarsoftware / graphql
Facebook GraphQL for Laravel
Requires
- php: >=7.0
- illuminate/support: 5.5.*|5.6.*|5.7.*
- webonyx/graphql-php: 0.13.*
Requires (Dev)
- fzaninotto/faker: ~1.4
- mockery/mockery: 0.9.*
- orchestra/testbench: 3.1.*|3.2.*|3.3.*|3.4.*|3.5.*
- phpunit/phpunit: ~4.0|~5.0|~5.7|~6.0
- satooshi/php-coveralls: ^1.0
- dev-master
- 2.0.7
- 2.0.6
- 2.0.5
- 2.0.4
- 2.0.3
- 2.0.2
- 2.0.1
- 2.0.0
- v1.x-dev
- v1.1.x-dev
- 1.0.39
- v1.0.38
- v1.0.37
- v1.0.36
- v1.0.35
- v1.0.34
- v1.0.33
- v1.0.32
- v1.0.31
- v1.0.30
- v1.0.29
- v1.0.28
- v1.0.27
- v1.0.26
- v1.0.25
- v1.0.24
- v1.0.23
- v1.0.22
- v1.0.21
- v1.0.20
- v1.0.19
- v1.0.18
- v1.0.17
- v1.0.16
- v1.0.15
- v1.0.14
- v1.0.13
- v1.0.12
- v1.0.11
- v1.0.10
- v1.0.9
- v1.0.8
- v1.0.7
- v1.0.6
- v1.0.5
- v1.0.4
- v1.0.3
- v1.0.2
- v1.0.1
- v1.0.0
- v0.4.9
- v0.4.8
- v0.4.7
- v0.4.6
- v0.4.5
- v0.4.4
- v0.4.3
- v0.4.2
- v0.4.1
- v0.4.0
- v0.3.3
- v0.3.2
- v0.3.1
- v0.3.0
- v0.2.5
- v0.2.4
- v0.2.3
- v0.2.2
- v0.2.1
- v0.2.0
- v0.1.1
- v0.1.0
- dev-develop
- dev-feature/relay
This package is auto-updated.
Last update: 2020-09-23 13:54:50 UTC
README
这个版本已经针对性能进行了优化,并且我一直在跟进webonyx库的最新版本。这个版本在schema注册方法中使用TypeRegistry类来懒加载类型,这提高了注册了500+类型的500%以上的应用性能。我还停止了未使用的TypeAdded事件触发,这也大大提高了性能。
TypeRegistry的使用使得库变得复杂一些 - 你现在必须在graphql.php配置文件中发布自定义标量目录和命名空间路径。在创建自定义标量时,你的type函数必须配置如下
static public function type() {
if(is_null(self::$_instance))
{
self::$_instance = new self();
}
return GraphQL::type(self::$_instance->name);
}
在任何引用GraphQL的地方,你都应该使用GraphQL::type而不是实例化类来调用它,因为这会将钩子连接到TypeRegistry类。如果你不这样做,你将得到模式错误。
Laravel GraphQL
使用Laravel 5或Lumen配合Facebook GraphQL。它基于这里的 PHP实现。你可以在GraphQL Introduction上找到有关GraphQL的更多信息,这是React博客上的文章,或者你可以阅读GraphQL specifications。这是一个正在进行中的工作。
此包与Eloquent模型(或任何其他数据源)兼容。请参阅下面的示例。
要使用laravel-graphql与Relay一起使用,请检查feature/relay分支。
安装
版本1.0已发布。如果您是从旧版本升级,您可以检查升级到1.0。
依赖项
1- 在您的composer.json文件中通过Composer要求此包。
{
"require": {
"sonarsoftware/graphql": "~2.0.0"
}
}
2- 运行Composer来安装或更新新要求。
$ composer install
或者
$ composer update
Laravel >= 5.5.x
1- 发布配置文件
$ php artisan vendor:publish --provider="Folklore\GraphQL\ServiceProvider"
2- 检查配置文件
config/graphql.php
用法
高级用法
模式
从版本1.0开始,您可以定义多个模式。如果有多个模式,例如您想要一个公开的端点和另一个需要认证的端点,这可能会很有用。
您可以在配置文件中定义多个模式
'schema' => 'default', 'schemas' => [ 'default' => [ 'query' => [ //'users' => 'App\GraphQL\Query\UsersQuery' ], 'mutation' => [ //'updateUserEmail' => 'App\GraphQL\Query\UpdateUserEmailMutation' ] ], 'secret' => [ 'query' => [ //'users' => 'App\GraphQL\Query\UsersQuery' ], 'mutation' => [ //'updateUserEmail' => 'App\GraphQL\Query\UpdateUserEmailMutation' ] ] ]
或者您可以使用外观添加模式
GraphQL::addSchema('secret', [ 'query' => [ 'users' => 'App\GraphQL\Query\UsersQuery' ], 'mutation' => [ 'updateUserEmail' => 'App\GraphQL\Query\UpdateUserEmailMutation' ] ]);
之后,您可以使用外观构建模式
// Will return the default schema defined by 'schema' in the config $schema = GraphQL::schema(); // Will return the 'secret' schema $schema = GraphQL::schema('secret'); // Will build a new schema $schema = GraphQL::schema([ 'query' => [ //'users' => 'App\GraphQL\Query\UsersQuery' ], 'mutation' => [ //'updateUserEmail' => 'App\GraphQL\Query\UpdateUserEmailMutation' ] ]);
或者您可以为特定模式请求端点
// Default schema
http://homestead.app/graphql?query=query+FetchUsers{users{id,email}}
// Secret schema
http://homestead.app/graphql/secret?query=query+FetchUsers{users{id,email}}
创建查询
首先您需要创建一个类型。
namespace App\GraphQL\Type; use GraphQL\Type\Definition\Type; use Folklore\GraphQL\Support\Type as GraphQLType; class UserType extends GraphQLType { protected $attributes = [ 'name' => 'User', 'description' => 'A user' ]; /* * Uncomment following line to make the type input object. * https://graphql.net.cn/learn/schema/#input-types */ // protected $inputObject = true; public function fields() { return [ 'id' => [ 'type' => Type::nonNull(Type::string()), 'description' => 'The id of the user' ], 'email' => [ 'type' => Type::string(), 'description' => 'The email of user' ] ]; } // If you want to resolve the field yourself, you can declare a method // with the following format resolve[FIELD_NAME]Field() protected function resolveEmailField($root, $args) { return strtolower($root->email); } }
将类型添加到配置文件config/graphql.php
'types' => [ 'User' => 'App\GraphQL\Type\UserType' ]
您也可以使用GraphQL外观,例如在服务提供者中添加类型。
GraphQL::addType('App\GraphQL\Type\UserType', 'User');
然后您需要定义一个返回此类型(或列表)的查询。您还可以指定在解析方法中可以使用的参数。
namespace App\GraphQL\Query; use GraphQL; use GraphQL\Type\Definition\Type; use Folklore\GraphQL\Support\Query; use App\User; class UsersQuery extends Query { protected $attributes = [ 'name' => 'users' ]; public function type() { return Type::listOf(GraphQL::type('User')); } public function args() { return [ 'id' => [ 'name' => 'id', 'type' => Type::string() ], 'email' => [ 'name' => 'email', 'type' => Type::string() ] ]; } public function resolve($root, $args) { if (isset($args['id'])) { return User::where('id' , $args['id'])->get(); } else if(isset($args['email'])) { return User::where('email', $args['email'])->get(); } else { return User::all(); } } }
如果查询的任何部分有默认值,您可以在args()函数中定义它,通过在其中一个参数中包含defaultValue作为数组键,并将值设置为默认值。例如,如果您在查询中有一个可选参数include_deleted_users,并且默认值应该是false,那么您可以将args()函数编辑如下
public function args()
{
return [
'id' => [
'name' => 'id',
'type' => Type::string()
],
'email' => [
'name' => 'email',
'type' => Type::string()
],
'include_deleted_users' => [
'name' => 'include_deleted_users',
'type' => Type::boolean(),
'defaultValue' => false
]
];
}
您还可以通过添加description数组键来包含查询字段的描述 - 这将在模式中显示,并由工具如GraphiQL拾取。
将查询添加到配置文件config/graphql.php
'schemas' => [ 'default' => [ 'query' => [ 'users' => 'App\GraphQL\Query\UsersQuery' ], // ... ] ]
就这样。您应该能够通过向URL /graphql(或您在配置中选择的其他内容)发出请求来查询GraphQL。尝试以下query输入的GET请求
query FetchUsers {
users {
id
email
}
}
例如,如果您使用homestead
http://homestead.app/graphql?query=query+FetchUsers{users{id,email}}
创建突变
突变就像任何其他查询一样,它接受参数(将用于执行突变)并返回一个特定类型的对象。
例如,更新用户密码的突变。首先您需要定义突变。
namespace App\GraphQL\Mutation; use GraphQL; use GraphQL\Type\Definition\Type; use Folklore\GraphQL\Support\Mutation; use App\User; class UpdateUserPasswordMutation extends Mutation { protected $attributes = [ 'name' => 'updateUserPassword' ]; public function type() { return GraphQL::type('User'); } public function args() { return [ 'id' => ['name' => 'id', 'type' => Type::nonNull(Type::string())], 'password' => ['name' => 'password', 'type' => Type::nonNull(Type::string())] ]; } public function resolve($root, $args) { $user = User::find($args['id']); if (!$user) { return null; } $user->password = bcrypt($args['password']); $user->save(); return $user; } }
如resolve方法所示,您使用参数更新您的模型并返回它。
然后您将突变添加到配置文件config/graphql.php
'schema' => [ 'default' => [ 'mutation' => [ 'updateUserPassword' => 'App\GraphQL\Mutation\UpdateUserPasswordMutation' ], // ... ] ]
然后您应该能够在端点上使用以下查询来执行突变。
mutation users {
updateUserPassword(id: "1", password: "newpassword") {
id
email
}
}
如果您使用homestead
http://homestead.app/graphql?query=mutation+users{updateUserPassword(id: "1", password: "newpassword"){id,email}}
向突变添加验证
可以向突变添加验证规则。它使用laravel的Validator对args执行验证。
在创建突变时,您可以添加一个方法来定义应用到的验证规则,如下所示
namespace App\GraphQL\Mutation; use GraphQL; use GraphQL\Type\Definition\Type; use Folklore\GraphQL\Support\Mutation; use App\User; class UpdateUserEmailMutation extends Mutation { protected $attributes = [ 'name' => 'UpdateUserEmail' ]; public function type() { return GraphQL::type('User'); } public function args() { return [ 'id' => ['name' => 'id', 'type' => Type::string()], 'email' => ['name' => 'email', 'type' => Type::string()] ]; } public function rules() { return [ 'id' => ['required'], 'email' => ['required', 'email'] ]; } public function resolve($root, $args) { $user = User::find($args['id']); if (!$user) { return null; } $user->email = $args['email']; $user->save(); return $user; } }
或者您可以为每个args定义规则
class UpdateUserEmailMutation extends Mutation { //... public function args() { return [ 'id' => [ 'name' => 'id', 'type' => Type::string(), 'rules' => ['required'] ], 'email' => [ 'name' => 'email', 'type' => Type::string(), 'rules' => ['required', 'email'] ] ]; } //... }
在执行突变时,它将返回验证错误。由于GraphQL规范定义了错误的一定格式,因此验证错误消息将添加到错误对象中作为额外的validation属性。要查找验证错误,您应该检查具有等于'validation'的message的错误,然后validation属性将包含由Laravel Validator返回的正常错误消息。
{
"data": {
"updateUserEmail": null
},
"errors": [
{
"message": "validation",
"locations": [
{
"line": 1,
"column": 20
}
],
"validation": {
"email": [
"The email is invalid."
]
}
}
]
}