Skip to content
This repository has been archived by the owner on Jul 25, 2023. It is now read-only.

Commit

Permalink
Merge pull request #2 from euautomation/dev
Browse files Browse the repository at this point in the history
Base package complete with guzzle and response wrapper!
  • Loading branch information
eNzyOfficial committed Oct 18, 2016
2 parents 7610125 + bbb0bbc commit 6fd59e2
Show file tree
Hide file tree
Showing 10 changed files with 515 additions and 2 deletions.
12 changes: 11 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,15 @@
"mockery/mockery": "~0.9"
},
"minimum-stability": "dev",
"prefer-stable": true
"prefer-stable": true,
"autoload": {
"psr-4": {
"EUAutomation\\GraphQL\\": "src/"
}
},
"autoload-dev":{
"psr-4":{
"Tests\\": "tests/"
}
}
}
2 changes: 1 addition & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions phpunit.xml
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>
116 changes: 116 additions & 0 deletions src/Client.php
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);
}
}
8 changes: 8 additions & 0 deletions src/Exceptions/GraphQLError.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace EUAutomation\GraphQL\Exceptions;

class GraphQLError extends \Exception
{

}
8 changes: 8 additions & 0 deletions src/Exceptions/GraphQLInvalidResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace EUAutomation\GraphQL\Exceptions;

class GraphQLInvalidResponse extends \Exception
{

}
8 changes: 8 additions & 0 deletions src/Exceptions/GraphQLMissingData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace EUAutomation\GraphQL\Exceptions;

class GraphQLMissingData extends \Exception
{

}
100 changes: 100 additions & 0 deletions src/Response.php
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};
}
}
Loading

0 comments on commit 6fd59e2

Please sign in to comment.