Replaced trigger_error with Warning for resolveType warning

This commit is contained in:
Vladimir Razuvaev 2017-07-10 19:38:12 +07:00
parent 9551569ffe
commit ed28deda81
4 changed files with 26 additions and 16 deletions

View File

@ -6,6 +6,7 @@ final class Warning
const NAME_WARNING = 1; const NAME_WARNING = 1;
const ASSIGN_WARNING = 2; const ASSIGN_WARNING = 2;
const CONFIG_WARNING = 4; const CONFIG_WARNING = 4;
const RESOLVE_TYPE_WARNING = 8;
const ALL = 7; 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) static function warnOnce($errorMessage, $warningId)
{ {
if ((self::$enableWarnings & $warningId) > 0 && !isset(self::$warned[$warningId])) { if ((self::$enableWarnings & $warningId) > 0 && !isset(self::$warned[$warningId])) {

View File

@ -3,9 +3,9 @@ namespace GraphQL\Executor;
use GraphQL\Error\Error; use GraphQL\Error\Error;
use GraphQL\Error\InvariantViolation; use GraphQL\Error\InvariantViolation;
use GraphQL\Error\Warning;
use GraphQL\Executor\Promise\Adapter\SyncPromiseAdapter; use GraphQL\Executor\Promise\Adapter\SyncPromiseAdapter;
use GraphQL\Executor\Promise\Promise; use GraphQL\Executor\Promise\Promise;
use GraphQL\GraphQL;
use GraphQL\Language\AST\DocumentNode; use GraphQL\Language\AST\DocumentNode;
use GraphQL\Language\AST\FieldNode; use GraphQL\Language\AST\FieldNode;
use GraphQL\Language\AST\FragmentDefinitionNode; use GraphQL\Language\AST\FragmentDefinitionNode;
@ -1023,14 +1023,13 @@ class Executor
$runtimeType = $returnType->resolveType($result, $exeContext->contextValue, $info); $runtimeType = $returnType->resolveType($result, $exeContext->contextValue, $info);
if (null === $runtimeType) { if (null === $runtimeType) {
if ($returnType instanceof InterfaceType && !$exeContext->schema->getConfig()->descriptor && if ($returnType instanceof InterfaceType && !$exeContext->schema->getConfig()->descriptor) {
!GraphQL::isIgnoredError(GraphQL::WARNING_ON_IMPLEMENTATION_RESOLUTION)) { Warning::warnOnce(
trigger_error(
"GraphQL Interface Type `{$returnType->name}` returned `null` from it`s `resolveType` function ". "GraphQL Interface Type `{$returnType->name}` returned `null` from it`s `resolveType` function ".
'for value: ' . Utils::printSafe($result) . '. Switching to slow resolution method using `isTypeOf` ' . 'for value: ' . Utils::printSafe($result) . '. Switching to slow resolution method using `isTypeOf` ' .
'of all possible implementations. It degrades query performance significantly. '. 'of all possible implementations. It degrades query performance significantly. '.
' Make sure your `resolveType` always returns valid implementation or throws.', ' 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); $runtimeType = self::defaultTypeResolver($result, $exeContext->contextValue, $info, $returnType);

View File

@ -2,6 +2,7 @@
namespace GraphQL\Tests\Executor; namespace GraphQL\Tests\Executor;
use GraphQL\Deferred; use GraphQL\Deferred;
use GraphQL\Error\Warning;
use GraphQL\GraphQL; use GraphQL\GraphQL;
use GraphQL\Schema; use GraphQL\Schema;
use GraphQL\Type\Definition\InterfaceType; 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); $result = GraphQL::execute($schema, $query);
Warning::enable(Warning::RESOLVE_TYPE_WARNING);
$expected = [ $expected = [
'data' => [ 'data' => [
@ -170,7 +173,9 @@ class AbstractPromiseTest extends \PHPUnit_Framework_TestCase
} }
}'; }';
Warning::suppress(Warning::RESOLVE_TYPE_WARNING);
$result = GraphQL::execute($schema, $query); $result = GraphQL::execute($schema, $query);
Warning::enable(Warning::RESOLVE_TYPE_WARNING);
$expected = [ $expected = [
'data' => [ 'data' => [

View File

@ -3,6 +3,7 @@ namespace GraphQL\Tests\Executor;
require_once __DIR__ . '/TestClasses.php'; require_once __DIR__ . '/TestClasses.php';
use GraphQL\Error\Warning;
use GraphQL\Executor\ExecutionResult; use GraphQL\Executor\ExecutionResult;
use GraphQL\Executor\Executor; use GraphQL\Executor\Executor;
use GraphQL\Error\FormattedError; 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)); $result = Executor::execute($schema, Parser::parse($query));
$this->assertEquals($expected, $result); $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)); $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[0]->getPrevious());
$this->assertInstanceOf('PHPUnit_Framework_Error_Warning', $result->errors[1]->getPrevious());
$this->assertEquals( $this->assertEquals(
'GraphQL Interface Type `Pet` returned `null` from it`s `resolveType` function for value: '. '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. '. 'all possible implementations. It degrades query performance significantly. '.
'Make sure your `resolveType` always returns valid implementation or throws.', 'Make sure your `resolveType` always returns valid implementation or throws.',
$result->errors[0]->getMessage()); $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());
} }
/** /**