Skip to content

Commit 21df374

Browse files
authored
Merge pull request #612 from vkublytskyi/master
Add support of GraphQL variables
2 parents 30c7d5c + d0365b5 commit 21df374

File tree

4 files changed

+60
-1
lines changed

4 files changed

+60
-1
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ The change log describes what is "Added", "Removed", "Changed" or "Fixed" betwee
44

55
## 2.6.0 (unreleased)
66

7+
### Added
8+
9+
- Support for graphql api [variables](https://developer.github.com/v4/guides/forming-calls/#working-with-variables) (#612)
10+
711
## 2.5.0
812

913
### Added

doc/graphql.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,25 @@ Wraps [GitHub v4 API (GraphQL API)](http://developer.github.com/v4/).
88
```php
99
$rateLimits = $client->api('graphql')->execute($query);
1010
```
11+
12+
#### Use variables
13+
14+
[Variables](https://developer.github.com/v4/guides/forming-calls/#working-with-variables) allow specifying of requested data without dynamical change of a query on a client side.
15+
16+
```php
17+
$query = <<<'QUERY'
18+
query showOrganizationInfo (
19+
$organizationLogin: String!
20+
) {
21+
organization(login: $organizationLogin) {
22+
name
23+
url
24+
}
25+
}
26+
QUERY;
27+
$variables = [
28+
'organizationLogin' => 'KnpLabs'
29+
];
30+
31+
$orgInfo = $client->api('graphql')->execute($query, $variables);
32+
```

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 = array())
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)