Skip to content

Commit

Permalink
update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
iLexN committed May 23, 2020
1 parent d930f60 commit a66d577
Showing 1 changed file with 70 additions and 0 deletions.
70 changes: 70 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,71 @@
# graphql-payload-object
> Simple Object to build graphql payload, and use your favourite http client to send.
[![Latest Stable Version](https://poser.pugx.org/ilexn/graphql-payload-object/v/stable)](https://packagist.org/packages/ilexn/graphql-payload-object)
[![Total Downloads](https://poser.pugx.org/ilexn/graphql-payload-object/downloads)](https://packagist.org/packages/ilexn/graphql-payload-object)

[![Build Status](https://travis-ci.org/iLexN/graphql-payload-object.svg?branch=master)](https://travis-ci.org/iLexN/graphql-payload-object)
![GitHub Action](https://github.com/iLexN/graphql-payload-object/workflows/CI%20Check/badge.svg)
[![Coverage Status](https://coveralls.io/repos/github/iLexN/graphql-payload-object/badge.svg?branch=master)](https://coveralls.io/github/iLexN/graphql-payload-object?branch=master)
[![Infection MSI](https://badge.stryker-mutator.io/github.com/iLexN/graphql-payload-object/master)](https://infection.github.io)

## Installation
```sh
composer require ilexn/graphql-payload-object
```

## Usage example
```php
<?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);

$payload->addVariable('key', 'value');


// 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://drupal.docker.localhost:8000/graphql/', [
'body' => $payload->toJson(),
// or
//'json' => $p->toArray(),
]);
var_dump($response->toArray());

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

0 comments on commit a66d577

Please sign in to comment.