Skip to content

Commit 17ede6d

Browse files
committed
$httpOptions for \GuzzleHttp\Client
1 parent 6fc45f6 commit 17ede6d

File tree

4 files changed

+31
-17
lines changed

4 files changed

+31
-17
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ $gql = $builder->getQuery();
223223
# Constructing The Client
224224

225225
A Client object can easily be instantiated by providing the GraphQL endpoint
226-
URL. The Client constructor also receives an optional "authorizationHeaders"
226+
URL. The Client constructor also receives an optional "httpOptions"
227227
array, which can be used to add authorization headers to all requests being sent
228228
to the GraphQL server.
229229

@@ -232,7 +232,9 @@ Example:
232232
```
233233
$client = new Client(
234234
'http://api.graphql.com',
235-
['Authorization' => 'Basic xyz']
235+
[
236+
'headers' => ['Authorization' => 'Basic xyz']
237+
]
236238
);
237239
```
238240

src/Client.php

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,25 @@ class Client
2020
protected $endpointUrl;
2121

2222
/**
23-
* @var array
23+
* @var \GuzzleHttp\Client
2424
*/
25-
protected $authorizationHeaders;
25+
protected $httpClient;
2626

2727
/**
28-
* @var \GuzzleHttp\Client
28+
* @var array
2929
*/
30-
protected $httpClient;
30+
protected $httpOptions;
3131

3232
/**
3333
* Client constructor.
3434
*
3535
* @param string $endpointUrl
36-
* @param array $authorizationHeaders
36+
* @param array $httpOptions
3737
*/
38-
public function __construct(string $endpointUrl, array $authorizationHeaders = [])
38+
public function __construct(string $endpointUrl, array $httpOptions = [])
3939
{
4040
$this->endpointUrl = $endpointUrl;
41-
$this->authorizationHeaders = $authorizationHeaders;
41+
$this->httpOptions = $httpOptions;
4242
$this->httpClient = new \GuzzleHttp\Client();
4343
}
4444

@@ -73,10 +73,9 @@ public function runQuery($query, bool $resultsAsArray = false, array $variables
7373
*/
7474
public function runRawQuery(string $queryString, $resultsAsArray = false, array $variables = []): ?Results
7575
{
76-
// Set request headers for authorization and content type
77-
if (!empty($this->authorizationHeaders)) {
78-
$options['headers'] = $this->authorizationHeaders;
79-
}
76+
// Set request options for \GuzzleHttp\Client
77+
$options = (!empty($this->httpOptions)) ? $this->httpOptions : [];
78+
8079
$options['headers']['Content-Type'] = 'application/json';
8180

8281
// Convert empty variables array to empty json object

tests/ClientTest.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use GraphQL\QueryBuilder\QueryBuilder;
88
use GraphQL\RawObject;
99
use GuzzleHttp\Exception\ClientException;
10+
use GuzzleHttp\Exception\ConnectException;
1011
use GuzzleHttp\Exception\ServerException;
1112
use GuzzleHttp\Handler\MockHandler;
1213
use GuzzleHttp\HandlerStack;
@@ -62,7 +63,7 @@ public function testConstructClient()
6263
$client = new MockClient('', $handler);
6364
$client->runRawQuery('query_string');
6465

65-
$client = new MockClient('', $handler, ['Authorization' => 'Basic xyz']);
66+
$client = new MockClient('', $handler, [ 'headers' => [ 'Authorization' => 'Basic xyz'] ]);
6667
$client->runRawQuery('query_string');
6768

6869
$client = new MockClient('', $handler);
@@ -232,4 +233,16 @@ public function testInternalServerErrorResponse()
232233
$this->expectException(ServerException::class);
233234
$this->client->runRawQuery('');
234235
}
236+
237+
/**
238+
* @covers \GraphQL\Client::runRawQuery
239+
*/
240+
public function testConnectTimeoutResponse()
241+
{
242+
$this->mockHandler->append(new ConnectException('Time Out', new Request('post', '')));
243+
244+
$this->expectException(ConnectException::class);
245+
$this->client->runRawQuery('');
246+
}
247+
235248
}

tests/MockClient.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ class MockClient extends Client
1616
*
1717
* @param string $endpointUrl
1818
* @param object $handler
19-
* @param array $authorizationHeaders
19+
* @param array $httpOptions
2020
*/
21-
public function __construct(string $endpointUrl, $handler, array $authorizationHeaders = [])
21+
public function __construct(string $endpointUrl, $handler, array $httpOptions = [])
2222
{
23-
parent::__construct($endpointUrl, $authorizationHeaders);
23+
parent::__construct($endpointUrl, $httpOptions);
2424
$this->httpClient = new \GuzzleHttp\Client(['handler' => $handler]);
2525
}
2626
}

0 commit comments

Comments
 (0)