Skip to content

Commit ed28ded

Browse files
committed
Replaced trigger_error with Warning for resolveType warning
1 parent 9551569 commit ed28ded

File tree

4 files changed

+26
-16
lines changed

4 files changed

+26
-16
lines changed

src/Error/Warning.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ final class Warning
66
const NAME_WARNING = 1;
77
const ASSIGN_WARNING = 2;
88
const CONFIG_WARNING = 4;
9+
const RESOLVE_TYPE_WARNING = 8;
910

1011
const ALL = 7;
1112

@@ -25,6 +26,18 @@ static function suppress($suppress = true)
2526
}
2627
}
2728

29+
public static function enable($enable = true)
30+
{
31+
if (true === $enable) {
32+
self::$enableWarnings = self::ALL;
33+
} else if (false === $enable) {
34+
self::$enableWarnings = 0;
35+
} else {
36+
$enable = (int) $enable;
37+
self::$enableWarnings |= $enable;
38+
}
39+
}
40+
2841
static function warnOnce($errorMessage, $warningId)
2942
{
3043
if ((self::$enableWarnings & $warningId) > 0 && !isset(self::$warned[$warningId])) {

src/Executor/Executor.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33

44
use GraphQL\Error\Error;
55
use GraphQL\Error\InvariantViolation;
6+
use GraphQL\Error\Warning;
67
use GraphQL\Executor\Promise\Adapter\SyncPromiseAdapter;
78
use GraphQL\Executor\Promise\Promise;
8-
use GraphQL\GraphQL;
99
use GraphQL\Language\AST\DocumentNode;
1010
use GraphQL\Language\AST\FieldNode;
1111
use GraphQL\Language\AST\FragmentDefinitionNode;
@@ -1023,14 +1023,13 @@ private function completeAbstractValue(AbstractType $returnType, $fieldNodes, Re
10231023
$runtimeType = $returnType->resolveType($result, $exeContext->contextValue, $info);
10241024

10251025
if (null === $runtimeType) {
1026-
if ($returnType instanceof InterfaceType && !$exeContext->schema->getConfig()->descriptor &&
1027-
!GraphQL::isIgnoredError(GraphQL::WARNING_ON_IMPLEMENTATION_RESOLUTION)) {
1028-
trigger_error(
1026+
if ($returnType instanceof InterfaceType && !$exeContext->schema->getConfig()->descriptor) {
1027+
Warning::warnOnce(
10291028
"GraphQL Interface Type `{$returnType->name}` returned `null` from it`s `resolveType` function ".
10301029
'for value: ' . Utils::printSafe($result) . '. Switching to slow resolution method using `isTypeOf` ' .
10311030
'of all possible implementations. It degrades query performance significantly. '.
10321031
' Make sure your `resolveType` always returns valid implementation or throws.',
1033-
E_USER_WARNING
1032+
Warning::RESOLVE_TYPE_WARNING
10341033
);
10351034
}
10361035
$runtimeType = self::defaultTypeResolver($result, $exeContext->contextValue, $info, $returnType);

tests/Executor/AbstractPromiseTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
namespace GraphQL\Tests\Executor;
33

44
use GraphQL\Deferred;
5+
use GraphQL\Error\Warning;
56
use GraphQL\GraphQL;
67
use GraphQL\Schema;
78
use GraphQL\Type\Definition\InterfaceType;
@@ -85,7 +86,9 @@ public function testIsTypeOfUsedToResolveRuntimeTypeForInterface()
8586
}
8687
}';
8788

89+
Warning::suppress(Warning::RESOLVE_TYPE_WARNING);
8890
$result = GraphQL::execute($schema, $query);
91+
Warning::enable(Warning::RESOLVE_TYPE_WARNING);
8992

9093
$expected = [
9194
'data' => [
@@ -170,7 +173,9 @@ public function testIsTypeOfCanBeRejected()
170173
}
171174
}';
172175

176+
Warning::suppress(Warning::RESOLVE_TYPE_WARNING);
173177
$result = GraphQL::execute($schema, $query);
178+
Warning::enable(Warning::RESOLVE_TYPE_WARNING);
174179

175180
$expected = [
176181
'data' => [

tests/Executor/AbstractTest.php

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
require_once __DIR__ . '/TestClasses.php';
55

6+
use GraphQL\Error\Warning;
67
use GraphQL\Executor\ExecutionResult;
78
use GraphQL\Executor\Executor;
89
use GraphQL\Error\FormattedError;
@@ -89,29 +90,21 @@ public function testIsTypeOfUsedToResolveRuntimeTypeForInterface()
8990
]
9091
]);
9192

92-
GraphQL::setIgnoreError(GraphQL::WARNING_ON_IMPLEMENTATION_RESOLUTION);
93+
Warning::suppress(Warning::RESOLVE_TYPE_WARNING);
9394
$result = Executor::execute($schema, Parser::parse($query));
9495
$this->assertEquals($expected, $result);
9596

96-
GraphQL::setIgnoreError(GraphQL::WARNING_ON_IMPLEMENTATION_RESOLUTION, false);
97+
Warning::enable(Warning::RESOLVE_TYPE_WARNING);
9798
$result = Executor::execute($schema, Parser::parse($query));
98-
$this->assertEquals(2, count($result->errors));
99+
$this->assertEquals(1, count($result->errors));
99100
$this->assertInstanceOf('PHPUnit_Framework_Error_Warning', $result->errors[0]->getPrevious());
100-
$this->assertInstanceOf('PHPUnit_Framework_Error_Warning', $result->errors[1]->getPrevious());
101101

102102
$this->assertEquals(
103103
'GraphQL Interface Type `Pet` returned `null` from it`s `resolveType` function for value: '.
104104
'instance of GraphQL\Tests\Executor\Dog. Switching to slow resolution method using `isTypeOf` of '.
105105
'all possible implementations. It degrades query performance significantly. '.
106106
'Make sure your `resolveType` always returns valid implementation or throws.',
107107
$result->errors[0]->getMessage());
108-
109-
$this->assertEquals(
110-
'GraphQL Interface Type `Pet` returned `null` from it`s `resolveType` function for value: '.
111-
'instance of GraphQL\Tests\Executor\Cat. Switching to slow resolution method using `isTypeOf` of '.
112-
'all possible implementations. It degrades query performance significantly. '.
113-
'Make sure your `resolveType` always returns valid implementation or throws.',
114-
$result->errors[1]->getMessage());
115108
}
116109

117110
/**

0 commit comments

Comments
 (0)