ilexn / graphql-payload-object
Simple create graphql payload
Fund package maintenance!
iLexN
1.1.0
2021-11-26 04:43 UTC
Requires
- php: >=8.1
- ext-json: *
Requires (Dev)
- codedungeon/phpunit-result-printer: ^0.31
- guzzlehttp/guzzle: ^7.4
- ilexn/keep-a-change-log: ^1.3
- infection/infection: ^0.25
- phpbench/phpbench: ^1.2
- phpstan/phpstan: ^1.2
- phpstan/phpstan-phpunit: ^1.0
- phpstan/phpstan-strict-rules: ^1.0
- phpunit/phpunit: ^9.5
- rector/rector: ^0.12
- spatie/phpunit-watcher: ^1.23
- symfony/http-client: ^5.3
- vimeo/psalm: ^4.13
This package is auto-updated.
Last update: 2024-11-12 01:24:21 UTC
README
Simple Object to build graphql payload, and use your favourite http client to send.
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());