Skip to content

Commit 37e2c30

Browse files
committed
Add support of GraphQL variables
1 parent 30c7d5c commit 37e2c30

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

lib/Github/Api/GraphQL.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,19 @@ class GraphQL extends AbstractApi
1616

1717
/**
1818
* @param string $query
19+
* @param array $variables
1920
*
2021
* @return array
2122
*/
22-
public function execute($query)
23+
public function execute($query, array $variables = null)
2324
{
2425
$this->acceptHeaderValue = 'application/vnd.github.v4+json';
2526
$params = array(
2627
'query' => $query
2728
);
29+
if (!empty($variables)) {
30+
$params['variables'] = json_encode($variables);
31+
}
2832

2933
return $this->post('/graphql', $params);
3034
}

test/Github/Tests/Api/GraphQLTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,35 @@ public function shouldTestGraphQL()
2121
$this->assertEquals('foo', $result);
2222
}
2323

24+
/**
25+
* @test
26+
*/
27+
public function shouldSupportGraphQLVariables()
28+
{
29+
$api = $this->getApiMock();
30+
31+
$api->method('post')
32+
->with('/graphql', $this->arrayHasKey('variables'));
33+
34+
$api->execute('bar', ['variable' => 'foo']);
35+
}
36+
37+
/**
38+
* @test
39+
*/
40+
public function shouldJSONEncodeGraphQLVariables()
41+
{
42+
$api = $this->getApiMock();
43+
44+
$api->method('post')
45+
->with('/graphql', $this->equalTo([
46+
'query'=>'bar',
47+
'variables' => '{"variable":"foo"}'
48+
]));
49+
50+
$api->execute('bar', ['variable' => 'foo']);
51+
}
52+
2453
protected function getApiClass()
2554
{
2655
return \Github\Api\GraphQL::class;

0 commit comments

Comments
 (0)