日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > php >内容正文

php

laravel graphql php,结合 Laravel 初步学习 GraphQL

發(fā)布時(shí)間:2023/12/6 php 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 laravel graphql php,结合 Laravel 初步学习 GraphQL 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

本文字?jǐn)?shù):7134,大概需要14.27分鐘。

按照官網(wǎng)所述的:

A query language for your API一種用于 API 的查詢語(yǔ)言

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.

GraphQL 既是一種用于 API 的查詢語(yǔ)言也是一個(gè)滿足你數(shù)據(jù)查詢的運(yùn)行時(shí)。 GraphQL 對(duì)你的 API 中的數(shù)據(jù)提供了一套易于理解的完整描述,使得客戶端能夠準(zhǔn)確地獲得它需要的數(shù)據(jù),而且沒(méi)有任何冗余,也讓 API 更容易地隨著時(shí)間推移而演進(jìn),還能用于構(gòu)建強(qiáng)大的開(kāi)發(fā)者工具。

主要有以下幾個(gè)特點(diǎn):

請(qǐng)求你所要的數(shù)據(jù)不多不少。向你的 API 發(fā)出一個(gè) GraphQL 請(qǐng)求就能準(zhǔn)確獲得你想要的數(shù)據(jù),不多不少。 GraphQL 查詢總是返回可預(yù)測(cè)的結(jié)果。

獲取多個(gè)資源只用一個(gè)請(qǐng)求。GraphQL 查詢不僅能夠獲得資源的屬性,還能沿著資源間引用進(jìn)一步查詢。典型的 REST API 請(qǐng)求多個(gè)資源時(shí)得載入多個(gè) URL,而 GraphQL 可以通過(guò)一次請(qǐng)求就獲取你應(yīng)用所需的所有數(shù)據(jù)。這樣一來(lái),即使是比較慢的移動(dòng)網(wǎng)絡(luò)連接下,使用 GraphQL 的應(yīng)用也能表現(xiàn)得足夠迅速。

描述所有的可能類型系統(tǒng)。GraphQL API 基于類型和字段的方式進(jìn)行組織,而非入口端點(diǎn)。你可以通過(guò)一個(gè)單一入口端點(diǎn)得到你所有的數(shù)據(jù)能力。GraphQL 使用類型來(lái)保證應(yīng)用只請(qǐng)求可能的數(shù)據(jù),還提供了清晰的輔助性錯(cuò)誤信息。應(yīng)用可以使用類型,而避免編寫手動(dòng)解析代碼。

API 演進(jìn)無(wú)需劃分版本。給你的 GraphQL API 添加字段和類型而無(wú)需影響現(xiàn)有查詢。老舊的字段可以廢棄,從工具中隱藏。通過(guò)使用單一演進(jìn)版本,GraphQL API 使得應(yīng)用始終能夠使用新的特性,并鼓勵(lì)使用更加簡(jiǎn)潔、更好維護(hù)的服務(wù)端代碼。

使用你現(xiàn)有的數(shù)據(jù)和代碼。GraphQL 讓你的整個(gè)應(yīng)用共享一套 API,而不用被限制于特定存儲(chǔ)引擎。GraphQL 引擎已經(jīng)有多種語(yǔ)言實(shí)現(xiàn),通過(guò) GraphQL API 能夠更好利用你的現(xiàn)有數(shù)據(jù)和代碼。你只需要為類型系統(tǒng)的字段編寫函數(shù),GraphQL 就能通過(guò)優(yōu)化并發(fā)的方式來(lái)調(diào)用它們。

Demo

先寫一個(gè) Demo 來(lái)看看如何結(jié)合 Laravel 使用 GraphQL。

引入 rebing/graphql-laravel

composer require "rebing/graphql-laravel"

因?yàn)?Laravel 5.5 開(kāi)始,有「包自動(dòng)發(fā)現(xiàn)」http://mp.weixin.qq.com/s/AD05BiKjPsI2ehC-mhQJQw功能,所以 Laravel 5.5 可以不用手動(dòng)引入該 provider 和 aliase。之前的版本需要引入對(duì)應(yīng)的 provider 和 aliase。

"extra": { ? ?"laravel": { ? ? ? ?"providers": [ ? ? ? ? ? ?"Rebing\\GraphQL\\GraphQLServiceProvider" ? ? ? ?], ? ? ? ?"aliases": { ? ? ? ? ? ?"GraphQL": "Rebing\\GraphQL\\Support\\Facades\\GraphQL" ? ? ? ?} ? ?}}

創(chuàng)建 Type 和 Query

Type: 通過(guò) Type,可以幫助我們格式化查詢結(jié)果的類型,主要有 boolean、string、float、int 等,同時(shí)也可以自定義類型

Query: 通過(guò) Query,可以獲取我們需要的數(shù)據(jù)。

在項(xiàng)目根目錄創(chuàng)建 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;classUsersTypeextendsGraphQLType{ ? ?protected $attributes = [ ? ? ? ?'name' => 'Users', ? ? ? ?'description' => 'A type', ? ? ? ?'model' => User::class, // define model for users type ? ?]; ? ?// define field of type ? ?public functionfields(){ ? ? ? ?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 functionresolveEmailField($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;classUsersQueryextendsQuery{ ? ?protected $attributes = [ ? ? ? ?'name' => 'users', ? ? ? ?'description' => 'A query of users' ? ?]; ? ?public functiontype(){ ? ? ? ?return Type::listOf(GraphQL::type('users')); ? ?} ? ?// arguments to filter query ? ?public functionargs(){ ? ? ? ?return [ ? ? ? ? ? ?'id' => [ ? ? ? ? ? ? ? ?'name' => 'id', ? ? ? ? ? ? ? ?'type' => Type::int() ? ? ? ? ? ?], ? ? ? ? ? ?'email' => [ ? ? ? ? ? ? ? ?'name' => 'email', ? ? ? ? ? ? ? ?'type' => Type::string() ? ? ? ? ? ?] ? ? ? ?]; ? ?} ? ?public functionresolve($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; ? ?}}

配置 graphql.php

將寫好的 UsersType 和 UsersQuery 注冊(cè)到 GraphGL 配置文件中。

測(cè)試

我們主要有兩種途徑用于測(cè)試,第一種就是向測(cè)試 RESTful 接口一樣,使用 Postman:

另一種方式就是利用 GraphiQL:

An in-browser IDE for exploring GraphQL.https://github.com/graphql/graphiql

這里我們使用noh4ck/graphiql

// 1. 安裝插件composer require "noh4ck/graphiql:@dev"// 2. 加入 providerGraphiql\GraphiqlServiceProvider::class// 3. 命令artisan graphiql:publish

配置文件可看出 route 為:/graphql-ui

運(yùn)行結(jié)果:

還可以通過(guò)傳入?yún)?shù) (id: 1) 來(lái)篩選數(shù)據(jù):

Mutation

通過(guò) Demo,我們初步了解 GraphQL 的 Query 查詢方法,接下來(lái)我們看看 Mutation 的用法。

如果說(shuō) Query 是 RESTful 的「查」,那么 Mutation 充當(dāng)?shù)淖饔镁褪恰冈觥h、改」了。

在 GraphQL 文件夾下創(chuàng)建「Mutation」文件夾,存放和 Mutation 相關(guān)的類。

Create Mutation Class

<?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;classNewUserMutationextendsMutation{ ? ?protected $attributes = [ ? ? ? ?'name' => 'NewUser' ? ?]; ? ?public functiontype(){ ? ? ? ?return GraphQL::type('users'); ? ?} ? ?public functionargs(){ ? ? ? ?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 functionresolve($root, $args){ ? ? ? ?$args['password'] = bcrypt($args['password']); ? ? ? ?$user = User::create($args); ? ? ? ?if (!$user) { ? ? ? ? ? ?return null; ? ? ? ?} ? ? ? ?return $user; ? ?}}

Update Mutation Class

<?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;classUpdateUserMutationextendsMutation{ ? ?protected $attributes = [ ? ? ? ?'name' => 'UpdateUser' ? ?]; ? ?public functiontype(){ ? ? ? ?return GraphQL::type('users'); ? ?} ? ?public functionargs(){ ? ? ? ?return [ ? ? ? ? ? ?'id' => [ ? ? ? ? ? ? ? ?'name' => 'id', ? ? ? ? ? ? ? ?'type' => Type::nonNull(Type::int()) ? ? ? ? ? ?], ? ? ? ? ? ?'name' => [ ? ? ? ? ? ? ? ?'name' => 'name', ? ? ? ? ? ? ? ?'type' => Type::nonNull(Type::string()) ? ? ? ? ? ?] ? ? ? ?]; ? ?} ? ?public functionresolve($root, $args){ ? ? ? ?$user = User::find($args['id']); ? ? ? ?if (!$user) { ? ? ? ? ? ?return null; ? ? ? ?} ? ? ? ?$user->name = $args['name']; ? ? ? ?$user->save(); ? ? ? ?return $user; ? ?}}

配置 graphql.php

把 NewUserMutation 和 UpdateUserMutation 加入 graphql mutation 配置中

測(cè)試

如上圖,創(chuàng)建兩個(gè) mutation,可以選擇任意一個(gè)看運(yùn)行效果。

創(chuàng)建一個(gè) User:

更新「id: 1」用戶信息:

其中在 graphql-ui 界面,右上角還可以看到「Docs」,點(diǎn)開(kāi)可以查看這兩個(gè) mutiation 的說(shuō)明:

總結(jié)

通過(guò)簡(jiǎn)單的「增改查」User 來(lái)初步了解 GraphQL 的 Query 和 Mutation 的使用,接下來(lái)可以將 GraphQL 作為微服務(wù)的「網(wǎng)關(guān)」層的前一層來(lái)使用。

「未完待續(xù)」

coding01 期待您繼續(xù)關(guān)注

總結(jié)

以上是生活随笔為你收集整理的laravel graphql php,结合 Laravel 初步学习 GraphQL的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。