ciloe/graphql-client-php
Composer 安装命令:
composer require ciloe/graphql-client-php
包简介
A basic php library for GraphQl API calls
README 文档
README
How to install
Install by sources
You can install the project with ssh git clone git@github.com:Ciloe/graphql-client-php.git or
or by https git clone https://github.com/Ciloe/graphql-client-php.git
Install by composer
To add at your project, just use this command line :
composer require ciloe/graphql-client-php
Configuration
At the first time, you need to configure the API information (host, uri, token).
<?php require_once './vendor/autoload.php'; $model = new \GraphQLClientPhp\Model\ApiModel('https://api.github.com', '/graphql', 'MyToken');
Now you can use the client. See following examples
Basic used (see demo Folder)
Before everything, declare the bridge, this class will calling your API. You must declare your client too.
<?php // Some code $bridge = new \GraphQLClientPhp\Bridge\BasicBridge($model); $client = new \GraphQLClientPhp\Client\BasicClient( $bridge, new GraphQLClientPhp\Parser\QueryBasicParser() );
Using factory
To create faster a client, you can use the factory function like this.
<?php require_once './vendor/autoload.php'; $client = \GraphQLClientPhp\Client\BasicClient::factory( 'https://api.github.com', 'graphql', 'MyToken' );
Now you can use the client to call the api with a simple query :
<?php // Some code $results = $client->query('query test {user {name}}');
See all functions available in the client here.
Advanced Use
Use queries with variables
Now you can use queries by 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}}');
Use queries cached
<?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();
This example will generate queries stored in $queries folder
with fragment declared in $fragment folder. The cache is a
single php file generated with the array adapter. The cache keys
are the name of file. It must be unique.
You can use the factory to create multiple cache object :
<?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 );
Now you can use queries by name.
<?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);
Get array results from async query (use 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.
ciloe/graphql-client-php 适用场景与选型建议
ciloe/graphql-client-php 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 21 次下载、GitHub Stars 达 6, 最近一次更新时间为 2018 年 11 月 10 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「client」 「bundle」 「graphql」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 ciloe/graphql-client-php 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 ciloe/graphql-client-php 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 ciloe/graphql-client-php 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
2lenet/EasyAdminPlusBundle
GraphQL authentication for your headless Craft CMS applications.
The bundle for easy using json-rpc api on your project
Provide a way to secure accesses to all routes of an symfony application.
DMS Meetup API Bundle, enables Meetup API clients in services
Authorize introspection documentarion for rebing/graphql-laravel
统计信息
- 总下载量: 21
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 6
- 点击次数: 0
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-11-10