From 37e2c303fa8ff7bd18bed9640813199a0c393503 Mon Sep 17 00:00:00 2001 From: Volodymyr Kublytskyi Date: Thu, 6 Jul 2017 17:14:50 +0300 Subject: [PATCH 1/3] Add support of GraphQL variables --- lib/Github/Api/GraphQL.php | 6 +++++- test/Github/Tests/Api/GraphQLTest.php | 29 +++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/lib/Github/Api/GraphQL.php b/lib/Github/Api/GraphQL.php index 6a112099e4c..ea9271030e7 100644 --- a/lib/Github/Api/GraphQL.php +++ b/lib/Github/Api/GraphQL.php @@ -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 = null) { $this->acceptHeaderValue = 'application/vnd.github.v4+json'; $params = array( 'query' => $query ); + if (!empty($variables)) { + $params['variables'] = json_encode($variables); + } return $this->post('/graphql', $params); } diff --git a/test/Github/Tests/Api/GraphQLTest.php b/test/Github/Tests/Api/GraphQLTest.php index c209a733b75..e2f2d692c84 100644 --- a/test/Github/Tests/Api/GraphQLTest.php +++ b/test/Github/Tests/Api/GraphQLTest.php @@ -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; From 5fc4032d1b5068af6f407fe962cfcabc8d3cba04 Mon Sep 17 00:00:00 2001 From: Volodymyr Kublytskyi Date: Thu, 6 Jul 2017 18:50:21 +0300 Subject: [PATCH 2/3] Add support of GraphQL variables - fixed default value to be same type if not specified --- lib/Github/Api/GraphQL.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Github/Api/GraphQL.php b/lib/Github/Api/GraphQL.php index ea9271030e7..a71bea4c8ea 100644 --- a/lib/Github/Api/GraphQL.php +++ b/lib/Github/Api/GraphQL.php @@ -20,7 +20,7 @@ class GraphQL extends AbstractApi * * @return array */ - public function execute($query, array $variables = null) + public function execute($query, array $variables = array()) { $this->acceptHeaderValue = 'application/vnd.github.v4+json'; $params = array( From d0365b5b7f4b6ffa3a8b4b01507f7556b1743355 Mon Sep 17 00:00:00 2001 From: Volodymyr Kublytskyi Date: Fri, 7 Jul 2017 18:08:03 +0300 Subject: [PATCH 3/3] Document support of GraphQL variables --- CHANGELOG.md | 4 ++++ doc/graphql.md | 22 ++++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index d167e5c2a1a..40b2dff1928 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/doc/graphql.md b/doc/graphql.md index 85def0f556b..89080b24227 100644 --- a/doc/graphql.md +++ b/doc/graphql.md @@ -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); +``` \ No newline at end of file