代码示例:产品列表和用户列表的 API 例子
昨天咱们学习了 在 Visual Code 中搭建 Laravel 环境,如今咱们来学习 Facebook 的 GraphQL 。php
GraphQL 是一种 API 查询语言,仍是一种根据你为数据定义的类型系统执行查询的服务器端运行时。GraphQL 不依赖于任何指定的数据库或存储引擎,而是由你的代码和数据来做支持的。 graphql.org
GraphQL 能够提高 API 调用的灵活性,咱们能够像写数据库查询语句同样来请求 API 来获取所须要的数据,这对构建复杂的 API 查询来讲很是有用。GraphQL 还提供了可视化界面来帮助咱们编写查询语句,还提供了自动补全的功能,这让编写查询更加简单。laravel
https://github.com/graphql/gr...
从如下图片能够看出,GraphQL 和 Rest 同样都是运行在业务逻辑层之外的:git
使用下面命令安装最新版本的 Laravel :github
# 在命令行中执行 composer global require "laravel/installer" laravel new laravel-graphql
使用 composer 安装 graphql-laravel,这个包提供了很是多的功能用于整合 Laravel 和 GraphQL 。数据库
像下面这样建立模型和表 user_profiles, products, product_images,别忘了还要建立模型间的关系。visual-studio-code
GraphQL 中的查询与 Restful API 中的末端路径查询是同样的,查询只是用于获取数据,以及建立、更新、删除操做。咱们把它称做 Mutation 。服务器
GraphQL 中的 类型 用于定义查询中每一个字段的类型定义,类型会帮助咱们格式化查询结果中的有格式的字段,例如布尔类型,字符串类型,浮点类型,整数类型等等,以及咱们的自定义类型。下面是查询和类型的目录结构:composer
这是 UsersQuery.php 和 UsersType.php 文件完整的源代码:post
<?php 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 Query', 'description' => 'A query of users' ]; public function type() { // 带分页效果的查询结果 return GraphQL::paginate('users'); } // 过滤查询的参数 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']); } }; $user = User::with(array_keys($fields->getRelations())) ->where($where) ->select($fields->getSelect()) ->paginate(); return $user; } }
<?php 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, // 定义用户类型的数据模型 ]; // 定义字段的类型 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' ], // 数据模型 user_profiles 中的关联字段 'user_profiles' => [ 'type' => GraphQL::type('user_profiles'), 'description' => 'The profile of the user' ] ]; } protected function resolveEmailField($root, $args) { return strtolower($root->email); } }
在编写完查询语句和类型以后,咱们须要编辑 config/graphql.php 文件,将查询语句和类型注册到 Schema 中。visual-studio
<?php use App\GraphQL\Query\ProductsQuery; use App\GraphQL\Query\UsersQuery; use App\GraphQL\Type\ProductImagesType; use App\GraphQL\Type\ProductsType; use App\GraphQL\Type\UserProfilesType; use App\GraphQL\Type\UsersType; return [ 'prefix' => 'graphql', 'routes' => 'query/{graphql_schema?}', 'controllers' => \Rebing\GraphQL\GraphQLController::class . '@query', 'middleware' => [], 'default_schema' => 'default', // 注册查询命令 'schemas' => [ 'default' => [ 'query' => [ 'users' => UsersQuery::class, 'products' => ProductsQuery::class, ], 'mutation' => [ ], 'middleware' => [] ], ], // 注册类型 'types' => [ 'product_images' => ProductImagesType::class, 'products' => ProductsType::class, 'user_profiles' => UserProfilesType::class, 'users' => UsersType::class, ], 'error_formatter' => ['\Rebing\GraphQL\GraphQL', 'formatError'], 'params_key' => 'params' ];
咱们可使用 GraphiQL 来十分简单地编写查询语句,由于在编写的时候它能够自动补全,或者咱们也可使用 postman 来请求 API,下面是自动补全的示例:
下面是查询结果的示例
若是你想查阅源代码,能够访问如下地址 :)。
https://github.com/ardani/lar...
转自 PHP / Laravel 开发者社区 https://laravel-china.org/top...