PHP code example of hearst-hatchery / graphql-php-client

1. Go to this page and download the library: Download hearst-hatchery/graphql-php-client library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

hearst-hatchery / graphql-php-client example snippets



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);


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);

use GraphQLQueryBuilder\QueryBuilder;

$query->setObjectField('allPersons')->setArguments(['first' => 2])->setQueryObject([
  'name',
  'birthYear',
  'gender',
  'homeworld' => ['name']
]);

$response = $client->query($query->buildQuery());