ホームページ  >  記事  >  バックエンド開発  >  PHPにGraphQLをインストールして使う方法を詳しく解説

PHPにGraphQLをインストールして使う方法を詳しく解説

青灯夜游
青灯夜游転載
2021-05-07 18:38:124084ブラウズ

この記事では、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/ の username# のみを必要とする場合は、 1 ## の場合、上記のインターフェイスは username 以外のデータを取得します。その場合、フロントエンドでは username 以外のデータはどこにも存在せず、リソースが無駄になります。

GraphQL を使用してクエリを実行する場合、REST メソッドと比較して、 を 1 回呼び出すだけで済みます。 指定したフィールドをクエリできます。 、リソースの無駄を避け、より効率的になります。

query {
 user(id:1) {
     username
 }
 book(id:1){
     book,
     author,
     country
 }
}
推奨学習: 「PHP ビデオ チュートリアル

graphql-php パッケージのインストール

composer require webonyx/graphql-php
開始

1. インストールが完了したら、まず、graphql-php の使用方法を確認するための簡単な例を作成します。具体的なコードは次のとおりです: このコードでは、## という名前のファイルを定義します。 #phoneNumber フィールドに入力し、郵便配達員を通じて作成したコードを呼び出します。

<?php
require_once __DIR__ . &#39;/vendor/autoload.php&#39;;
use GraphQL\Type\Schema;
use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\Type;
use GraphQL\GraphQL;

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

$schema = new Schema([
    &#39;query&#39; => $queryType,
]);



$rawInput = file_get_contents(&#39;php://input&#39;);
$input = json_decode($rawInput, true);

$query = $input[&#39;query&#39;];
$variableValues = isset($input[&#39;variables&#39;]) ? $input[&#39;variables&#39;] : null;

try {
    $rootValue = [&#39;prefix&#39; => &#39;prefix: &#39;];
    $result = GraphQL::executeQuery($schema, $query, $rootValue, null, $variableValues);
    $output = $result->toArray();
} catch (\Exception $e) {

    $output = [
        &#39;errors&#39; => [
            [
                &#39;message&#39; => $e->getMessage()
            ]
        ]
    ];
}
header(&#39;Content-Type: application/json&#39;);
echo json_encode($output);

2. postman を使用して、先ほど作成したコードを呼び出します。以下はクエリ結果の例です

##はじめに

PHPにGraphQLをインストールして使う方法を詳しく解説

上記の例から、この例では主に 4 つのクラスが導入されていることがわかります

use GraphQL\Type\Schema;
use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\Type;
use GraphQL\GraphQL;
Schema class

Schema は型階層のコンテナーです。コンストラクターでルート型を受け入れ、内部 GrahpQL ツールが型情報を受け取るためのメソッドを提供します。

構成オプション

次のオプションを含む配列:

Option

Type NotesqueryObjectType ObjectType ルートレベルのフィールドを含むオブジェクト タイプを API に記述します (通常は「Mutation」という名前)。subscription# はデータが変更されるときに使用されます##ObjectTypegraphql-js#ディレクティブ には、デフォルトで組み込みディレクティブ および カスタム ディレクティブを渡しても組み込みディレクティブを使用したい場合は、それらを明示的に追加します。例: オブジェクト型クラス テーブル。静的スキーマ解析中に graphql-php では検出できません。 ほとんどの場合、オブジェクト型はフィールド内で直接参照されませんが、スキーマの一部である場合には引き続き使用されます。これは、オブジェクト型が ここですべての型を渡す必要はないことに注意してください。これは、特定の使用例の単なる回避策です。 指定された型インスタンス名を返します。複数の呼び出しの場合は、同じインスタンスを返す必要があります。以下の遅延型ロードのセクションを参照してください。

ObjectType类

GraphQL\Type\Definition\ObjectType

对象类型是典型的 GraphQL 应用程序中使用最频繁的基元。

配置选项

必須。 データの読み取りに使用される、API のルートレベルのフィールドを含むオブジェクト タイプ (通常は「Query」という名前) を読み取ります。 mutation
は、将来の記述の実装のために予約されています。現在は です。 自己チェック クエリと互換性があり、さまざまなクライアント (Relay や​​ GraphiQL など) に使用されます。 Directive[]
@skip@include が含まれます。 array_merge(GraphQL::getStandardDirectives(), [$myCustomDirective]);

##types

#ObjectType[]
resolveType で呼び出しを実装して解決するためです。このオブジェクト タイプのインターフェイス。 #typeLoader

callable

##function($name)
Option Type Notes
name string 必须。 Schema 中此对象的唯一名称
fields array or callable 必须。 描述对象字段或可调用返回此类数组的数组。
description string 呈现于客户端的参数文本说明(例如:用于 GraphiQL 自动生成文档 )
interfaces array or callable 此类型实现的接口列表或返回此类列表的可调用接口。

内置标量类型

<?php
use GraphQL\Type\Definition\Type;
// 内置标量类型
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([
    &#39;name&#39; => &#39;Query&#39;,
    &#39;fields&#39; => [
        &#39;phoneNumber&#39; => [
            &#39;type&#39; => Type::int(),
            &#39;resolve&#39; => function () {
                return 1875555555;
            }
        ]
    ],
]);

GraphQL 类

GraphQL类主要在查询的时候用到,我们可以用 GraphQL::executeQuery 方法来执行查询

executeQuery 方法的参数说明

参数 类型 说明
schema GraphQL\Type\Schema 必须。  Schema应用实例
queryString string or GraphQL\Language\AST\DocumentNode 必须。 解析,验证并执行现有的 GraphQL 查询字符。 如果在执行之前解析其他查询,则在此处传递相应的 AST 文档节点来避免新的解析。
rootValue mixed 表示数据图结构的基础值。作为Query type 字段解析传递的第一个参数。如果现有该值已被 Query type 解析过,则可忽略或设置为 null 值。
context mixed 字段解析器的共享信息。 常用来传递已登录用户信息,位置详情等。

它将用在所有字段解析器的第 3 个参数。
variableValues array 变量的映射,该值将随同查询字符串一起传递。请查阅 GraphQL官网查询变量的相关
operationName string 指定请求方可执行的操作, 防止条件查询字符包含多级操作。
fieldResolver callable Schema 参数 schema 中未实现的解析器函数。
validationRules array 查询验证规则组,默认所有规则。空数组将跳过查询验证 (对于持久化查询将会比较方便,查询会在持久化之前默认已验证,并在执行期间假设符合规则)。
use GraphQL\GraphQL;

$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__ . &#39;/vendor/autoload.php&#39;;

use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\Type;
use GraphQL\GraphQL;
use GraphQL\Type\Schema;

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

        &#39;echo&#39; => [
            &#39;type&#39; => Type::string(),
            &#39;args&#39; => [
                &#39;message&#39; => Type::string(),
            ],
            &#39;resolve&#39; => function ($root, $args) {
                return &#39;echo msg result:&#39; . ($args[&#39;message&#39;] ?? &#39;nothing&#39;);
            }
        ],
    ],
]);

$schema = new Schema([
    &#39;query&#39; => $queryType
]);


$rawInput = file_get_contents(&#39;php://input&#39;);
$input = json_decode($rawInput, true);

$query = $input[&#39;query&#39;];
$variableValues = isset($input[&#39;variables&#39;]) ? $input[&#39;variables&#39;] : null;

try {
    $rootValue = [&#39;prefix&#39; => &#39;prefix: &#39;];
    $result = GraphQL::executeQuery($schema, $query, $rootValue, null, $variableValues);
    $output = $result->toArray();
} catch (\Exception $e) {

    $output = [
        &#39;errors&#39; => [
            [
                &#39;message&#39; => $e->getMessage()
            ]
        ]
    ];
}
header(&#39;Content-Type: application/json&#39;);
echo json_encode($output);

执行示例代码结果

PHPにGraphQLをインストールして使う方法を詳しく解説

我们可以看到,在请求时我们传了phoneNumberecho两个字段,并且messagetest

对象示例

我们在上面说过,对象类型是典型的 GraphQL 应用程序中使用最频繁的基元,一个对象类型里面可以包含宁外一个对象类型,我们可以新定义一个名为$userTypeObjectType,然后在oneUser指定它的类型为$userType,这样我们执行查询的时候,oneUser就会返回一个对象。

<?php
require_once __DIR__ . &#39;/vendor/autoload.php&#39;;

use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\Type;
use GraphQL\GraphQL;
use GraphQL\Type\Schema;

$userType = new ObjectType([
    &#39;name&#39; => &#39;userType&#39;,
    &#39;description&#39; => &#39;用户详情&#39;,
    &#39;fields&#39; => [
        &#39;uid&#39; => [
            &#39;type&#39; => Type::int(),
            &#39;description&#39; => &#39;用户ID&#39;
        ],
        &#39;name&#39; => Type::string()
    ]
]);


$queryType = new ObjectType([
    &#39;name&#39; => &#39;Query&#39;,
    &#39;fields&#39; => [
        &#39;oneUser&#39; => [
            &#39;type&#39; => $userType, // 我们这里指定type为我们上面创建的$userType
            &#39;description&#39; => &#39;用户列表&#39;,
            &#39;args&#39; => [
                &#39;uid&#39; => [
                    &#39;type&#39; => Type::int(),
                    &#39;defaultValue&#39; => 222
                ]
            ],
            &#39;resolve&#39; => function($root, $args) {
                return  [
                    "uid" => $args[&#39;user_id&#39;] ?? 3,
                    "name" => "xzl",
                ];
            }
        ],
    ]
]);

$schema = new Schema([
    &#39;query&#39; => $queryType
]);

$rawInput = file_get_contents(&#39;php://input&#39;);
$input = json_decode($rawInput, true);
$query = $input[&#39;query&#39;];
$variableValues = isset($input[&#39;variables&#39;]) ? $input[&#39;variables&#39;] : null;

try {
    $rootValue = [&#39;prefix&#39; => &#39;prefix: &#39;];
    $result = GraphQL::executeQuery($schema, $query, $rootValue, null, $variableValues);
    $output = $result->toArray();
} catch (\Exception $e) {
    $output = [
        &#39;errors&#39; => [
            [
                &#39;message&#39; => $e->getMessage()
            ]
        ]
    ];
}
header(&#39;Content-Type: application/json&#39;);
echo json_encode($output);

执行示例代码结果

PHPにGraphQLをインストールして使う方法を詳しく解説

列表示例

在平时的开发请求中,我们从后端接口获取数据的时候,大部分都是以列表的形式返回的,我们可以通过Type::listOf方法来指定我们返回的字段是一个列表。

<?php
require_once __DIR__ . &#39;/vendor/autoload.php&#39;;

use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\Type;
use GraphQL\GraphQL;
use GraphQL\Type\Schema;

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([
    &#39;name&#39; => &#39;userType&#39;,
    &#39;description&#39; => &#39;用户详情&#39;,
    &#39;fields&#39; => [
        &#39;uid&#39; => [
            &#39;type&#39; => Type::int(),
            &#39;description&#39; => &#39;用户ID&#39;
        ],
        &#39;name&#39; => Type::string()
    ]
]);


$queryType = new ObjectType([
    &#39;name&#39; => &#39;Query&#39;,
    &#39;fields&#39; => [
        &#39;users&#39; => [
            &#39;type&#39; => Type::listOf($userType),
            &#39;description&#39; => &#39;用户列表&#39;,
            &#39;args&#39; => [
                &#39;limit&#39; => [
                    &#39;type&#39; => Type::int(),
                    &#39;description&#39; => &#39;限制条数&#39;,
                    &#39;defaultValue&#39; => 10
                ]
            ],
            &#39;resolve&#39; => function($root, $args) {
                return User::getUserLimit($args[&#39;limit&#39;]);
            }
        ]
    ]
]);



$schema = new Schema([
    &#39;query&#39; => $queryType
]);


$rawInput = file_get_contents(&#39;php://input&#39;);
$input = json_decode($rawInput, true);
$query = $input[&#39;query&#39;];
$variableValues = isset($input[&#39;variables&#39;]) ? $input[&#39;variables&#39;] : null;

try {
    $rootValue = [&#39;prefix&#39; => &#39;prefix: &#39;];
    $result = GraphQL::executeQuery($schema, $query, $rootValue, null, $variableValues);
    $output = $result->toArray();
} catch (\Exception $e) {
    $output = [
        &#39;errors&#39; => [
            [
                &#39;message&#39; => $e->getMessage()
            ]
        ]
    ];
}
header(&#39;Content-Type: application/json&#39;);
echo json_encode($output);

执行示例代码结果

PHPにGraphQLをインストールして使う方法を詳しく解説

从上面结果可以看到,我们传了limit参数为2,最终从我们模拟的数据里面取出了2条数据

使用类型语言

在上面的示例中,如果我们代码返回的数据比较复杂时,需要编写大量的代码,通过GraphQL类型语言,我们可以减少代码量,使代码看上去更加简洁,这是一个用 GraphQL 类型语言定义的简单 Schema示例。

<?php

require_once __DIR__ . &#39;/vendor/autoload.php&#39;;
use GraphQL\GraphQL;
use GraphQL\Utils\BuildSchema;
// 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(&#39;php://input&#39;);
$input = json_decode($rawInput, true);
$query = $input[&#39;query&#39;];
$variableValues = isset($input[&#39;variables&#39;]) ? $input[&#39;variables&#39;] : null;

try {
    $rootValue = [
        &#39;sum&#39; => function($rootValue, $args, $context) {
            return $args[&#39;x&#39;] + $args[&#39;y&#39;];
        },
        &#39;echo&#39; => function($rootValue, $args, $context) {
            return $rootValue[&#39;prefix&#39;] . ($args[&#39;message&#39;] ?? &#39;no echo&#39;);
        },
        &#39;show_test&#39; => function($rootValue, $args, $context) {
            return [
                &#39;content&#39; => &#39;show_content&#39;,
                &#39;text&#39; => &#39;xxxx xxx&#39;
            ];
        },
        &#39;show_test_arr&#39; => function($rootValue, $args, $context) {
            return [
                [
                    &#39;content&#39; => &#39;show_content&#39;,
                    &#39;text&#39; => &#39;xxxx xxx&#39;
                ],
                [
                    &#39;content&#39; => &#39;show_content_2&#39;,
                    &#39;text&#39; => &#39;xxxx xxx_2&#39;
                ]

            ];
        },
        &#39;prefix&#39; => &#39;from test:&#39;,
        "graph_test" => "graphql_test"
    ];;
    $result = GraphQL::executeQuery($schema, $query, $rootValue, null, $variableValues);
    $output = $result->toArray();
} catch (\Exception $e) {

    \GraphQL\Server\StandardServer::send500Error($e);
}
header(&#39;Content-Type: application/json&#39;);
echo json_encode($output);

执行示例代码结果

PHPにGraphQLをインストールして使う方法を詳しく解説

参考

graphql.cn/learn/

learnku.com/docs/graphq…

更多编程相关知识,请访问:编程视频!!

以上がPHPにGraphQLをインストールして使う方法を詳しく解説の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事はjuejin.cnで複製されています。侵害がある場合は、admin@php.cn までご連絡ください。