From ed28deda817f99370feaad2c325f26850e93a114 Mon Sep 17 00:00:00 2001 From: Vladimir Razuvaev Date: Mon, 10 Jul 2017 19:38:12 +0700 Subject: [PATCH] Replaced trigger_error with Warning for resolveType warning --- src/Error/Warning.php | 13 +++++++++++++ src/Executor/Executor.php | 9 ++++----- tests/Executor/AbstractPromiseTest.php | 5 +++++ tests/Executor/AbstractTest.php | 15 ++++----------- 4 files changed, 26 insertions(+), 16 deletions(-) diff --git a/src/Error/Warning.php b/src/Error/Warning.php index b37abba..12650bb 100644 --- a/src/Error/Warning.php +++ b/src/Error/Warning.php @@ -6,6 +6,7 @@ final class Warning const NAME_WARNING = 1; const ASSIGN_WARNING = 2; const CONFIG_WARNING = 4; + const RESOLVE_TYPE_WARNING = 8; const ALL = 7; @@ -25,6 +26,18 @@ final class Warning } } + public static function enable($enable = true) + { + if (true === $enable) { + self::$enableWarnings = self::ALL; + } else if (false === $enable) { + self::$enableWarnings = 0; + } else { + $enable = (int) $enable; + self::$enableWarnings |= $enable; + } + } + static function warnOnce($errorMessage, $warningId) { if ((self::$enableWarnings & $warningId) > 0 && !isset(self::$warned[$warningId])) { diff --git a/src/Executor/Executor.php b/src/Executor/Executor.php index 5e5ea07..5581363 100644 --- a/src/Executor/Executor.php +++ b/src/Executor/Executor.php @@ -3,9 +3,9 @@ namespace GraphQL\Executor; use GraphQL\Error\Error; use GraphQL\Error\InvariantViolation; +use GraphQL\Error\Warning; use GraphQL\Executor\Promise\Adapter\SyncPromiseAdapter; use GraphQL\Executor\Promise\Promise; -use GraphQL\GraphQL; use GraphQL\Language\AST\DocumentNode; use GraphQL\Language\AST\FieldNode; use GraphQL\Language\AST\FragmentDefinitionNode; @@ -1023,14 +1023,13 @@ class Executor $runtimeType = $returnType->resolveType($result, $exeContext->contextValue, $info); if (null === $runtimeType) { - if ($returnType instanceof InterfaceType && !$exeContext->schema->getConfig()->descriptor && - !GraphQL::isIgnoredError(GraphQL::WARNING_ON_IMPLEMENTATION_RESOLUTION)) { - trigger_error( + if ($returnType instanceof InterfaceType && !$exeContext->schema->getConfig()->descriptor) { + Warning::warnOnce( "GraphQL Interface Type `{$returnType->name}` returned `null` from it`s `resolveType` function ". 'for value: ' . Utils::printSafe($result) . '. Switching to slow resolution method using `isTypeOf` ' . 'of all possible implementations. It degrades query performance significantly. '. ' Make sure your `resolveType` always returns valid implementation or throws.', - E_USER_WARNING + Warning::RESOLVE_TYPE_WARNING ); } $runtimeType = self::defaultTypeResolver($result, $exeContext->contextValue, $info, $returnType); diff --git a/tests/Executor/AbstractPromiseTest.php b/tests/Executor/AbstractPromiseTest.php index 1a6894a..6f1a24c 100644 --- a/tests/Executor/AbstractPromiseTest.php +++ b/tests/Executor/AbstractPromiseTest.php @@ -2,6 +2,7 @@ namespace GraphQL\Tests\Executor; use GraphQL\Deferred; +use GraphQL\Error\Warning; use GraphQL\GraphQL; use GraphQL\Schema; use GraphQL\Type\Definition\InterfaceType; @@ -85,7 +86,9 @@ class AbstractPromiseTest extends \PHPUnit_Framework_TestCase } }'; + Warning::suppress(Warning::RESOLVE_TYPE_WARNING); $result = GraphQL::execute($schema, $query); + Warning::enable(Warning::RESOLVE_TYPE_WARNING); $expected = [ 'data' => [ @@ -170,7 +173,9 @@ class AbstractPromiseTest extends \PHPUnit_Framework_TestCase } }'; + Warning::suppress(Warning::RESOLVE_TYPE_WARNING); $result = GraphQL::execute($schema, $query); + Warning::enable(Warning::RESOLVE_TYPE_WARNING); $expected = [ 'data' => [ diff --git a/tests/Executor/AbstractTest.php b/tests/Executor/AbstractTest.php index 6f4a9b0..1bb2ecb 100644 --- a/tests/Executor/AbstractTest.php +++ b/tests/Executor/AbstractTest.php @@ -3,6 +3,7 @@ namespace GraphQL\Tests\Executor; require_once __DIR__ . '/TestClasses.php'; +use GraphQL\Error\Warning; use GraphQL\Executor\ExecutionResult; use GraphQL\Executor\Executor; use GraphQL\Error\FormattedError; @@ -89,15 +90,14 @@ class AbstractTest extends \PHPUnit_Framework_TestCase ] ]); - GraphQL::setIgnoreError(GraphQL::WARNING_ON_IMPLEMENTATION_RESOLUTION); + Warning::suppress(Warning::RESOLVE_TYPE_WARNING); $result = Executor::execute($schema, Parser::parse($query)); $this->assertEquals($expected, $result); - GraphQL::setIgnoreError(GraphQL::WARNING_ON_IMPLEMENTATION_RESOLUTION, false); + Warning::enable(Warning::RESOLVE_TYPE_WARNING); $result = Executor::execute($schema, Parser::parse($query)); - $this->assertEquals(2, count($result->errors)); + $this->assertEquals(1, count($result->errors)); $this->assertInstanceOf('PHPUnit_Framework_Error_Warning', $result->errors[0]->getPrevious()); - $this->assertInstanceOf('PHPUnit_Framework_Error_Warning', $result->errors[1]->getPrevious()); $this->assertEquals( 'GraphQL Interface Type `Pet` returned `null` from it`s `resolveType` function for value: '. @@ -105,13 +105,6 @@ class AbstractTest extends \PHPUnit_Framework_TestCase 'all possible implementations. It degrades query performance significantly. '. 'Make sure your `resolveType` always returns valid implementation or throws.', $result->errors[0]->getMessage()); - - $this->assertEquals( - 'GraphQL Interface Type `Pet` returned `null` from it`s `resolveType` function for value: '. - 'instance of GraphQL\Tests\Executor\Cat. Switching to slow resolution method using `isTypeOf` of '. - 'all possible implementations. It degrades query performance significantly. '. - 'Make sure your `resolveType` always returns valid implementation or throws.', - $result->errors[1]->getMessage()); } /**