hearst-hatchery / graphql-php-client
此包已被废弃,不再维护。没有建议的替代包。
GraphQL 的 PHP 客户端
0.4.1
2021-04-16 14:28 UTC
Requires
- php: ^7.1
- ext-json: *
- guzzlehttp/guzzle: ^6.3
- php-http/client-common: ^1.8
- php-http/discovery: ^1.4
- php-http/guzzle6-adapter: ^1.1
- psr/log: ^1.0
- symfony/options-resolver: ^4.2
Requires (Dev)
- codeception/codeception: ^4.1
- codeception/verify: ^1.0
- friendsofphp/php-cs-fixer: ^2.16
- guzzlehttp/psr7: ^1.4
- php-http/mock-client: ^1.1
Suggests
- hearst-hatchery/graphql-php-query-builder: Easy and convenient way to build GraphQL queries
This package is auto-updated.
Last update: 2022-12-24 20:57:38 UTC
README
这是一个 PHP 的 GraphQL 客户端。它使用 Guzzle,但通过 HTTPlug 与任何符合 PSR-18 标准的客户端兼容
安装
composer require hearst-hatchery/graphql-php-client
用法
require 'vendor/autoload.php'; use GraphQLClient\Client; $client = new Client("https://swapi.graph.cool/"); $query = <<<QUERY { allPersons(first:2) { name birthYear gender homeworld { name } } } QUERY; $response = $client->query($query); var_dump($response);
这将返回
array(1) {
["allPersons"]=>
array(2) {
[0]=>
array(4) {
["name"]=>
string(14) "Luke Skywalker"
["birthYear"]=>
string(5) "19BBY"
["gender"]=>
string(4) "MALE"
["homeworld"]=>
array(1) {
["name"]=>
string(8) "Tatooine"
}
}
[1]=>
array(4) {
["name"]=>
string(5) "C-3PO"
["birthYear"]=>
string(6) "112BBY"
["gender"]=>
string(7) "UNKNOWN"
["homeworld"]=>
array(1) {
["name"]=>
string(8) "Tatooine"
}
}
}
}
高级示例
您可以轻松扩展客户端,并支持 Httplug 的插件。此示例使用 GitHub 的 API 和变量。
此示例需要先运行 composer require php-http/message
。改编自 https://developer.github.com/v4/guides/forming-calls/#working-with-variables
require 'vendor/autoload.php'; use GraphQLClient\Client; use Http\Message\Authentication\Bearer; use Http\Client\Common\Plugin\AuthenticationPlugin; use Http\Client\Common\PluginClient; class GithubClient extends Client { public function __construct(){ parent::__construct("https://api.github.com/graphql"); } protected function buildClient(array $options = []) { $authentication = new Bearer('<Github API Token>'); $authenticationPlugin = new AuthenticationPlugin($authentication); return new PluginClient( parent::buildClient($options), [$authenticationPlugin] ); } } $client = new GithubClient(); $query = <<<QUERY query(\$number_of_repos:Int!) { viewer { name repositories(last: \$number_of_repos) { nodes { name } } } } QUERY; $response = $client->query($query, ['number_of_repos' => 3]); var_dump($response);
客户端选项
目前,此客户端接受所有 Guzzle 请求选项。
默认选项
method
:POST
headers
:['Content-Type: application/json']
json
:true
使用我们的 GraphQL 查询构建器
我们还在我们的应用程序中使用查询构建器 (https://github.com/Hearst-Hatchery/graphql-php-query-builder)。使用第一个示例,安装很简单
composer require hearst-hatchery/graphql-php-query-builder
然后包含它
use GraphQLQueryBuilder\QueryBuilder;
然后 $query
可以缩短为
$query->setObjectField('allPersons')->setArguments(['first' => 2])->setQueryObject([ 'name', 'birthYear', 'gender', 'homeworld' => ['name'] ]);
然后在我们的 query()
调用中展开它
$response = $client->query($query->buildQuery());
这应该给出相同的结果,同时可以访问动态 GraphQL 查询!
贡献
请阅读 CONTRIBUTING.md 了解提交拉取请求的详细信息。
许可证
此项目受 MIT 许可证的许可 - 请参阅 LICENSE.md 文件了解详细信息