Skip to content

Commit 8e8998f

Browse files
authored
feat: add request method as client construct argument (mghoneimy#40)
Add support for sending requests with other HTTP methods than POST
1 parent 072341e commit 8e8998f

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed

src/Client.php

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
use GraphQL\QueryBuilder\QueryBuilderInterface;
77
use GraphQL\Util\GuzzleAdapter;
88
use GuzzleHttp\Exception\ClientException;
9+
use GuzzleHttp\Psr7;
910
use GuzzleHttp\Psr7\Request;
1011
use Psr\Http\Client\ClientInterface;
1112
use TypeError;
12-
use GuzzleHttp\Psr7;
1313

1414
/**
1515
* Class Client
@@ -33,16 +33,27 @@ class Client
3333
*/
3434
protected $httpHeaders;
3535

36+
/**
37+
* @var string
38+
*/
39+
protected $requestMethod;
40+
3641
/**
3742
* Client constructor.
3843
*
3944
* @param string $endpointUrl
4045
* @param array $authorizationHeaders
4146
* @param array $httpOptions
4247
* @param ClientInterface $httpClient
48+
* @param string $requestMethod
4349
*/
44-
public function __construct(string $endpointUrl, array $authorizationHeaders = [], array $httpOptions = [], ClientInterface $httpClient = null)
45-
{
50+
public function __construct(
51+
string $endpointUrl,
52+
array $authorizationHeaders = [],
53+
array $httpOptions = [],
54+
ClientInterface $httpClient = null,
55+
string $requestMethod = 'POST'
56+
) {
4657
$headers = array_merge(
4758
$authorizationHeaders,
4859
$httpOptions['headers'] ?? [],
@@ -59,6 +70,7 @@ public function __construct(string $endpointUrl, array $authorizationHeaders = [
5970
$this->endpointUrl = $endpointUrl;
6071
$this->httpClient = $httpClient ?? new GuzzleAdapter(new \GuzzleHttp\Client($httpOptions));
6172
$this->httpHeaders = $headers;
73+
$this->requestMethod = $requestMethod;
6274
}
6375

6476
/**
@@ -93,7 +105,7 @@ public function runQuery($query, bool $resultsAsArray = false, array $variables
93105
*/
94106
public function runRawQuery(string $queryString, $resultsAsArray = false, array $variables = []): Results
95107
{
96-
$request = new Request('POST', $this->endpointUrl);
108+
$request = new Request($this->requestMethod, $this->endpointUrl);
97109

98110
foreach($this->httpHeaders as $header => $value) {
99111
$request = $request->withHeader($header, $value);
@@ -122,4 +134,4 @@ public function runRawQuery(string $queryString, $resultsAsArray = false, array
122134
// Parse response to extract results
123135
return new Results($response, $resultsAsArray);
124136
}
125-
}
137+
}

tests/ClientTest.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
use GraphQL\Exception\QueryError;
77
use GraphQL\QueryBuilder\QueryBuilder;
88
use GraphQL\RawObject;
9-
use GraphQL\Util\GuzzleAdapter;
109
use GuzzleHttp\Exception\ClientException;
1110
use GuzzleHttp\Exception\ConnectException;
1211
use GuzzleHttp\Exception\ServerException;
@@ -61,6 +60,7 @@ public function testConstructClient()
6160
$mockHandler->append(new Response(200));
6261
$mockHandler->append(new Response(200));
6362
$mockHandler->append(new Response(200));
63+
$mockHandler->append(new Response(200));
6464

6565
$client = new Client('', [], ['handler' => $handler]);
6666
$client->runRawQuery('query_string');
@@ -74,9 +74,13 @@ public function testConstructClient()
7474
$client = new Client('', ['Authorization' => 'Basic xyz'], ['handler' => $handler, 'headers' => [ 'Authorization' => 'Basic zyx', 'User-Agent' => 'test' ]]);
7575
$client->runRawQuery('query_string');
7676

77+
$client = new Client('', ['Authorization' => 'Basic xyz'], ['handler' => $handler, 'headers' => [ 'Authorization' => 'Basic zyx', 'User-Agent' => 'test' ]], null, 'GET');
78+
$client->runRawQuery('query_string');
79+
7780
/** @var Request $firstRequest */
7881
$firstRequest = $container[0]['request'];
7982
$this->assertEquals('{"query":"query_string","variables":{}}', $firstRequest->getBody()->getContents());
83+
$this->assertSame('POST', $firstRequest->getMethod());
8084

8185
/** @var Request $thirdRequest */
8286
$thirdRequest = $container[1]['request'];
@@ -96,6 +100,10 @@ public function testConstructClient()
96100
$this->assertNotEmpty($fourthRequest->getHeader('User-Agent'));
97101
$this->assertEquals(['Basic zyx'], $fourthRequest->getHeader('Authorization'));
98102
$this->assertEquals(['test'], $fourthRequest->getHeader('User-Agent'));
103+
104+
/** @var Request $fifthRequest */
105+
$fifthRequest = $container[4]['request'];
106+
$this->assertSame('GET', $fifthRequest->getMethod());
99107
}
100108

101109
/**
@@ -255,4 +263,4 @@ public function testConnectTimeoutResponse()
255263
$this->expectException(ConnectException::class);
256264
$this->client->runRawQuery('');
257265
}
258-
}
266+
}

0 commit comments

Comments
 (0)