代碼示例:產品列表和用戶列表的 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...