ciloe / graphql-client-php
用于 GraphQl API 调用的基础 PHP 库
v0.1
2018-11-10 21:18 UTC
Requires
- php: >=7.1
- guzzlehttp/guzzle: ^6.3
- symfony/cache: ^4.1
- symfony/filesystem: ^4.1
- symfony/finder: ^4.1
- webonyx/graphql-php: ^0.12.0
Requires (Dev)
- phpmd/phpmd: ^2.6
- phpunit/phpunit: ^7.2
- sebastian/phpcpd: ^4.0
- squizlabs/php_codesniffer: ^3.2
This package is auto-updated.
Last update: 2024-09-29 05:02:11 UTC
README
如何安装
通过 源 安装
您可以使用以下命令安装项目:ssh git clone git@github.com:Ciloe/graphql-client-php.git
或 https git clone https://github.com/Ciloe/graphql-client-php.git
通过 composer 安装
要将此库添加到您的项目中,只需使用以下命令行
composer require ciloe/graphql-client-php
配置
第一次运行时,您需要配置 API 信息(主机、URI、令牌)。
<?php require_once './vendor/autoload.php'; $model = new \GraphQLClientPhp\Model\ApiModel('https://api.github.com', '/graphql', 'MyToken');
现在您可以开始使用客户端了。请看以下示例
基本使用(见 demo 文件夹)
在一切开始之前,请声明桥梁,这个类将调用您的 API。您也必须声明您的客户端。
<?php // Some code $bridge = new \GraphQLClientPhp\Bridge\BasicBridge($model); $client = new \GraphQLClientPhp\Client\BasicClient( $bridge, new GraphQLClientPhp\Parser\QueryBasicParser() );
使用工厂
为了更快地创建一个客户端,您可以使用工厂函数,如下所示。
<?php require_once './vendor/autoload.php'; $client = \GraphQLClientPhp\Client\BasicClient::factory( 'https://api.github.com', 'graphql', 'MyToken' );
现在您可以使用客户端通过一个简单的查询调用 API。
<?php // Some code $results = $client->query('query test {user {name}}');
查看客户端中所有可用的函数 这里。
高级使用
使用带变量的查询
现在您可以使用按名称的查询。
<?php // Some code $bridge = new \GraphQLClientPhp\Bridge\BasicBridge($model); $client = new \GraphQLClientPhp\Client\BasicClient( $bridge, new GraphQLClientPhp\Parser\QueryBasicParser() ); $results = $client ->addVariable('variable', true) ->query('query test ($variable: Boolean!) {user @include (if: $variable) {name}}');
使用缓存的查询
<?php // Some code $pool = new \Symfony\Component\Cache\Adapter\FilesystemAdapter(); $fileCache = $PATH_TO_CACHE_DIR . 'cache.php'; $queries = $PATH_TO_GRAPHQL_QUERIES . 'queries'; $fragments = $PATH_TO_GRAPHQL_FRAGMENTS . 'fragments'; $adapter = new \Symfony\Component\Cache\Adapter\PhpArrayAdapter($fileCache, $pool); $service = new \GraphQLClientPhp\Cache\BasicCache( $adapter, new \GraphQLClientPhp\Parser\QueryBasicParser(), ['queries' => $queries, 'fragments' => $fragments] ); $service->warmUp();
此示例将在 $queries
文件夹中生成存储的查询,在 $fragment
文件夹中声明片段。缓存是一个使用数组适配器生成的单个 PHP 文件。缓存键是文件名。它必须是唯一的。
您可以使用工厂创建多个缓存对象。
<?php // Some code $DS = DIRECTORY_SEPARATOR; $fileCache = __DIR__ . $DS . 'Resources' . $DS . 'cache' . $DS . 'cache.php'; $queries = __DIR__ . $DS . 'Resources' . $DS . 'graph' . $DS . 'queries'; $fragments = __DIR__ . $DS . 'Resources' . $DS . 'graph' . $DS . 'fragments'; $queryParser = new \GraphQLClientPhp\Parser\QueryBasicQueryParser(); $cache = \GraphQLClientPhp\Cache\BasicCache::factory( $fileCache, $queries, $fragments, $queryParser );
现在您可以使用按名称的查询。
<?php // Some code $query = $adapter->getItem('myQueryFile'); $fragments = $adapter->getItem(\GraphQLClientPhp\Cache\CacheInterface::CACHED_FRAGMENT_KEY); $client = new \GraphQLClientPhp\Client\BasicClient( $bridge, new GraphQLClientPhp\Parser\QueryBasicParser(), $fragments ); $result = $client->query($query);
从异步查询获取数组结果(使用 Promise)
<?php // Some code // In your PHP execution, you can stored more than one query. $client ->setName('myFirstQuery') ->setVariables(['number' => 5]) ->addQuery( 'query ($number:Int!) { viewer { name repositories(last: $number) { nodes { name } } } }' ); $client ->setName('mySecondQuery') ->addQuery( 'query { viewer { name } }' ); $results = $client->sendQueries(true); // Use parameter $async to use or not promises.