站長資訊網
最全最豐富的資訊網站

詳解PHP中如何安裝和使用GraphQL

本篇文章帶大家了解一下GraphQL,并詳細介紹PHP中安裝和使用GraphQL的方法。有一定的參考價值,有需要的朋友可以參考一下,希望對大家有所幫助。

詳解PHP中如何安裝和使用GraphQL

關于 GraphQL

GraphQL 是一種現代化的 HTTP API 接口構建方式,客戶端可以按需查詢需要的數據。
GraphQL 可以提升 API 調用的靈活性,我們可以像寫數據庫查詢語句一樣來請求 API 來獲取所需要的數據,這對構建復雜的 API 查詢來說非常有用。

與REST對比

REST的核心思想就是資源,每個資源都能用一個URL來表示,你能通過一個GET請求訪問該URL從而獲取該資源。根據當今大多數API的定義,你很有可能會得到一份JSON格式的數據響應,整個過程大概是這樣:

GET /user/1 {     "username":"姓名",     "age":20,     "sex":"男" }
GET /book/1 {     "book":"書名",     "author":"作者",     "country":"中國" }

從上面的示例可以看出,如果前端需要user/1book/1的時候需要調用2次接口,并且如果前端只需要user/1里面的username,而上面的接口獲取了username以外的數據,那么對于前端而言,除 username 之外的數據無處可用,造成了資源的浪費。

如果我們使用GraphQL來進行查詢的話,與REST方式相比,只需要調用一次并且可以查詢我們指定的字段,避免了資源的浪費,并且更加高效。

query {  user(id:1) {      username  }  book(id:1){      book,      author,      country  } }

推薦學習:《PHP視頻教程》

安裝graphql-php包

composer require webonyx/graphql-php

開始

1、安裝完成之后,我們先編寫一個簡單示例,來看看graphql-php怎么用,具體代碼如下:這段代碼中,我們定義了一個名為phoneNumber的字段,然后通過postman來調用我們編寫的代碼。

<?php require_once __DIR__ . '/vendor/autoload.php'; use GraphQLTypeSchema; use GraphQLTypeDefinitionObjectType; use GraphQLTypeDefinitionType; use GraphQLGraphQL;  $queryType = new ObjectType([     'name' => 'Query',     'fields' => [         'phoneNumber' => [             'type' => Type::int(),             'resolve' => function () {                 return 1875555555;             }         ]     ], ]);  $schema = new Schema([     'query' => $queryType, ]);    $rawInput = file_get_contents('php://input'); $input = json_decode($rawInput, true);  $query = $input['query']; $variableValues = isset($input['variables']) ? $input['variables'] : null;  try {     $rootValue = ['prefix' => 'prefix: '];     $result = GraphQL::executeQuery($schema, $query, $rootValue, null, $variableValues);     $output = $result->toArray(); } catch (Exception $e) {      $output = [         'errors' => [             [                 'message' => $e->getMessage()             ]         ]     ]; } header('Content-Type: application/json'); echo json_encode($output);

2、使用postman來調用我們剛剛編寫的代碼,以下是我們查詢結果的示例

詳解PHP中如何安裝和使用GraphQL

介紹

從上面的示例中,我們可以看到示例主要引入了4個類

use GraphQLTypeSchema; use GraphQLTypeDefinitionObjectType; use GraphQLTypeDefinitionType; use GraphQLGraphQL;

Schema 類

Schema 是類型層次結構的容器,它接受構造函數中的根類型并向內部 GrahpQL 工具提供接收你的類型信息的方法。

配置選項

包含以下選項的數組:

Option Type Notes
query ObjectType 必須。 讀取 API 中包含根級字段的對象類型 (通常命名為 "Query"),用于讀取數據
mutation ObjectType 寫入 API 中包含根級字段的對象類型 (通常命名為 "Mutation"),數據變更時會用到
subscription ObjectType 保留用于將來的描述實現。目前表現為 graphql-js 自檢查詢的兼容,用于各種客戶端 (如 Relay 或 GraphiQL)
directives Directive[] 默認包含內建指令 @skip@include

如果你傳遞自定義指令并且依然想使用內建指令,請聲明添加它們。例如:

array_merge(GraphQL::getStandardDirectives(), [$myCustomDirective]);

types ObjectType[] 對象類型類表,它在靜態 schema 解析期間是不能被 graphql-php 發現的。

大多數情況下,對象類型未曾在字段中被直接引用,但它依然是 schema 的一部分時會用到,因為它實現了一個在 resolveType 中調用解析為此對象類型的接口。

請注意,您在此處無需傳遞所有類型 ,它只是具體用例的解決方法。

typeLoader callable function($name) 返回給定的類型實例名稱。 多次調用情況下,必須返回同樣的實例。 查閱下文延遲類型加載部分。

ObjectType類

GraphQLTypeDefinitionObjectType

對象類型是典型的 GraphQL 應用程序中使用最頻繁的基元。

配置選項

Option Type Notes
name string 必須。 Schema 中此對象的唯一名稱
fields array or callable 必須。 描述對象字段或可調用返回此類數組的數組。
description string 呈現于客戶端的參數文本說明(例如:用于 GraphiQL 自動生成文檔 )
interfaces array or callable 此類型實現的接口列表或返回此類列表的可調用接口。

內置標量類型

<?php use GraphQLTypeDefinitionType; // 內置標量類型 Type::string();  // String 類型 Type::int();     // Int 類型 Type::float();   // Float 類型 Type::boolean(); // Boolean 類型 Type::id();      // ID 類型

字段參數

GraphQL 對象類型上的所有字段都有 0 個或多個參數,使用在 args 的字段定義上。每個參數數組參考以下說明:

Option Type Notes
name string 必須。 參數名稱。 為空時,使用 args 數組鍵值
type Type 必須。
description string 呈現于客戶端的參數文本說明
defaultValue scalar 當前參數默認值

示例

$queryType = new ObjectType([     'name' => 'Query',     'fields' => [         'phoneNumber' => [             'type' => Type::int(),             'resolve' => function () {                 return 1875555555;             }         ]     ], ]);

GraphQL 類

GraphQL類主要在查詢的時候用到,我們可以用 GraphQL::executeQuery 方法來執行查詢

executeQuery 方法的參數說明

參數 類型 說明
schema GraphQLTypeSchema 必須。 Schema應用實例
queryString string or GraphQLLanguageASTDocumentNode 必須。 解析,驗證并執行現有的 GraphQL 查詢字符。 如果在執行之前解析其他查詢,則在此處傳遞相應的 AST 文檔節點來避免新的解析。
rootValue mixed 表示數據圖結構的基礎值。作為Query type 字段解析傳遞的第一個參數。如果現有該值已被 Query type 解析過,則可忽略或設置為 null 值。
context mixed 字段解析器的共享信息。 常用來傳遞已登錄用戶信息,位置詳情等。

它將用在所有字段解析器的第 3 個參數。

variableValues array 變量的映射,該值將隨同查詢字符串一起傳遞。請查閱 GraphQL官網查詢變量的相關。
operationName string 指定請求方可執行的操作, 防止條件查詢字符包含多級操作。
fieldResolver callable Schema 參數 schema 中未實現的解析器函數。
validationRules array 查詢驗證規則組,默認所有規則。空數組將跳過查詢驗證 (對于持久化查詢將會比較方便,查詢會在持久化之前默認已驗證,并在執行期間假設符合規則)。
use GraphQLGraphQL;  $result = GraphQL::executeQuery(     $schema,      $queryString,      $rootValue = null,      $context = null,      $variableValues = null,      $operationName = null,     $fieldResolver = null,     $validationRules = null );

簡單示例

我們介紹完GraphQL幾個概念之后,用幾個簡單的示例帶大家來體驗一下。

普通示例

在這個示例中我們定義了2個字段,分別是phoneNumberecho,其中phoneNumber為 Type::int()類型,echoType::string()類型,同時echo字段帶有一個參數為message

<?php  require_once __DIR__ . '/vendor/autoload.php';  use GraphQLTypeDefinitionObjectType; use GraphQLTypeDefinitionType; use GraphQLGraphQL; use GraphQLTypeSchema;  $queryType = new ObjectType([     'name' => 'Query',     'fields' => [         'phoneNumber' => [             'type' => Type::int(),             'resolve' => function () {                 return 1875555555;             }         ],          'echo' => [             'type' => Type::string(),             'args' => [                 'message' => Type::string(),             ],             'resolve' => function ($root, $args) {                 return 'echo msg result:' . ($args['message'] ?? 'nothing');             }         ],     ], ]);  $schema = new Schema([     'query' => $queryType ]);   $rawInput = file_get_contents('php://input'); $input = json_decode($rawInput, true);  $query = $input['query']; $variableValues = isset($input['variables']) ? $input['variables'] : null;  try {     $rootValue = ['prefix' => 'prefix: '];     $result = GraphQL::executeQuery($schema, $query, $rootValue, null, $variableValues);     $output = $result->toArray(); } catch (Exception $e) {      $output = [         'errors' => [             [                 'message' => $e->getMessage()             ]         ]     ]; } header('Content-Type: application/json'); echo json_encode($output);

執行示例代碼結果

詳解PHP中如何安裝和使用GraphQL

我們可以看到,在請求時我們傳了phoneNumberecho兩個字段,并且messagetest

對象示例

我們在上面說過,對象類型是典型的 GraphQL 應用程序中使用最頻繁的基元,一個對象類型里面可以包含寧外一個對象類型,我們可以新定義一個名為$userTypeObjectType,然后在oneUser指定它的類型為$userType,這樣我們執行查詢的時候,oneUser就會返回一個對象。

<?php require_once __DIR__ . '/vendor/autoload.php';  use GraphQLTypeDefinitionObjectType; use GraphQLTypeDefinitionType; use GraphQLGraphQL; use GraphQLTypeSchema;  $userType = new ObjectType([     'name' => 'userType',     'description' => '用戶詳情',     'fields' => [         'uid' => [             'type' => Type::int(),             'description' => '用戶ID'         ],         'name' => Type::string()     ] ]);   $queryType = new ObjectType([     'name' => 'Query',     'fields' => [         'oneUser' => [             'type' => $userType, // 我們這里指定type為我們上面創建的$userType             'description' => '用戶列表',             'args' => [                 'uid' => [                     'type' => Type::int(),                     'defaultValue' => 222                 ]             ],             'resolve' => function($root, $args) {                 return  [                     "uid" => $args['user_id'] ?? 3,                     "name" => "xzl",                 ];             }         ],     ] ]);  $schema = new Schema([     'query' => $queryType ]);  $rawInput = file_get_contents('php://input'); $input = json_decode($rawInput, true); $query = $input['query']; $variableValues = isset($input['variables']) ? $input['variables'] : null;  try {     $rootValue = ['prefix' => 'prefix: '];     $result = GraphQL::executeQuery($schema, $query, $rootValue, null, $variableValues);     $output = $result->toArray(); } catch (Exception $e) {     $output = [         'errors' => [             [                 'message' => $e->getMessage()             ]         ]     ]; } header('Content-Type: application/json'); echo json_encode($output);

執行示例代碼結果

詳解PHP中如何安裝和使用GraphQL

列表示例

在平時的開發請求中,我們從后端接口獲取數據的時候,大部分都是以列表的形式返回的,我們可以通過Type::listOf方法來指定我們返回的字段是一個列表。

<?php require_once __DIR__ . '/vendor/autoload.php';  use GraphQLTypeDefinitionObjectType; use GraphQLTypeDefinitionType; use GraphQLGraphQL; use GraphQLTypeSchema;  class User {     // 模擬從數據庫取數據     public static function getUserLimit($limit)     {         $user  = [             [                 "uid" => 1,                 "name" => "name1"             ],             [                 "uid" => 2,                 "name" => "name2"             ],             [                 "uid" => 3,                 "name" => "name3"             ],             [                 "uid" => 4,                 "name" => "name4"             ]         ];         return array_slice($user, 0, $limit);     } }   $userType = new ObjectType([     'name' => 'userType',     'description' => '用戶詳情',     'fields' => [         'uid' => [             'type' => Type::int(),             'description' => '用戶ID'         ],         'name' => Type::string()     ] ]);   $queryType = new ObjectType([     'name' => 'Query',     'fields' => [         'users' => [             'type' => Type::listOf($userType),             'description' => '用戶列表',             'args' => [                 'limit' => [                     'type' => Type::int(),                     'description' => '限制條數',                     'defaultValue' => 10                 ]             ],             'resolve' => function($root, $args) {                 return User::getUserLimit($args['limit']);             }         ]     ] ]);    $schema = new Schema([     'query' => $queryType ]);   $rawInput = file_get_contents('php://input'); $input = json_decode($rawInput, true); $query = $input['query']; $variableValues = isset($input['variables']) ? $input['variables'] : null;  try {     $rootValue = ['prefix' => 'prefix: '];     $result = GraphQL::executeQuery($schema, $query, $rootValue, null, $variableValues);     $output = $result->toArray(); } catch (Exception $e) {     $output = [         'errors' => [             [                 'message' => $e->getMessage()             ]         ]     ]; } header('Content-Type: application/json'); echo json_encode($output);

執行示例代碼結果

詳解PHP中如何安裝和使用GraphQL

從上面結果可以看到,我們傳了limit參數為2,最終從我們模擬的數據里面取出了2條數據

使用類型語言

在上面的示例中,如果我們代碼返回的數據比較復雜時,需要編寫大量的代碼,通過GraphQL類型語言,我們可以減少代碼量,使代碼看上去更加簡潔,這是一個用 GraphQL 類型語言定義的簡單 Schema示例。

<?php  require_once __DIR__ . '/vendor/autoload.php'; use GraphQLGraphQL; use GraphQLUtilsBuildSchema; // graph.graphql  文件內容 $graph = <<<GRAPH schema {   query: Query }  type Query {   graph_test: String   echo(message: String): String   show_test: Show   show_test_arr: [Show] }   type Show {     content: String!     text: String! } GRAPH;   $schema = BuildSchema::build($graph); $rawInput = file_get_contents('php://input'); $input = json_decode($rawInput, true); $query = $input['query']; $variableValues = isset($input['variables']) ? $input['variables'] : null;  try {     $rootValue = [         'sum' => function($rootValue, $args, $context) {             return $args['x'] + $args['y'];         },         'echo' => function($rootValue, $args, $context) {             return $rootValue['prefix'] . ($args['message'] ?? 'no echo');         },         'show_test' => function($rootValue, $args, $context) {             return [                 'content' => 'show_content',                 'text' => 'xxxx xxx'             ];         },         'show_test_arr' => function($rootValue, $args, $context) {             return [                 [                     'content' => 'show_content',                     'text' => 'xxxx xxx'                 ],                 [                     'content' => 'show_content_2',                     'text' => 'xxxx xxx_2'                 ]              ];         },         'prefix' => 'from test:',         "graph_test" => "graphql_test"     ];;     $result = GraphQL::executeQuery($schema, $query, $rootValue, null, $variableValues);     $output = $result->toArray(); } catch (Exception $e) {      GraphQLServerStandardServer::send500Error($e); } header('Content-Type: application/json'); echo json_encode($output);

執行示例代碼結果

詳解PHP中如何安裝和使用GraphQL

參考

graphql.cn/learn/

learnku.com/docs/graphq…

贊(0)
分享到: 更多 (0)
網站地圖   滬ICP備18035694號-2    滬公網安備31011702889846號
中文字幕日韩专区精品系列| 无码精品A∨在线观看无广告 | 午夜精品久久久久久中宇| 国内精品久久久久久久影视麻豆| 精品一区二区三区无码视频| 亚洲日韩国产一区二区三区在线 | 国产国产成人久久精品| 日韩无套内射视频6| 无码中文字幕日韩专区| 中文字幕51日韩视频| 国产亚洲午夜精品| 国产成人综合日韩精品无码| 国产精品视频久久久久久| 精品国产自在现线看| 嫩草影院在线观看精品视频| 精品伊人久久久香线蕉| 国产高清在线精品二区| 99国产精品欧美一区二区三区| 国内精品久久久久久久久齐齐| 久久久久无码精品国产h动漫| 国产欧美一区二区精品仙草咪| 精品一区二区ww| 精品久久久久久亚洲综合网 | 99精品高清视频一区二区| 久久亚洲中文字幕精品一区| 国产一区二区精品尤物| 99久久精品国产麻豆| 国内大量偷窥精品视频| 久久成人精品视频| 久久精品国产亚洲av麻| 久久午夜无码鲁丝片直播午夜精品| 亚洲精品无码鲁网中文电影| 久久精品国产91久久麻豆自制| 久久精品女人毛片国产| 99久热只有精品视频免费观看17| 久久狠狠高潮亚洲精品| 91麻豆精品国产91久久久久久| 国产精品色拉拉免费看| 日韩精品无码区免费专区| 国产精品资源一区二区| 日韩欧群交P片内射中文|