ilexn/graphql-payload-object

Simple create graphql payload

1.1.0 2021-11-26 04:43 UTC

README

Simple Object to build graphql payload, and use your favourite http client to send.

Latest Stable Version Total Downloads

GitHub Action Coverage Status Mutation testing badge

Installation

composer require ilexn/graphql-payload-object

Usage example

<?php
declare(strict_types=1);

require_once __DIR__ . '/../vendor/autoload.php';

$query = <<<'QUERY'
query HeroNameAndFriends($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());