Skip to content

Add support of GraphQL variables #612

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ The change log describes what is "Added", "Removed", "Changed" or "Fixed" betwee

## 2.6.0 (unreleased)

### Added

- Support for graphql api [variables](https://developer.github.com/v4/guides/forming-calls/#working-with-variables) (#612)

## 2.5.0

### Added
Expand Down
22 changes: 22 additions & 0 deletions doc/graphql.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,25 @@ Wraps [GitHub v4 API (GraphQL API)](http://developer.github.com/v4/).
```php
$rateLimits = $client->api('graphql')->execute($query);
```

#### Use variables

[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.

```php
$query = <<<'QUERY'
query showOrganizationInfo (
$organizationLogin: String!
) {
organization(login: $organizationLogin) {
name
url
}
}
QUERY;
$variables = [
'organizationLogin' => 'KnpLabs'
];

$orgInfo = $client->api('graphql')->execute($query, $variables);
```
6 changes: 5 additions & 1 deletion lib/Github/Api/GraphQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,19 @@ class GraphQL extends AbstractApi

/**
* @param string $query
* @param array $variables
*
* @return array
*/
public function execute($query)
public function execute($query, array $variables = array())
{
$this->acceptHeaderValue = 'application/vnd.github.v4+json';
$params = array(
'query' => $query
);
if (!empty($variables)) {
$params['variables'] = json_encode($variables);
}

return $this->post('/graphql', $params);
}
Expand Down
29 changes: 29 additions & 0 deletions test/Github/Tests/Api/GraphQLTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,35 @@ public function shouldTestGraphQL()
$this->assertEquals('foo', $result);
}

/**
* @test
*/
public function shouldSupportGraphQLVariables()
{
$api = $this->getApiMock();

$api->method('post')
->with('/graphql', $this->arrayHasKey('variables'));

$api->execute('bar', ['variable' => 'foo']);
}

/**
* @test
*/
public function shouldJSONEncodeGraphQLVariables()
{
$api = $this->getApiMock();

$api->method('post')
->with('/graphql', $this->equalTo([
'query'=>'bar',
'variables' => '{"variable":"foo"}'
]));

$api->execute('bar', ['variable' => 'foo']);
}

protected function getApiClass()
{
return \Github\Api\GraphQL::class;
Expand Down