Skip to content

Commit 2577b0c

Browse files
committed
GQL-47: Tweaked Client behavior when constructing variables map
- Modified the client to send an empty JSON object in the request instead of an empty array if variables is not defined - Added test cases to capture the changes
1 parent 4a48bde commit 2577b0c

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

src/Client.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ public function runRawQuery(string $queryString, $resultsAsArray = false, array
7979
}
8080
$options['headers']['Content-Type'] = 'application/json';
8181

82+
// Convert empty variables array to empty json object
83+
if (empty($variables)) $variables = (object) null;
8284
// Set query in the request body
8385
$bodyArray = ['query' => (string) $queryString, 'variables' => $variables];
8486
$options['body'] = json_encode($bodyArray);

tests/ClientTest.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ public function testConstructClient()
5555
$history = Middleware::history($container);
5656
$handler->push($history);
5757

58+
$mockHandler->append(new Response(200));
5859
$mockHandler->append(new Response(200));
5960
$mockHandler->append(new Response(200));
6061

@@ -64,17 +65,24 @@ public function testConstructClient()
6465
$client = new MockClient('', $handler, ['Authorization' => 'Basic xyz']);
6566
$client->runRawQuery('query_string');
6667

68+
$client = new MockClient('', $handler);
69+
$client->runRawQuery('query_string', false, ['name' => 'val']);
70+
6771
/** @var Request $firstRequest */
6872
$firstRequest = $container[0]['request'];
69-
$this->assertEquals('{"query":"query_string","variables":[]}', $firstRequest->getBody()->getContents());
73+
$this->assertEquals('{"query":"query_string","variables":{}}', $firstRequest->getBody()->getContents());
7074

71-
/** @var Request $secondRequest */
72-
$secondRequest = $container[1]['request'];
73-
$this->assertNotNull($secondRequest->getHeader('Authorization'));
75+
/** @var Request $thirdRequest */
76+
$thirdRequest = $container[1]['request'];
77+
$this->assertNotNull($thirdRequest->getHeader('Authorization'));
7478
$this->assertEquals(
7579
['Basic xyz'],
76-
$secondRequest->getHeader('Authorization')
80+
$thirdRequest->getHeader('Authorization')
7781
);
82+
83+
/** @var Request $secondRequest */
84+
$secondRequest = $container[2]['request'];
85+
$this->assertEquals('{"query":"query_string","variables":{"name":"val"}}', $secondRequest->getBody()->getContents());
7886
}
7987

8088
/**

0 commit comments

Comments
 (0)