mirror of
https://github.com/retailcrm/graphql-php.git
synced 2025-02-11 10:09:24 +03:00
Convert error to warning for non-compliant usage of __ in names
This commit is contained in:
parent
34bd378c7e
commit
c5484ae6f9
24
src/Error/Warning.php
Normal file
24
src/Error/Warning.php
Normal file
@ -0,0 +1,24 @@
|
||||
<?php
|
||||
namespace GraphQL\Error;
|
||||
|
||||
final class Warning
|
||||
{
|
||||
static $supressWarnings = false;
|
||||
|
||||
static $warned = [];
|
||||
|
||||
static function supress($set = true)
|
||||
{
|
||||
self::$supressWarnings = $set;
|
||||
}
|
||||
|
||||
static function warnOnce($errorMessage, $errorId = null)
|
||||
{
|
||||
$errorId = $errorId ?: $errorMessage;
|
||||
|
||||
if (!self::$supressWarnings && !isset(self::$warned[$errorId])) {
|
||||
self::$warned[$errorId] = true;
|
||||
trigger_error($errorMessage, E_USER_WARNING);
|
||||
}
|
||||
}
|
||||
}
|
@ -2,6 +2,7 @@
|
||||
namespace GraphQL;
|
||||
|
||||
use GraphQL\Error\InvariantViolation;
|
||||
use GraphQL\Error\Warning;
|
||||
use GraphQL\Type\Definition\Type;
|
||||
use GraphQL\Type\Definition\WrappingType;
|
||||
use GraphQL\Utils\SchemaUtils;
|
||||
@ -351,9 +352,10 @@ class Utils
|
||||
}
|
||||
|
||||
if (!$isIntrospection && isset($name[1]) && $name[0] === '_' && $name[1] === '_') {
|
||||
throw new InvariantViolation(
|
||||
Warning::warnOnce(
|
||||
'Name "'.$name.'" must not begin with "__", which is reserved by ' .
|
||||
'GraphQL introspection.'
|
||||
'GraphQL introspection.',
|
||||
'warnAboutDunder'
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -34,7 +34,7 @@ class ValidationTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
public function testRejectsAnObjectTypeWithReservedName()
|
||||
{
|
||||
$this->assertEachCallableThrows([
|
||||
$this->assertWarnsOnce([
|
||||
function() {
|
||||
return new ObjectType([
|
||||
'name' => '__ReservedName',
|
||||
@ -53,6 +53,7 @@ class ValidationTest extends \PHPUnit_Framework_TestCase
|
||||
function() {
|
||||
return new UnionType([
|
||||
'name' => '__ReservedName',
|
||||
'types' => [new ObjectType(['name' => 'Test'])]
|
||||
]);
|
||||
},
|
||||
function() {
|
||||
@ -268,6 +269,26 @@ class ValidationTest extends \PHPUnit_Framework_TestCase
|
||||
}
|
||||
}
|
||||
|
||||
private function assertWarnsOnce($closures, $expectedError)
|
||||
{
|
||||
$warned = false;
|
||||
|
||||
foreach ($closures as $index => $factory) {
|
||||
if (!$warned) {
|
||||
try {
|
||||
$factory();
|
||||
$this->fail('Expected exception not thrown for entry ' . $index);
|
||||
} catch (\PHPUnit_Framework_Error_Warning $e) {
|
||||
$warned = true;
|
||||
$this->assertEquals($expectedError, $e->getMessage(), 'Error in callable #' . $index);
|
||||
}
|
||||
} else {
|
||||
// Should not throw
|
||||
$factory();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function schemaWithFieldType($type)
|
||||
{
|
||||
return new Schema([
|
||||
|
Loading…
x
Reference in New Issue
Block a user