Skip to content

Commit e9c0980

Browse files
committed
GQL-45: Added support for the client to run query builders natively
- 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
1 parent 302f15c commit e9c0980

File tree

7 files changed

+62
-24
lines changed

7 files changed

+62
-24
lines changed

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,9 +256,8 @@ $builder = (new QueryBuilder('pokemon'))
256256
)
257257
)
258258
);
259-
$gql = $builder->getQuery();
260259
try {
261-
$results = $client->runQuery($gql, true);
260+
$results = $client->runQuery($builder, true);
262261
}
263262
catch (QueryError $exception) {
264263
print_r($exception->getErrorDetails());

examples/query_builder_example.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,9 @@
4343
)
4444
);
4545

46-
$gql = $builder->getQuery();
47-
4846
// Run query to get results
4947
try {
50-
$results = $client->runQuery($gql);
48+
$results = $client->runQuery($builder);
5149
}
5250
catch (QueryError $exception) {
5351

src/Client.php

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
namespace GraphQL;
44

55
use GraphQL\Exception\QueryError;
6+
use GraphQL\QueryBuilder\QueryBuilderInterface;
67
use GuzzleHttp\Exception\ClientException;
8+
use TypeError;
79

810
/**
911
* Class Client
@@ -41,16 +43,22 @@ public function __construct(string $endpointUrl, array $authorizationHeaders = [
4143
}
4244

4345
/**
44-
* @codeCoverageIgnore
45-
*
46-
* @param Query $query
47-
* @param bool $resultsAsArray
46+
* @param Query|QueryBuilderInterface $query
47+
* @param bool $resultsAsArray
4848
*
4949
* @return Results|null
5050
* @throws QueryError
5151
*/
52-
public function runQuery(Query $query, bool $resultsAsArray = false): ?Results
52+
public function runQuery($query, bool $resultsAsArray = false): ?Results
5353
{
54+
if ($query instanceof QueryBuilderInterface) {
55+
$query = $query->getQuery();
56+
}
57+
58+
if (!$query instanceof Query) {
59+
throw new TypeError('Client::runQuery accepts the first argument of type Query or QueryBuilderInterface');
60+
}
61+
5462
return $this->runRawQuery((string) $query, $resultsAsArray);
5563
}
5664

@@ -60,8 +68,6 @@ public function runQuery(Query $query, bool $resultsAsArray = false): ?Results
6068
*
6169
* @return Results|null
6270
* @throws QueryError
63-
*
64-
* TODO: Rename this to runStringQuery on v1.0
6571
*/
6672
public function runRawQuery(string $queryString, $resultsAsArray = false): ?Results
6773
{

src/QueryBuilder/AbstractQueryBuilder.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
*
1212
* @package GraphQL
1313
*/
14-
abstract class AbstractQueryBuilder
14+
abstract class AbstractQueryBuilder implements QueryBuilderInterface
1515
{
1616
/**
1717
* @var Query
@@ -43,7 +43,7 @@ public function __construct(string $queryObject)
4343
/**
4444
* @return Query
4545
*/
46-
protected function getQuery(): Query
46+
public function getQuery(): Query
4747
{
4848
if (empty($this->selectionSet)) {
4949
throw new EmptySelectionSetException(static::class);

src/QueryBuilder/QueryBuilder.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,4 @@ public function setArgument(string $argumentName, $argumentValue)
3535
{
3636
return parent::setArgument($argumentName, $argumentValue);
3737
}
38-
39-
/**
40-
* Changing method visibility to public
41-
*
42-
* @return Query
43-
*/
44-
public function getQuery(): Query
45-
{
46-
return parent::getQuery();
47-
}
4838
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace GraphQL\QueryBuilder;
4+
5+
use GraphQL\Query;
6+
7+
/**
8+
* Interface QueryBuilderInterface
9+
*
10+
* @package GraphQL\QueryBuilder
11+
*/
12+
interface QueryBuilderInterface
13+
{
14+
/**
15+
* @return Query
16+
*/
17+
function getQuery(): Query;
18+
}

tests/ClientTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
use GraphQL\Client;
66
use GraphQL\Exception\QueryError;
7+
use GraphQL\QueryBuilder\QueryBuilder;
8+
use GraphQL\RawObject;
79
use GuzzleHttp\Exception\ClientException;
810
use GuzzleHttp\Exception\ServerException;
911
use GuzzleHttp\Handler\MockHandler;
@@ -12,6 +14,7 @@
1214
use GuzzleHttp\Psr7\Request;
1315
use GuzzleHttp\Psr7\Response;
1416
use PHPUnit\Framework\TestCase;
17+
use TypeError;
1518

1619
/**
1720
* Class ClientTest
@@ -74,6 +77,30 @@ public function testConstructClient()
7477
);
7578
}
7679

80+
/**
81+
* @covers \GraphQL\Client::runQuery
82+
*/
83+
public function testRunQueryBuilder()
84+
{
85+
$this->mockHandler->append(new Response(200, [], json_encode([
86+
'data' => [
87+
'someData'
88+
]
89+
])));
90+
91+
$response = $this->client->runQuery((new QueryBuilder('obj'))->selectField('field'));
92+
$this->assertNotNull($response->getData());
93+
}
94+
95+
/**
96+
* @covers \GraphQL\Client::runQuery
97+
*/
98+
public function testRunInvalidQueryClass()
99+
{
100+
$this->expectException(TypeError::class);
101+
$this->client->runQuery(new RawObject('obj'));
102+
}
103+
77104
/**
78105
* @covers \GraphQL\Client::runRawQuery
79106
*/

0 commit comments

Comments
 (0)