From 6ff427d241a75916caeac10514408a246a52ea10 Mon Sep 17 00:00:00 2001 From: Vladimir Razuvaev Date: Wed, 30 Aug 2017 23:26:45 +0700 Subject: [PATCH] Server: do not raise an error when variables are passed as empty string (#156) --- src/Server/OperationParams.php | 4 ++++ tests/Server/RequestValidationTest.php | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/src/Server/OperationParams.php b/src/Server/OperationParams.php index 905dc9a..1b9ee76 100644 --- a/src/Server/OperationParams.php +++ b/src/Server/OperationParams.php @@ -71,6 +71,10 @@ class OperationParams 'variables' => null ]; + if ($params['variables'] === "") { + $params['variables'] = null; + } + if (is_string($params['variables'])) { $tmp = json_decode($params['variables'], true); if (!json_last_error()) { diff --git a/tests/Server/RequestValidationTest.php b/tests/Server/RequestValidationTest.php index 3f9ca1c..2a335f1 100644 --- a/tests/Server/RequestValidationTest.php +++ b/tests/Server/RequestValidationTest.php @@ -99,6 +99,26 @@ class RequestValidationTest extends \PHPUnit_Framework_TestCase ); } + /** + * @see https://github.com/webonyx/graphql-php/issues/156 + */ + public function testIgnoresNullAndEmptyStringVariables() + { + $query = '{my q}'; + $parsedBody = OperationParams::create([ + 'query' => $query, + 'variables' => null + ]); + $this->assertValid($parsedBody); + + $variables = ""; + $parsedBody = OperationParams::create([ + 'query' => $query, + 'variables' => $variables + ]); + $this->assertValid($parsedBody); + } + public function testFailsWhenVariablesParameterIsNotObject() { $parsedBody = OperationParams::create([