Skip to content

Commit c8a6642

Browse files
authored
Merge pull request mghoneimy#14 from actionm/dev-master
Added support for setting $httpOptions for \GuzzleHttp\Client through the \GraphQL\Client
2 parents e8a17e1 + 9dfde7c commit c8a6642

File tree

4 files changed

+73
-7
lines changed

4 files changed

+73
-7
lines changed

README.md

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,9 @@ $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.
227+
228+
The Client constructor also receives an optional "authorizationHeaders"
227229
array, which can be used to add authorization headers to all requests being sent
228230
to the GraphQL server.
229231

@@ -236,6 +238,33 @@ $client = new Client(
236238
);
237239
```
238240

241+
242+
The Client constructor also receives an optional "httpOptions" array, which **overrides** the "authorizationHeaders" and can be used to add custom [Guzzle HTTP Client request options](https://guzzle.readthedocs.io/en/latest/request-options.html).
243+
244+
Example:
245+
246+
```
247+
$client = new Client(
248+
'http://api.graphql.com',
249+
[],
250+
[
251+
'connect_timeout' => 5,
252+
'timeout' => 5,
253+
'headers' => [
254+
'Authorization' => 'Basic xyz'
255+
'User-Agent' => 'testing/1.0',
256+
],
257+
'proxy' => [
258+
'http' => 'tcp://localhost:8125', // Use this proxy with "http"
259+
'https' => 'tcp://localhost:9124', // Use this proxy with "https",
260+
'no' => ['.mit.edu', 'foo.com'] // Don't use a proxy with these
261+
],
262+
'cert' => ['/path/server.pem', 'password']
263+
...
264+
]
265+
);
266+
```
267+
239268
# Running Queries
240269

241270
## Result Formatting

src/Client.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,25 @@ class Client
2929
*/
3030
protected $httpClient;
3131

32+
/**
33+
* @var array
34+
*/
35+
protected $httpOptions;
36+
37+
3238
/**
3339
* Client constructor.
3440
*
3541
* @param string $endpointUrl
36-
* @param array $authorizationHeaders
42+
* @param array $authorizationHeaders
43+
* @param array $httpOptions
3744
*/
38-
public function __construct(string $endpointUrl, array $authorizationHeaders = [])
45+
public function __construct(string $endpointUrl, array $authorizationHeaders = [], array $httpOptions = [])
3946
{
4047
$this->endpointUrl = $endpointUrl;
4148
$this->authorizationHeaders = $authorizationHeaders;
4249
$this->httpClient = new \GuzzleHttp\Client();
50+
$this->httpOptions = $httpOptions;
4351
}
4452

4553
/**
@@ -77,6 +85,12 @@ public function runRawQuery(string $queryString, $resultsAsArray = false, array
7785
if (!empty($this->authorizationHeaders)) {
7886
$options['headers'] = $this->authorizationHeaders;
7987
}
88+
89+
// Set request options for \GuzzleHttp\Client
90+
if (!empty($this->httpOptions)) {
91+
$options = $this->httpOptions;
92+
}
93+
8094
$options['headers']['Content-Type'] = 'application/json';
8195

8296
// Convert empty variables array to empty json object

tests/ClientTest.php

Lines changed: 23 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;
@@ -58,6 +59,7 @@ public function testConstructClient()
5859
$mockHandler->append(new Response(200));
5960
$mockHandler->append(new Response(200));
6061
$mockHandler->append(new Response(200));
62+
$mockHandler->append(new Response(200));
6163

6264
$client = new MockClient('', $handler);
6365
$client->runRawQuery('query_string');
@@ -68,13 +70,16 @@ public function testConstructClient()
6870
$client = new MockClient('', $handler);
6971
$client->runRawQuery('query_string', false, ['name' => 'val']);
7072

73+
$client = new MockClient('', $handler, ['Authorization' => 'Basic xyz'], ['headers' => [ 'Authorization' => 'Basic zyx', 'User-Agent' => 'test' ]]);
74+
$client->runRawQuery('query_string');
75+
7176
/** @var Request $firstRequest */
7277
$firstRequest = $container[0]['request'];
7378
$this->assertEquals('{"query":"query_string","variables":{}}', $firstRequest->getBody()->getContents());
7479

7580
/** @var Request $thirdRequest */
7681
$thirdRequest = $container[1]['request'];
77-
$this->assertNotNull($thirdRequest->getHeader('Authorization'));
82+
$this->assertNotEmpty($thirdRequest->getHeader('Authorization'));
7883
$this->assertEquals(
7984
['Basic xyz'],
8085
$thirdRequest->getHeader('Authorization')
@@ -83,6 +88,13 @@ public function testConstructClient()
8388
/** @var Request $secondRequest */
8489
$secondRequest = $container[2]['request'];
8590
$this->assertEquals('{"query":"query_string","variables":{"name":"val"}}', $secondRequest->getBody()->getContents());
91+
92+
/** @var Request $fourthRequest */
93+
$fourthRequest = $container[3]['request'];
94+
$this->assertNotEmpty($fourthRequest->getHeader('Authorization'));
95+
$this->assertNotEmpty($fourthRequest->getHeader('User-Agent'));
96+
$this->assertEquals(['Basic zyx'], $fourthRequest->getHeader('Authorization'));
97+
$this->assertEquals(['test'], $fourthRequest->getHeader('User-Agent'));
8698
}
8799

88100
/**
@@ -232,4 +244,14 @@ public function testInternalServerErrorResponse()
232244
$this->expectException(ServerException::class);
233245
$this->client->runRawQuery('');
234246
}
247+
248+
/**
249+
* @covers \GraphQL\Client::runRawQuery
250+
*/
251+
public function testConnectTimeoutResponse()
252+
{
253+
$this->mockHandler->append(new ConnectException('Time Out', new Request('post', '')));
254+
$this->expectException(ConnectException::class);
255+
$this->client->runRawQuery('');
256+
}
235257
}

tests/MockClient.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@ class MockClient extends Client
1616
*
1717
* @param string $endpointUrl
1818
* @param object $handler
19-
* @param array $authorizationHeaders
19+
* @param array $authorizationHeaders
20+
* @param array $httpOptions
2021
*/
21-
public function __construct(string $endpointUrl, $handler, array $authorizationHeaders = [])
22+
public function __construct(string $endpointUrl, $handler, array $authorizationHeaders = [], array $httpOptions = [])
2223
{
23-
parent::__construct($endpointUrl, $authorizationHeaders);
24+
parent::__construct($endpointUrl, $authorizationHeaders, $httpOptions);
2425
$this->httpClient = new \GuzzleHttp\Client(['handler' => $handler]);
2526
}
2627
}

0 commit comments

Comments
 (0)