Deprecated GraphQL\Type\Definition\Config (#148)

This commit is contained in:
Vladimir Razuvaev 2017-08-14 00:08:13 +07:00
parent ed3591c1a9
commit 6845b28a35
11 changed files with 27 additions and 33 deletions

View File

@ -6,19 +6,14 @@ require_once __DIR__ . '/../../vendor/autoload.php';
use \GraphQL\Examples\Blog\Types; use \GraphQL\Examples\Blog\Types;
use \GraphQL\Examples\Blog\AppContext; use \GraphQL\Examples\Blog\AppContext;
use \GraphQL\Examples\Blog\Data\DataSource; use \GraphQL\Examples\Blog\Data\DataSource;
use \GraphQL\Schema; use \GraphQL\Type\Schema;
use \GraphQL\GraphQL; use \GraphQL\GraphQL;
use \GraphQL\Type\Definition\Config;
use \GraphQL\Error\FormattedError; use \GraphQL\Error\FormattedError;
// Disable default PHP error reporting - we have better one for debug mode (see bellow) // Disable default PHP error reporting - we have better one for debug mode (see bellow)
ini_set('display_errors', 0); ini_set('display_errors', 0);
if (!empty($_GET['debug'])) { if (!empty($_GET['debug'])) {
// Enable additional validation of type configs
// (disabled by default because it is costly)
Config::enableValidation();
// Catch custom errors (to report them in query results if debugging is enabled) // Catch custom errors (to report them in query results if debugging is enabled)
$phpErrors = []; $phpErrors = [];
set_error_handler(function($severity, $message, $file, $line) use (&$phpErrors) { set_error_handler(function($severity, $message, $file, $line) use (&$phpErrors) {

View File

@ -7,8 +7,9 @@ final class Warning
const ASSIGN_WARNING = 2; const ASSIGN_WARNING = 2;
const CONFIG_WARNING = 4; const CONFIG_WARNING = 4;
const RESOLVE_TYPE_WARNING = 8; const RESOLVE_TYPE_WARNING = 8;
const CONFIG_DEPRECATION_WARNING = 16;
const ALL = 7; const ALL = 23;
static $enableWarnings = self::ALL; static $enableWarnings = self::ALL;
@ -45,24 +46,24 @@ final class Warning
} }
} }
static function warnOnce($errorMessage, $warningId) static function warnOnce($errorMessage, $warningId, $messageLevel = null)
{ {
if (self::$warningHandler) { if (self::$warningHandler) {
$fn = self::$warningHandler; $fn = self::$warningHandler;
$fn($errorMessage, $warningId); $fn($errorMessage, $warningId);
} else if ((self::$enableWarnings & $warningId) > 0 && !isset(self::$warned[$warningId])) { } else if ((self::$enableWarnings & $warningId) > 0 && !isset(self::$warned[$warningId])) {
self::$warned[$warningId] = true; self::$warned[$warningId] = true;
trigger_error($errorMessage, E_USER_WARNING); trigger_error($errorMessage, $messageLevel ?: E_USER_WARNING);
} }
} }
static function warn($errorMessage, $warningId) static function warn($errorMessage, $warningId, $messageLevel = null)
{ {
if (self::$warningHandler) { if (self::$warningHandler) {
$fn = self::$warningHandler; $fn = self::$warningHandler;
$fn($errorMessage, $warningId); $fn($errorMessage, $warningId);
} else if ((self::$enableWarnings & $warningId) > 0) { } else if ((self::$enableWarnings & $warningId) > 0) {
trigger_error($errorMessage, E_USER_WARNING); trigger_error($errorMessage, $messageLevel ?: E_USER_WARNING);
} }
} }
} }

View File

@ -56,6 +56,13 @@ class Config
*/ */
public static function enableValidation($allowCustomOptions = true) public static function enableValidation($allowCustomOptions = true)
{ {
Warning::warnOnce(
'GraphQL\Type\Defintion\Config is deprecated and will be removed in the next version. ' .
'See https://github.com/webonyx/graphql-php/issues/148 for alternatives',
Warning::CONFIG_DEPRECATION_WARNING,
E_USER_DEPRECATED
);
self::$enableValidation = true; self::$enableValidation = true;
self::$allowCustomOptions = $allowCustomOptions; self::$allowCustomOptions = $allowCustomOptions;
} }

View File

@ -128,9 +128,9 @@ class FieldDefinition
} }
/** /**
* @param array|Config $fields * @param array $fields
* @param string $parentTypeName * @param string $parentTypeName
* @deprecated * @deprecated use defineFieldMap instead
* @return array * @return array
*/ */
public static function createMap(array $fields, $parentTypeName = null) public static function createMap(array $fields, $parentTypeName = null)

View File

@ -3,8 +3,7 @@ namespace GraphQL\Tests\Executor;
use GraphQL\Executor\Executor; use GraphQL\Executor\Executor;
use GraphQL\Language\Parser; use GraphQL\Language\Parser;
use GraphQL\Schema; use GraphQL\Type\Schema;
use GraphQL\Type\Definition\Config;
use GraphQL\Type\Definition\ObjectType; use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\Type; use GraphQL\Type\Definition\Type;

View File

@ -7,8 +7,7 @@ use GraphQL\Error\Warning;
use GraphQL\Executor\Executor; use GraphQL\Executor\Executor;
use GraphQL\GraphQL; use GraphQL\GraphQL;
use GraphQL\Language\Parser; use GraphQL\Language\Parser;
use GraphQL\Schema; use GraphQL\Type\Schema;
use GraphQL\Type\Definition\Config;
use GraphQL\Type\Definition\InterfaceType; use GraphQL\Type\Definition\InterfaceType;
use GraphQL\Type\Definition\ObjectType; use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\ResolveInfo; use GraphQL\Type\Definition\ResolveInfo;

View File

@ -12,6 +12,11 @@ use GraphQL\Utils\Utils;
class ConfigTest extends \PHPUnit_Framework_TestCase class ConfigTest extends \PHPUnit_Framework_TestCase
{ {
public function setUp()
{
\PHPUnit_Framework_Error_Deprecated::$enabled = false;
}
public static function tearDownAfterClass() public static function tearDownAfterClass()
{ {
Config::disableValidation(); Config::disableValidation();

View File

@ -3,8 +3,7 @@ namespace GraphQL\Tests\Type;
require_once __DIR__ . '/TestClasses.php'; require_once __DIR__ . '/TestClasses.php';
use GraphQL\Schema; use GraphQL\Type\Schema;
use GraphQL\Type\Definition\Config;
use GraphQL\Type\Definition\EnumType; use GraphQL\Type\Definition\EnumType;
use GraphQL\Type\Definition\InputObjectType; use GraphQL\Type\Definition\InputObjectType;
use GraphQL\Type\Definition\InterfaceType; use GraphQL\Type\Definition\InterfaceType;
@ -496,20 +495,18 @@ class DefinitionTest extends \PHPUnit_Framework_TestCase
$this->inputObjectType $this->inputObjectType
]; ];
// TODO: extract config validation to separate test
Config::enableValidation();
foreach ($badUnionTypes as $type) { foreach ($badUnionTypes as $type) {
try { try {
new UnionType(['name' => 'BadUnion', 'types' => [$type]]); $union = new UnionType(['name' => 'BadUnion', 'types' => [$type]]);
$union->assertValid();
$this->fail('Expected exception not thrown'); $this->fail('Expected exception not thrown');
} catch (\Exception $e) { } catch (\Exception $e) {
$this->assertSame( $this->assertSame(
'Error in "BadUnion" type definition: expecting "ObjectType definition" at "types:0", but got "' . Utils::getVariableType($type) . '"', 'BadUnion may only contain Object types, it cannot contain: ' . Utils::printSafe($type) . '.',
$e->getMessage() $e->getMessage()
); );
} }
} }
Config::disableValidation();
} }
/** /**

View File

@ -2,7 +2,6 @@
namespace GraphQL\Tests\Type; namespace GraphQL\Tests\Type;
use GraphQL\Error\InvariantViolation; use GraphQL\Error\InvariantViolation;
use GraphQL\Type\Definition\Config;
use GraphQL\Type\Definition\InputObjectType; use GraphQL\Type\Definition\InputObjectType;
use GraphQL\Type\Definition\InterfaceType; use GraphQL\Type\Definition\InterfaceType;
use GraphQL\Type\Definition\ObjectType; use GraphQL\Type\Definition\ObjectType;
@ -83,8 +82,6 @@ class ResolutionTest extends \PHPUnit_Framework_TestCase
public function setUp() public function setUp()
{ {
Config::enableValidation(false);
$this->node = new InterfaceType([ $this->node = new InterfaceType([
'name' => 'Node', 'name' => 'Node',
'fields' => [ 'fields' => [

View File

@ -3,7 +3,6 @@ namespace GraphQL\Tests\Type;
use GraphQL\Error\InvariantViolation; use GraphQL\Error\InvariantViolation;
use GraphQL\Error\Warning; use GraphQL\Error\Warning;
use GraphQL\Type\Definition\Config;
use GraphQL\Type\Schema; use GraphQL\Type\Schema;
use GraphQL\Type\Definition\CustomScalarType; use GraphQL\Type\Definition\CustomScalarType;
use GraphQL\Type\Definition\EnumType; use GraphQL\Type\Definition\EnumType;
@ -42,8 +41,6 @@ class ValidationTest extends \PHPUnit_Framework_TestCase
public function setUp() public function setUp()
{ {
Config::disableValidation();
$this->String = 'TestString'; $this->String = 'TestString';
$this->SomeScalarType = new CustomScalarType([ $this->SomeScalarType = new CustomScalarType([

View File

@ -1,7 +1,6 @@
<?php <?php
namespace Utils; namespace Utils;
use GraphQL\Type\Definition\Config;
use GraphQL\Type\Definition\InputObjectType; use GraphQL\Type\Definition\InputObjectType;
use GraphQL\Type\Definition\InterfaceType; use GraphQL\Type\Definition\InterfaceType;
use GraphQL\Type\Definition\ObjectType; use GraphQL\Type\Definition\ObjectType;
@ -81,8 +80,6 @@ class ExtractTypesTest extends \PHPUnit_Framework_TestCase
public function setUp() public function setUp()
{ {
Config::enableValidation(false);
$this->node = new InterfaceType([ $this->node = new InterfaceType([
'name' => 'Node', 'name' => 'Node',
'fields' => [ 'fields' => [