This repository has been archived by the owner on Jul 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from euautomation/dev
Base package complete with guzzle and response wrapper!
- Loading branch information
Showing
10 changed files
with
515 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<phpunit backupGlobals="false" | ||
backupStaticAttributes="false" | ||
colors="true" | ||
convertErrorsToExceptions="true" | ||
convertNoticesToExceptions="true" | ||
convertWarningsToExceptions="true" | ||
processIsolation="false" | ||
stopOnFailure="false" | ||
syntaxCheck="false"> | ||
<testsuites> | ||
<testsuite name="Package Test Suite"> | ||
<directory suffix="Test.php">./tests</directory> | ||
</testsuite> | ||
</testsuites> | ||
<filter> | ||
<whitelist processUncoveredFilesFromWhitelist="true"> | ||
<directory suffix=".php">./src</directory> | ||
</whitelist> | ||
</filter> | ||
</phpunit> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
<?php | ||
|
||
namespace EUAutomation\GraphQL; | ||
|
||
use EUAutomation\GraphQL\Exceptions\GraphQLInvalidResponse; | ||
use EUAutomation\GraphQL\Exceptions\GraphQLMissingData; | ||
|
||
class Client | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
protected $url; | ||
|
||
/** | ||
* @var \GuzzleHttp\Client | ||
*/ | ||
protected $guzzle; | ||
|
||
/** | ||
* Client constructor. | ||
* | ||
* @param string $url | ||
*/ | ||
public function __construct($url) | ||
{ | ||
$this->url = $url; | ||
$this->guzzle = new \GuzzleHttp\Client(); | ||
} | ||
|
||
/** | ||
* Set the URL to query against | ||
* | ||
* @param string $url | ||
*/ | ||
public function setUrl($url) | ||
{ | ||
$this->url = $url; | ||
} | ||
|
||
/** | ||
* Set an instance of guzzle to use | ||
* | ||
* @param \GuzzleHttp\Client $guzzle | ||
*/ | ||
public function setGuzzle(\GuzzleHttp\Client $guzzle) | ||
{ | ||
$this->guzzle = $guzzle; | ||
} | ||
|
||
/** | ||
* Make a GraphQL Request and get the raw guzzle response. | ||
* | ||
* @param string $query | ||
* @param array $variables | ||
* @param array $headers | ||
* | ||
* @return mixed|\Psr\Http\Message\ResponseInterface | ||
*/ | ||
public function raw($query, $variables = [], $headers = []) | ||
{ | ||
return $this->guzzle->request('POST', $this->url, [ | ||
'json' => [ | ||
'query' => $query, | ||
'variables' => $variables | ||
], | ||
'headers' => $headers | ||
]); | ||
} | ||
|
||
/** | ||
* Make a GraphQL Request and get the response body in JSON form. | ||
* | ||
* @param string $query | ||
* @param array $variables | ||
* @param array $headers | ||
* @param bool $assoc | ||
* | ||
* @return mixed | ||
* | ||
* @throws GraphQLInvalidResponse | ||
* @throws GraphQLMissingData | ||
*/ | ||
public function json($query, $variables = [], $headers = [], $assoc = false) | ||
{ | ||
$response = $this->raw($query, $variables, $headers); | ||
|
||
$responseJson = json_decode($response->getBody()->getContents(), $assoc); | ||
|
||
if ($responseJson === null) { | ||
throw new GraphQLInvalidResponse('GraphQL did not provide a valid JSON response. Please make sure you are pointing at the correct URL.'); | ||
} else if (!isset($responseJson->data)) { | ||
throw new GraphQLMissingData('There was an error with the GraphQL response, no data key was found.'); | ||
} | ||
|
||
return $responseJson; | ||
} | ||
|
||
/** | ||
* Make a GraphQL Request and get the guzzle response . | ||
* | ||
* @param string $query | ||
* @param array $variables | ||
* @param array $headers | ||
* | ||
* @return Response | ||
* | ||
* @throws \Exception | ||
*/ | ||
public function response($query, $variables = [], $headers = []) | ||
{ | ||
$response = $this->json($query, $variables, $headers); | ||
|
||
return new Response($response); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace EUAutomation\GraphQL\Exceptions; | ||
|
||
class GraphQLError extends \Exception | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace EUAutomation\GraphQL\Exceptions; | ||
|
||
class GraphQLInvalidResponse extends \Exception | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace EUAutomation\GraphQL\Exceptions; | ||
|
||
class GraphQLMissingData extends \Exception | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
<?php | ||
|
||
namespace EUAutomation\GraphQL; | ||
|
||
class Response | ||
{ | ||
/** | ||
* @var | ||
*/ | ||
protected $data; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
protected $errors = []; | ||
|
||
/** | ||
* Response constructor. | ||
* | ||
* @param $data | ||
* @param array $errors | ||
*/ | ||
public function __construct($response) | ||
{ | ||
$this->data = $response->data; | ||
|
||
if (isset($response->errors)) { | ||
$this->errors = $response->errors; | ||
} | ||
} | ||
|
||
/** | ||
* Return all the data | ||
* | ||
* @return mixed | ||
*/ | ||
public function all() | ||
{ | ||
return $this->data; | ||
} | ||
|
||
/** | ||
* Get errors returned from query | ||
* | ||
* @return array | ||
*/ | ||
public function errors() | ||
{ | ||
return $this->errors; | ||
} | ||
|
||
/** | ||
* Check if there are any errors | ||
* | ||
* @return bool | ||
*/ | ||
public function hasErrors() | ||
{ | ||
return (bool)count($this->errors()); | ||
} | ||
|
||
/** | ||
* Return the data as a JSON string | ||
* | ||
* @return string | ||
*/ | ||
public function toJson() | ||
{ | ||
return json_encode($this->data); | ||
} | ||
|
||
/** | ||
* @param string $name | ||
* | ||
* @return mixed | ||
*/ | ||
public function __get($name) | ||
{ | ||
return $this->data->{$name}; | ||
} | ||
|
||
/** | ||
* @param string $name | ||
* @param mixed $value | ||
*/ | ||
public function __set($name, $value) | ||
{ | ||
$this->data->{$name} = $value; | ||
} | ||
|
||
/** | ||
* @param string $name | ||
* | ||
* @return mixed | ||
*/ | ||
public function __isset($name) | ||
{ | ||
return $this->data->{$name}; | ||
} | ||
} |
Oops, something went wrong.