Skip to content

Commit

Permalink
GQL-45: Added support for the client to run query builders natively
Browse files Browse the repository at this point in the history
- Added an QueryBuilderInterface that all query builders must adhere to
- Added support for running query builders in the runQuery method in the client class
- Minor tweaks in the QueryBuilder implementation to allow for adding the interface in a clean way
- Modified examples and README to show how client can be used to run QueryBuilderInterface directly
- Added tests to cover the new implementation
  • Loading branch information
mghoneimy committed Apr 26, 2019
1 parent 302f15c commit e9c0980
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 24 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -256,9 +256,8 @@ $builder = (new QueryBuilder('pokemon'))
)
)
);
$gql = $builder->getQuery();
try {
$results = $client->runQuery($gql, true);
$results = $client->runQuery($builder, true);
}
catch (QueryError $exception) {
print_r($exception->getErrorDetails());
Expand Down
4 changes: 1 addition & 3 deletions examples/query_builder_example.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,9 @@
)
);

$gql = $builder->getQuery();

// Run query to get results
try {
$results = $client->runQuery($gql);
$results = $client->runQuery($builder);
}
catch (QueryError $exception) {

Expand Down
20 changes: 13 additions & 7 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
namespace GraphQL;

use GraphQL\Exception\QueryError;
use GraphQL\QueryBuilder\QueryBuilderInterface;
use GuzzleHttp\Exception\ClientException;
use TypeError;

/**
* Class Client
Expand Down Expand Up @@ -41,16 +43,22 @@ public function __construct(string $endpointUrl, array $authorizationHeaders = [
}

/**
* @codeCoverageIgnore
*
* @param Query $query
* @param bool $resultsAsArray
* @param Query|QueryBuilderInterface $query
* @param bool $resultsAsArray
*
* @return Results|null
* @throws QueryError
*/
public function runQuery(Query $query, bool $resultsAsArray = false): ?Results
public function runQuery($query, bool $resultsAsArray = false): ?Results
{
if ($query instanceof QueryBuilderInterface) {
$query = $query->getQuery();
}

if (!$query instanceof Query) {
throw new TypeError('Client::runQuery accepts the first argument of type Query or QueryBuilderInterface');
}

return $this->runRawQuery((string) $query, $resultsAsArray);
}

Expand All @@ -60,8 +68,6 @@ public function runQuery(Query $query, bool $resultsAsArray = false): ?Results
*
* @return Results|null
* @throws QueryError
*
* TODO: Rename this to runStringQuery on v1.0
*/
public function runRawQuery(string $queryString, $resultsAsArray = false): ?Results
{
Expand Down
4 changes: 2 additions & 2 deletions src/QueryBuilder/AbstractQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
*
* @package GraphQL
*/
abstract class AbstractQueryBuilder
abstract class AbstractQueryBuilder implements QueryBuilderInterface
{
/**
* @var Query
Expand Down Expand Up @@ -43,7 +43,7 @@ public function __construct(string $queryObject)
/**
* @return Query
*/
protected function getQuery(): Query
public function getQuery(): Query
{
if (empty($this->selectionSet)) {
throw new EmptySelectionSetException(static::class);
Expand Down
10 changes: 0 additions & 10 deletions src/QueryBuilder/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,4 @@ public function setArgument(string $argumentName, $argumentValue)
{
return parent::setArgument($argumentName, $argumentValue);
}

/**
* Changing method visibility to public
*
* @return Query
*/
public function getQuery(): Query
{
return parent::getQuery();
}
}
18 changes: 18 additions & 0 deletions src/QueryBuilder/QueryBuilderInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace GraphQL\QueryBuilder;

use GraphQL\Query;

/**
* Interface QueryBuilderInterface
*
* @package GraphQL\QueryBuilder
*/
interface QueryBuilderInterface
{
/**
* @return Query
*/
function getQuery(): Query;
}
27 changes: 27 additions & 0 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use GraphQL\Client;
use GraphQL\Exception\QueryError;
use GraphQL\QueryBuilder\QueryBuilder;
use GraphQL\RawObject;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\ServerException;
use GuzzleHttp\Handler\MockHandler;
Expand All @@ -12,6 +14,7 @@
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
use PHPUnit\Framework\TestCase;
use TypeError;

/**
* Class ClientTest
Expand Down Expand Up @@ -74,6 +77,30 @@ public function testConstructClient()
);
}

/**
* @covers \GraphQL\Client::runQuery
*/
public function testRunQueryBuilder()
{
$this->mockHandler->append(new Response(200, [], json_encode([
'data' => [
'someData'
]
])));

$response = $this->client->runQuery((new QueryBuilder('obj'))->selectField('field'));
$this->assertNotNull($response->getData());
}

/**
* @covers \GraphQL\Client::runQuery
*/
public function testRunInvalidQueryClass()
{
$this->expectException(TypeError::class);
$this->client->runQuery(new RawObject('obj'));
}

/**
* @covers \GraphQL\Client::runRawQuery
*/
Expand Down

0 comments on commit e9c0980

Please sign in to comment.