rabelos-coder / php-graphql-client
一个PHP库,通过提供简单的客户端简化了与GraphQL API交互的过程。
1.0.28
2024-01-20 22:32 UTC
Requires
- php: >=8.0
- ext-json: *
- guzzlehttp/guzzle: ^7.0
- psr/http-client: ^1.0
- psr/http-message: ^2.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.45
- mockery/mockery: ^1.6
- phpunit/phpunit: ^10.5
- rector/rector: ^0.13.8
- squizlabs/php_codesniffer: ^3.8
README
GraphQL的PHP客户端 GraphQL
主要功能
- 支持文件附件的客户端
- 轻松执行查询/突变
- 突变和查询的简单数组结果
- 突变和查询的强大对象结果
安装
通过composer
composer require rabelos-coder/php-graphql-client
文档
实例化客户端
您可以实例化一个简单的客户端。
简单客户端
<?php $client = new \RabelosCoder\GraphQL\Client('https://your-domain/graphql');
使用GraphQL客户端
您可以使用客户端执行查询和突变,并获取结果。
<?php /** * Query Example */ $query = <<<'GQL' query GetFooBar($idFoo: String, $idBar: String) { foo(id: $idFoo) { id_foo bar (id: $idBar) { id_bar } } } GQL; $variables = [ 'idFoo' => 'foo', 'idBar' => 'bar', ]; /** @var \RabelosCoder\GraphQL\Client $client */ $request = $client->query($query, $variables); try { // returns response array $response = $request->send(); return $response; } catch (\Exception $e) { // Returns exception message } /** * Mutation Example */ $mutation = <<<'GQL' mutation ($foo: ObjectInput!){ CreateObjectMutation (object: $foo) { status } } GQL; $variables = [ 'foo' => [ 'id_foo' => 'foo', 'bar' => [ 'id_bar' => 'bar' ] ] ]; /** @var \RabelosCoder\GraphQL\Client $client */ $request = $client->query($mutation, $variables); try { // returns response array $response = $request->send(); return $response; } catch (\Exception $e) { // Returns exception message } /** * Mutation With Single File Upload Example */ $mutation = <<<'GQL' mutation ($file: Upload!){ CreateObjectMutation (object: $file) { fileName filePath } } GQL; $file = $_FILES['fieldName']; $uploaded = [ 'fileName' => $file['name'], 'mimeType' => $file['type'], 'filePath' => $file['tmp_name'], ]; $variables = [ 'file' => null, ]; /** @var \RabelosCoder\GraphQL\Client $client */ $request = $client->fileField('file') ->attachment($uploaded) ->query($mutation, $variables); try { // returns response array $response = $request->send(); return $response; } catch (\Exception $e) { // Returns exception message } /** * Mutation With Multiple File Upload Example */ $mutation = <<<'GQL' mutation ($files: [Upload!]!){ CreateObjectMutation (object: $files) { fileName filePath } } GQL; $files = $_FILES['fieldName']; // Remember that form field input name must contains [] at the end and the property multiple setted. $uploaded = []; foreach ($files as $file) { $uploaded[] = [ 'fileName' => $file['name'], 'mimeType' => $file['type'], 'filePath' => $file['tmp_name'], ]; } $variables = [ 'files' => array_map(fn() => null, array_keys($uploaded)), ]; /** @var \RabelosCoder\GraphQL\Client $client */ $request = $client->filesField('files') ->attachments($uploaded) ->query($mutation, $variables); try { // returns response array $response = $request->send(); return $response; } catch (\Exception $e) { // Returns exception message }
在上面的示例中,客户端用于执行查询和突变。响应对象用于以数组格式获取结果。
许可协议
MIT许可协议。有关更多信息,请参阅LICENSE。