From 6845b28a355732e101e164d1150cf3c4130b47b1 Mon Sep 17 00:00:00 2001 From: Vladimir Razuvaev Date: Mon, 14 Aug 2017 00:08:13 +0700 Subject: [PATCH] Deprecated `GraphQL\Type\Definition\Config` (#148) --- examples/01-blog/graphql.php | 7 +------ src/Error/Warning.php | 11 ++++++----- src/Type/Definition/Config.php | 7 +++++++ src/Type/Definition/FieldDefinition.php | 4 ++-- tests/Executor/ExecutorSchemaTest.php | 3 +-- tests/Executor/UnionInterfaceTest.php | 3 +-- tests/Type/ConfigTest.php | 5 +++++ tests/Type/DefinitionTest.php | 11 ++++------- tests/Type/ResolutionTest.php | 3 --- tests/Type/ValidationTest.php | 3 --- tests/Utils/ExtractTypesTest.php | 3 --- 11 files changed, 27 insertions(+), 33 deletions(-) diff --git a/examples/01-blog/graphql.php b/examples/01-blog/graphql.php index f5b5772..a471dde 100644 --- a/examples/01-blog/graphql.php +++ b/examples/01-blog/graphql.php @@ -6,19 +6,14 @@ require_once __DIR__ . '/../../vendor/autoload.php'; use \GraphQL\Examples\Blog\Types; use \GraphQL\Examples\Blog\AppContext; use \GraphQL\Examples\Blog\Data\DataSource; -use \GraphQL\Schema; +use \GraphQL\Type\Schema; use \GraphQL\GraphQL; -use \GraphQL\Type\Definition\Config; use \GraphQL\Error\FormattedError; // Disable default PHP error reporting - we have better one for debug mode (see bellow) ini_set('display_errors', 0); 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) $phpErrors = []; set_error_handler(function($severity, $message, $file, $line) use (&$phpErrors) { diff --git a/src/Error/Warning.php b/src/Error/Warning.php index 3cd54b4..2ec7631 100644 --- a/src/Error/Warning.php +++ b/src/Error/Warning.php @@ -7,8 +7,9 @@ final class Warning const ASSIGN_WARNING = 2; const CONFIG_WARNING = 4; const RESOLVE_TYPE_WARNING = 8; + const CONFIG_DEPRECATION_WARNING = 16; - const ALL = 7; + const ALL = 23; 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) { $fn = self::$warningHandler; $fn($errorMessage, $warningId); } else if ((self::$enableWarnings & $warningId) > 0 && !isset(self::$warned[$warningId])) { 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) { $fn = self::$warningHandler; $fn($errorMessage, $warningId); } else if ((self::$enableWarnings & $warningId) > 0) { - trigger_error($errorMessage, E_USER_WARNING); + trigger_error($errorMessage, $messageLevel ?: E_USER_WARNING); } } } diff --git a/src/Type/Definition/Config.php b/src/Type/Definition/Config.php index a67c546..04e9983 100644 --- a/src/Type/Definition/Config.php +++ b/src/Type/Definition/Config.php @@ -56,6 +56,13 @@ class Config */ 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::$allowCustomOptions = $allowCustomOptions; } diff --git a/src/Type/Definition/FieldDefinition.php b/src/Type/Definition/FieldDefinition.php index 04a0498..d3721b9 100644 --- a/src/Type/Definition/FieldDefinition.php +++ b/src/Type/Definition/FieldDefinition.php @@ -128,9 +128,9 @@ class FieldDefinition } /** - * @param array|Config $fields + * @param array $fields * @param string $parentTypeName - * @deprecated + * @deprecated use defineFieldMap instead * @return array */ public static function createMap(array $fields, $parentTypeName = null) diff --git a/tests/Executor/ExecutorSchemaTest.php b/tests/Executor/ExecutorSchemaTest.php index 4da1af6..d79cc71 100644 --- a/tests/Executor/ExecutorSchemaTest.php +++ b/tests/Executor/ExecutorSchemaTest.php @@ -3,8 +3,7 @@ namespace GraphQL\Tests\Executor; use GraphQL\Executor\Executor; use GraphQL\Language\Parser; -use GraphQL\Schema; -use GraphQL\Type\Definition\Config; +use GraphQL\Type\Schema; use GraphQL\Type\Definition\ObjectType; use GraphQL\Type\Definition\Type; diff --git a/tests/Executor/UnionInterfaceTest.php b/tests/Executor/UnionInterfaceTest.php index 4a9f0f2..5c8894e 100644 --- a/tests/Executor/UnionInterfaceTest.php +++ b/tests/Executor/UnionInterfaceTest.php @@ -7,8 +7,7 @@ use GraphQL\Error\Warning; use GraphQL\Executor\Executor; use GraphQL\GraphQL; use GraphQL\Language\Parser; -use GraphQL\Schema; -use GraphQL\Type\Definition\Config; +use GraphQL\Type\Schema; use GraphQL\Type\Definition\InterfaceType; use GraphQL\Type\Definition\ObjectType; use GraphQL\Type\Definition\ResolveInfo; diff --git a/tests/Type/ConfigTest.php b/tests/Type/ConfigTest.php index 6746d2f..9911174 100644 --- a/tests/Type/ConfigTest.php +++ b/tests/Type/ConfigTest.php @@ -12,6 +12,11 @@ use GraphQL\Utils\Utils; class ConfigTest extends \PHPUnit_Framework_TestCase { + public function setUp() + { + \PHPUnit_Framework_Error_Deprecated::$enabled = false; + } + public static function tearDownAfterClass() { Config::disableValidation(); diff --git a/tests/Type/DefinitionTest.php b/tests/Type/DefinitionTest.php index b84308d..a363685 100644 --- a/tests/Type/DefinitionTest.php +++ b/tests/Type/DefinitionTest.php @@ -3,8 +3,7 @@ namespace GraphQL\Tests\Type; require_once __DIR__ . '/TestClasses.php'; -use GraphQL\Schema; -use GraphQL\Type\Definition\Config; +use GraphQL\Type\Schema; use GraphQL\Type\Definition\EnumType; use GraphQL\Type\Definition\InputObjectType; use GraphQL\Type\Definition\InterfaceType; @@ -496,20 +495,18 @@ class DefinitionTest extends \PHPUnit_Framework_TestCase $this->inputObjectType ]; - // TODO: extract config validation to separate test - Config::enableValidation(); foreach ($badUnionTypes as $type) { try { - new UnionType(['name' => 'BadUnion', 'types' => [$type]]); + $union = new UnionType(['name' => 'BadUnion', 'types' => [$type]]); + $union->assertValid(); $this->fail('Expected exception not thrown'); } catch (\Exception $e) { $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() ); } } - Config::disableValidation(); } /** diff --git a/tests/Type/ResolutionTest.php b/tests/Type/ResolutionTest.php index 06fb8f5..2a01ac6 100644 --- a/tests/Type/ResolutionTest.php +++ b/tests/Type/ResolutionTest.php @@ -2,7 +2,6 @@ namespace GraphQL\Tests\Type; use GraphQL\Error\InvariantViolation; -use GraphQL\Type\Definition\Config; use GraphQL\Type\Definition\InputObjectType; use GraphQL\Type\Definition\InterfaceType; use GraphQL\Type\Definition\ObjectType; @@ -83,8 +82,6 @@ class ResolutionTest extends \PHPUnit_Framework_TestCase public function setUp() { - Config::enableValidation(false); - $this->node = new InterfaceType([ 'name' => 'Node', 'fields' => [ diff --git a/tests/Type/ValidationTest.php b/tests/Type/ValidationTest.php index 97f8c94..90a308e 100644 --- a/tests/Type/ValidationTest.php +++ b/tests/Type/ValidationTest.php @@ -3,7 +3,6 @@ namespace GraphQL\Tests\Type; use GraphQL\Error\InvariantViolation; use GraphQL\Error\Warning; -use GraphQL\Type\Definition\Config; use GraphQL\Type\Schema; use GraphQL\Type\Definition\CustomScalarType; use GraphQL\Type\Definition\EnumType; @@ -42,8 +41,6 @@ class ValidationTest extends \PHPUnit_Framework_TestCase public function setUp() { - Config::disableValidation(); - $this->String = 'TestString'; $this->SomeScalarType = new CustomScalarType([ diff --git a/tests/Utils/ExtractTypesTest.php b/tests/Utils/ExtractTypesTest.php index 551d525..6801f2f 100644 --- a/tests/Utils/ExtractTypesTest.php +++ b/tests/Utils/ExtractTypesTest.php @@ -1,7 +1,6 @@ node = new InterfaceType([ 'name' => 'Node', 'fields' => [