按照官网所述的:php
A query language for your API 一种用于 API 的查询语言laravel
GraphQL is a query language for APIs and a runtime for fulfilling those queries with your existing data. GraphQL provides a complete and understandable description of the data in your API, gives clients the power to ask for exactly what they need and nothing more, makes it easier to evolve APIs over time, and enables powerful developer tools.git
GraphQL 既是一种用于 API 的查询语言也是一个知足你数据查询的运行时。 GraphQL 对你的 API 中的数据提供了一套易于理解的完整描述,使得客户端可以准确地得到它须要的数据,并且没有任何冗余,也让 API 更容易地随着时间推移而演进,还能用于构建强大的开发者工具。github
主要有如下几个特色:bash
先写一个 Demo 来看看如何结合 Laravel 使用 GraphQL。网络
composer require "rebing/graphql-laravel"
复制代码
由于 Laravel 5.5 开始,有「包自动发现」mp.weixin.qq.com/s/AD05BiKjP…功能,因此 Laravel 5.5 能够不用手动引入该 provider 和 aliase。以前的版本须要引入对应的 provider 和 aliase。并发
"extra": {
"laravel": {
"providers": [
"Rebing\\GraphQL\\GraphQLServiceProvider"
],
"aliases": {
"GraphQL": "Rebing\\GraphQL\\Support\\Facades\\GraphQL"
}
}
}
复制代码
Type: 经过 Type,能够帮助咱们格式化查询结果的类型,主要有 boolean、string、float、int 等,同时也能够自定义类型composer
Query: 经过 Query,能够获取咱们须要的数据。ide
在项目根目录建立 GraphQL 文件件用于存放 Type 和 Query函数
定义 UsersType:
<?php
/** * User: yemeishu */
namespace App\GraphQL\Type;
use App\User;
use GraphQL\Type\Definition\Type;
use Rebing\GraphQL\Support\Facades\GraphQL;
use Rebing\GraphQL\Support\Type as GraphQLType;
class UsersType extends GraphQLType {
protected $attributes = [
'name' => 'Users',
'description' => 'A type',
'model' => User::class, // define model for users type
];
// define field of type
public function fields() {
return [
'id' => [
'type' => Type::nonNull(Type::int()),
'description' => 'The id of the user'
],
'email' => [
'type' => Type::string(),
'description' => 'The email of user'
],
'name' => [
'type' => Type::string(),
'description' => 'The name of the user'
]
];
}
protected function resolveEmailField($root, $args) {
return strtolower($root->email);
}
}
复制代码
定义 Query:
<?php
/** * User: yemeishu */
namespace App\GraphQL\Query;
use App\User;
use GraphQL\Type\Definition\Type;
use Rebing\GraphQL\Support\Facades\GraphQL;
use Rebing\GraphQL\Support\Query;
use Rebing\GraphQL\Support\SelectFields;
class UsersQuery extends Query {
protected $attributes = [
'name' => 'users',
'description' => 'A query of users'
];
public function type() {
return Type::listOf(GraphQL::type('users'));
}
// arguments to filter query
public function args() {
return [
'id' => [
'name' => 'id',
'type' => Type::int()
],
'email' => [
'name' => 'email',
'type' => Type::string()
]
];
}
public function resolve($root, $args, SelectFields $fields) {
$where = function ($query) use ($args) {
if (isset($args['id'])) {
$query->where('id',$args['id']);
}
if (isset($args['email'])) {
$query->where('email',$args['email']);
}
};
$users = User::with(array_keys($fields->getRelations()))
->where($where)
->select($fields->getSelect())
->get();
return $users;
}
}
复制代码
将写好的 UsersType 和 UsersQuery 注册到 GraphGL 配置文件中。
咱们主要有两种途径用于测试,第一种就是向测试 RESTful 接口同样,使用 Postman:
另外一种方式就是利用 GraphiQL:
An in-browser IDE for exploring GraphQL. https://github.com/graphql/graphiql
这里咱们使用 noh4ck/graphiql
// 1. 安装插件
composer require "noh4ck/graphiql:@dev"
// 2. 加入 provider
Graphiql\GraphiqlServiceProvider::class
// 3. 命令
artisan graphiql:publish
复制代码
配置文件可看出 route 为:/graphql-ui
运行结果:
还能够经过传入参数 (id: 1) 来筛选数据:
经过 Demo,咱们初步了解 GraphQL 的 Query 查询方法,接下来咱们看看 Mutation 的用法。
若是说 Query 是 RESTful 的「查」,那么 Mutation 充当的做用就是「增、删、改」了。
在 GraphQL 文件夹下建立「Mutation」文件夹,存放和 Mutation 相关的类。
<?php
/** * User: yemeishu * Date: 2018/4/3 */
namespace App\GraphQL\Mutation;
use GraphQL\Type\Definition\Type;
use Rebing\GraphQL\Support\Facades\GraphQL;
use Rebing\GraphQL\Support\Mutation;
use App\User;
class NewUserMutation extends Mutation {
protected $attributes = [
'name' => 'NewUser'
];
public function type() {
return GraphQL::type('users');
}
public function args() {
return [
'name' => [
'name' => 'name',
'type' => Type::nonNull(Type::string())
],
'email' => [
'name' => 'email',
'type' => Type::nonNull(Type::string())
],
'password' => [
'name' => 'password',
'type' => Type::nonNull(Type::string())
]
];
}
public function resolve($root, $args) {
$args['password'] = bcrypt($args['password']);
$user = User::create($args);
if (!$user) {
return null;
}
return $user;
}
}
复制代码
<?php
/** * User: yemeishu * Date: 2018/4/3 */
namespace App\GraphQL\Mutation;
use GraphQL\Type\Definition\Type;
use Rebing\GraphQL\Support\Facades\GraphQL;
use Rebing\GraphQL\Support\Mutation;
use App\User;
class UpdateUserMutation extends Mutation {
protected $attributes = [
'name' => 'UpdateUser'
];
public function type() {
return GraphQL::type('users');
}
public function args() {
return [
'id' => [
'name' => 'id',
'type' => Type::nonNull(Type::int())
],
'name' => [
'name' => 'name',
'type' => Type::nonNull(Type::string())
]
];
}
public function resolve($root, $args) {
$user = User::find($args['id']);
if (!$user) {
return null;
}
$user->name = $args['name'];
$user->save();
return $user;
}
}
复制代码
把 NewUserMutation 和 UpdateUserMutation 加入 graphql mutation 配置中
如上图,建立两个 mutation,能够选择任意一个看运行效果。
建立一个 User:
更新「id: 1」用户信息:
其中在 graphql-ui 界面,右上角还能够看到「Docs」,点开能够查看这两个 mutiation 的说明:
经过简单的「增改查」User 来初步了解 GraphQL 的 Query 和 Mutation 的使用,接下来能够将 GraphQL 做为微服务的「网关」层的前一层来使用。
其中,推荐一篇文章给你们:微服务下使用GraphQL构建BFF | 洞见mp.weixin.qq.com/s/HSIp5PL-s…
「未完待续」