From e07c86bd5ee4cb288234235da3ec4c52186b1ad9 Mon Sep 17 00:00:00 2001 From: leocavalcante Date: Sun, 7 May 2017 19:21:20 -0300 Subject: [PATCH 1/2] Default resolve callables --- src/Executor/Executor.php | 2 +- tests/Executor/ResolveTest.php | 26 +++++++++++++++++++++++++- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/Executor/Executor.php b/src/Executor/Executor.php index 7ef5fa0..a52f239 100644 --- a/src/Executor/Executor.php +++ b/src/Executor/Executor.php @@ -918,7 +918,7 @@ class Executor } } - return $property instanceof \Closure ? $property($source, $args, $context) : $property; + return is_callable($property) ? $property($source, $args, $context) : $property; } /** diff --git a/tests/Executor/ResolveTest.php b/tests/Executor/ResolveTest.php index f3f6cf4..a459c9e 100644 --- a/tests/Executor/ResolveTest.php +++ b/tests/Executor/ResolveTest.php @@ -60,6 +60,30 @@ class ResolveTest extends \PHPUnit_Framework_TestCase ); } + /** + * @it default function calls callables + */ + public function testDefaultFunctionCallsCallables() + { + $schema = $this->buildSchema(['type' => Type::string()]); + $_secret = 'secretValue' . uniqid(); + + $source = [ + 'test' => new class($_secret) { + public function __construct($_secret) { + $this->_secret = $_secret; + } + public function __invoke() { + return $this->_secret; + } + } + ]; + $this->assertEquals( + ['data' => ['test' => $_secret]], + GraphQL::execute($schema, '{ test }', $source) + ); + } + /** * @it default function passes args and context */ @@ -114,4 +138,4 @@ class ResolveTest extends \PHPUnit_Framework_TestCase GraphQL::execute($schema, '{ test(aInt: -123, aStr: "String!") }', 'Source!') ); } -} \ No newline at end of file +} From 8a5f337469c47f7f8696de7226cb9f7d58bd7f44 Mon Sep 17 00:00:00 2001 From: leocavalcante Date: Sun, 7 May 2017 19:42:59 -0300 Subject: [PATCH 2/2] Extract anonymous class --- tests/Executor/ResolveTest.php | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/tests/Executor/ResolveTest.php b/tests/Executor/ResolveTest.php index a459c9e..49e23b6 100644 --- a/tests/Executor/ResolveTest.php +++ b/tests/Executor/ResolveTest.php @@ -69,14 +69,7 @@ class ResolveTest extends \PHPUnit_Framework_TestCase $_secret = 'secretValue' . uniqid(); $source = [ - 'test' => new class($_secret) { - public function __construct($_secret) { - $this->_secret = $_secret; - } - public function __invoke() { - return $this->_secret; - } - } + 'test' => new ResolveTestCallableFixture($_secret) ]; $this->assertEquals( ['data' => ['test' => $_secret]], @@ -139,3 +132,18 @@ class ResolveTest extends \PHPUnit_Framework_TestCase ); } } + +class ResolveTestCallableFixture +{ + private $value; + + public function __construct($value) + { + $this->value = $value; + } + + public function __invoke($root, $args, $context) + { + return $this->value; + } +}