PHP code example of ilexn / graphql-payload-object

1. Go to this page and download the library: Download ilexn/graphql-payload-object 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/ */

    

ilexn / graphql-payload-object example snippets



declare(strict_types=1);

NameAndFriends($episode: Episode) {
  hero(episode: $episode) {
    name
    friends {
      name
    }
  }
}
QUERY;

$variables = [
    "episode" => "JEDI",
];


$payload = \Ilex\GraphqlPayloadObject\Payload::fromString($query, $variables);
// or from path
//$payload = \Ilex\GraphqlPayloadObject\Payload::fromPath('example.gql', $variables);


// use the same query , with different variable set
$newPayload = $payload->withVariable([
    'episode' => 'new episode',
    'key' => 'new value',
]);

// Symfony HttpClient Component
$client = Symfony\Component\HttpClient\HttpClient::create();
$response = $client->request('POST',
    'http://example.com/graphql', [
        'body' => $payload->toJson(),
        // or
        //'json' => $payload->toArray(),
    ]);
var_dump($response->toArray());

// Guzzle, PHP HTTP client
$client = new GuzzleHttp\Client();
$response = $client->post('http://example.com/graphql', [
    'body' => $payload->toJson(),
    // or
    //'json' => $payload->toArray(),
]);
var_dump((string)$response->getBody());