hearst-hatchery / graphql-php-client
PHP Client for GraphQL
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
A GraphQL client in PHP. This uses Guzzle, but it's compatible with any PSR-18 client through HTTPlug
Installation
composer require hearst-hatchery/graphql-php-client
Usage
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);
this will return
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"
}
}
}
}
Advanced Example
You can easily extend the client, and it supports Httplug's Plugins. This one uses Github's API and variables.
This example needs composer require php-http/message
before running.
Adapted from 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);
Client options
Currently, this client accepts all Guzzle request options.
Default options
method
:POST
headers
:['Content-Type: application/json']
json
:true
Using our GraphQL Query Builder
We also use a query builder in our applications (https://github.com/Hearst-Hatchery/graphql-php-query-builder). Using the first example, installation is easy:
composer require hearst-hatchery/graphql-php-query-builder
Then include it
use GraphQLQueryBuilder\QueryBuilder;
Then $query
can be shortened to:
$query->setObjectField('allPersons')->setArguments(['first' => 2])->setQueryObject([ 'name', 'birthYear', 'gender', 'homeworld' => ['name'] ]);
And expand it in our query()
call:
$response = $client->query($query->buildQuery());
Which should give the same result, while having access to dynamic GraphQL queries!
Contributing
Please read CONTRIBUTING.md for details on submitting pull requests to us.
License
This project is licensed under the MIT License - see the LICENSE.md file for details