From 0969073b8abd391952eea3b14a4a8b8b7e24aba5 Mon Sep 17 00:00:00 2001 From: vladar Date: Fri, 25 Nov 2016 16:54:57 +0700 Subject: [PATCH] Reverted DefinitionContainer (YAGNI) --- docs/type-system/index.md | 27 +------------------ docs/type-system/scalar-types.md | 26 +----------------- examples/01-blog/Blog/Type/BaseType.php | 24 ----------------- examples/01-blog/Blog/Type/CommentType.php | 8 +++--- .../Blog/Type/Enum/ContentFormatEnum.php | 9 +++---- .../Blog/Type/Enum/ImageSizeEnumType.php | 1 - examples/01-blog/Blog/Type/ImageType.php | 1 - examples/01-blog/Blog/Type/NodeType.php | 12 ++++----- examples/01-blog/Blog/Type/QueryType.php | 8 +++--- .../01-blog/Blog/Type/Scalar/EmailType.php | 20 +++++++------- examples/01-blog/Blog/Type/Scalar/UrlType.php | 2 -- .../01-blog/Blog/Type/SearchResultType.php | 8 +++--- examples/01-blog/Blog/Type/StoryType.php | 8 +++--- examples/01-blog/Blog/Type/UserType.php | 8 +++--- examples/01-blog/Blog/Types.php | 7 +++-- src/Executor/Executor.php | 6 ----- src/Schema.php | 16 ----------- src/Type/Definition/Config.php | 9 ------- src/Type/Definition/ListOfType.php | 5 ++-- src/Type/Definition/NonNull.php | 6 ++--- src/Type/Definition/ObjectType.php | 3 +-- src/Type/Definition/Type.php | 4 --- src/Type/Definition/UnionType.php | 1 - src/Type/DefinitionContainer.php | 15 ----------- 24 files changed, 49 insertions(+), 185 deletions(-) delete mode 100644 examples/01-blog/Blog/Type/BaseType.php delete mode 100644 src/Type/DefinitionContainer.php diff --git a/docs/type-system/index.md b/docs/type-system/index.md index e4a41f1..dc0c2bd 100644 --- a/docs/type-system/index.md +++ b/docs/type-system/index.md @@ -26,7 +26,7 @@ $myType = new ObjectType([ ]); ``` -Class per type, using inheritance: +Class per type: ```php definition ?: ($this->definition = new ObjectType([ - 'name' => 'MyType', - 'fields' => [ - 'id' => Type::id() - ] - ])); - } -} -``` - You can also mix-and-match styles for convenience. For example: ```php definition ?: ($this->definition = new CustomScalarType([ - 'name' => 'Email', - 'serialize' => function($value) {/* See function body above */}, - 'parseValue' => function($value) {/* See function body above */}, - 'parseLiteral' => function($valueNode) {/* See function body above */}, - ])); - } -} -``` - Or with inline style: ```php diff --git a/examples/01-blog/Blog/Type/BaseType.php b/examples/01-blog/Blog/Type/BaseType.php deleted file mode 100644 index af14eea..0000000 --- a/examples/01-blog/Blog/Type/BaseType.php +++ /dev/null @@ -1,24 +0,0 @@ -definition; - } -} diff --git a/examples/01-blog/Blog/Type/CommentType.php b/examples/01-blog/Blog/Type/CommentType.php index b3cdeac..b89aa54 100644 --- a/examples/01-blog/Blog/Type/CommentType.php +++ b/examples/01-blog/Blog/Type/CommentType.php @@ -8,12 +8,11 @@ use GraphQL\Examples\Blog\Types; use GraphQL\Type\Definition\ObjectType; use GraphQL\Type\Definition\ResolveInfo; -class CommentType extends BaseType +class CommentType extends ObjectType { public function __construct() { - // Option #1: using composition over inheritance to define type, see ImageType for inheritance example - $this->definition = new ObjectType([ + $config = [ 'name' => 'Comment', 'fields' => function() { return [ @@ -43,7 +42,8 @@ class CommentType extends BaseType return $value->{$info->fieldName}; } } - ]); + ]; + parent::__construct($config); } public function author(Comment $comment) diff --git a/examples/01-blog/Blog/Type/Enum/ContentFormatEnum.php b/examples/01-blog/Blog/Type/Enum/ContentFormatEnum.php index 162d457..8825247 100644 --- a/examples/01-blog/Blog/Type/Enum/ContentFormatEnum.php +++ b/examples/01-blog/Blog/Type/Enum/ContentFormatEnum.php @@ -1,20 +1,19 @@ definition = new EnumType([ + $config = [ 'name' => 'ContentFormatEnum', 'values' => [self::FORMAT_TEXT, self::FORMAT_HTML] - ]); + ]; + parent::__construct($config); } } diff --git a/examples/01-blog/Blog/Type/Enum/ImageSizeEnumType.php b/examples/01-blog/Blog/Type/Enum/ImageSizeEnumType.php index 384fb5e..5e744a6 100644 --- a/examples/01-blog/Blog/Type/Enum/ImageSizeEnumType.php +++ b/examples/01-blog/Blog/Type/Enum/ImageSizeEnumType.php @@ -8,7 +8,6 @@ class ImageSizeEnumType extends EnumType { public function __construct() { - // Option #2: Define enum type using inheritance $config = [ // Note: 'name' option is not needed in this form - it will be inferred from className 'values' => [ diff --git a/examples/01-blog/Blog/Type/ImageType.php b/examples/01-blog/Blog/Type/ImageType.php index 90b8555..aef9bff 100644 --- a/examples/01-blog/Blog/Type/ImageType.php +++ b/examples/01-blog/Blog/Type/ImageType.php @@ -11,7 +11,6 @@ class ImageType extends ObjectType { public function __construct() { - // Option #2: define type using inheritance, see any other object type for compositional example $config = [ 'name' => 'ImageType', 'fields' => [ diff --git a/examples/01-blog/Blog/Type/NodeType.php b/examples/01-blog/Blog/Type/NodeType.php index 7a60288..d3be9ed 100644 --- a/examples/01-blog/Blog/Type/NodeType.php +++ b/examples/01-blog/Blog/Type/NodeType.php @@ -7,21 +7,21 @@ use GraphQL\Examples\Blog\Data\Image; use GraphQL\Examples\Blog\Types; use GraphQL\Type\Definition\InterfaceType; -class NodeType extends BaseType +class NodeType extends InterfaceType { public function __construct() { - // Option #1: using composition over inheritance to define type, see ImageType for inheritance example - $this->definition = new InterfaceType([ + $config = [ 'name' => 'Node', 'fields' => [ 'id' => Types::id() ], - 'resolveType' => [$this, 'resolveType'] - ]); + 'resolveType' => [$this, 'resolveNodeType'] + ]; + parent::__construct($config); } - public function resolveType($object, Types $types) + public function resolveNodeType($object) { if ($object instanceof User) { return Types::user(); diff --git a/examples/01-blog/Blog/Type/QueryType.php b/examples/01-blog/Blog/Type/QueryType.php index 0e7268e..ff85577 100644 --- a/examples/01-blog/Blog/Type/QueryType.php +++ b/examples/01-blog/Blog/Type/QueryType.php @@ -8,12 +8,11 @@ use GraphQL\Type\Definition\ObjectType; use GraphQL\Type\Definition\ResolveInfo; use GraphQL\Type\Definition\Type; -class QueryType extends BaseType +class QueryType extends ObjectType { public function __construct() { - // Option #1: using composition over inheritance to define type, see ImageType for inheritance example - $this->definition = new ObjectType([ + $config = [ 'name' => 'Query', 'fields' => [ 'user' => [ @@ -61,7 +60,8 @@ class QueryType extends BaseType 'resolveField' => function($val, $args, $context, ResolveInfo $info) { return $this->{$info->fieldName}($val, $args, $context, $info); } - ]); + ]; + parent::__construct($config); } public function user($rootValue, $args) diff --git a/examples/01-blog/Blog/Type/Scalar/EmailType.php b/examples/01-blog/Blog/Type/Scalar/EmailType.php index 0eea0dd..9c1f15e 100644 --- a/examples/01-blog/Blog/Type/Scalar/EmailType.php +++ b/examples/01-blog/Blog/Type/Scalar/EmailType.php @@ -2,21 +2,19 @@ namespace GraphQL\Examples\Blog\Type\Scalar; use GraphQL\Error\Error; -use GraphQL\Examples\Blog\Type\BaseType; use GraphQL\Language\AST\StringValueNode; use GraphQL\Type\Definition\CustomScalarType; use GraphQL\Utils; -class EmailType extends BaseType +class EmailType { - public function __construct() + public static function create() { - // Option #1: define scalar types using composition (see UrlType fo option #2 using inheritance) - $this->definition = new CustomScalarType([ + return new CustomScalarType([ 'name' => 'Email', - 'serialize' => [$this, 'serialize'], - 'parseValue' => [$this, 'parseValue'], - 'parseLiteral' => [$this, 'parseLiteral'], + 'serialize' => [__CLASS__, 'serialize'], + 'parseValue' => [__CLASS__, 'parseValue'], + 'parseLiteral' => [__CLASS__, 'parseLiteral'], ]); } @@ -26,7 +24,7 @@ class EmailType extends BaseType * @param string $value * @return string */ - public function serialize($value) + public static function serialize($value) { // Assuming internal representation of email is always correct: return $value; @@ -42,7 +40,7 @@ class EmailType extends BaseType * @param mixed $value * @return mixed */ - public function parseValue($value) + public static function parseValue($value) { if (!filter_var($value, FILTER_VALIDATE_EMAIL)) { throw new \UnexpectedValueException("Cannot represent value as email: " . Utils::printSafe($value)); @@ -57,7 +55,7 @@ class EmailType extends BaseType * @return string * @throws Error */ - public function parseLiteral($valueNode) + public static function parseLiteral($valueNode) { // Note: throwing GraphQL\Error\Error vs \UnexpectedValueException to benefit from GraphQL // error location in query: diff --git a/examples/01-blog/Blog/Type/Scalar/UrlType.php b/examples/01-blog/Blog/Type/Scalar/UrlType.php index f9169e2..f565096 100644 --- a/examples/01-blog/Blog/Type/Scalar/UrlType.php +++ b/examples/01-blog/Blog/Type/Scalar/UrlType.php @@ -9,8 +9,6 @@ use GraphQL\Utils; class UrlType extends ScalarType { - // Option #2: Displays scalar type defined using inheritance. See EmailType for definition via composition - /** * Serializes an internal value to include in a response. * diff --git a/examples/01-blog/Blog/Type/SearchResultType.php b/examples/01-blog/Blog/Type/SearchResultType.php index b1df78a..d832a26 100644 --- a/examples/01-blog/Blog/Type/SearchResultType.php +++ b/examples/01-blog/Blog/Type/SearchResultType.php @@ -6,12 +6,11 @@ use GraphQL\Examples\Blog\Data\User; use GraphQL\Examples\Blog\Types; use GraphQL\Type\Definition\UnionType; -class SearchResultType extends BaseType +class SearchResultType extends UnionType { public function __construct() { - // Option #1: using composition over inheritance to define type, see ImageType for inheritance example - $this->definition = new UnionType([ + $config = [ 'name' => 'SearchResultType', 'types' => function() { return [ @@ -26,6 +25,7 @@ class SearchResultType extends BaseType return Types::user(); } } - ]); + ]; + parent::__construct($config); } } diff --git a/examples/01-blog/Blog/Type/StoryType.php b/examples/01-blog/Blog/Type/StoryType.php index d4ee8e2..38cf73f 100644 --- a/examples/01-blog/Blog/Type/StoryType.php +++ b/examples/01-blog/Blog/Type/StoryType.php @@ -13,7 +13,7 @@ use GraphQL\Type\Definition\ResolveInfo; * Class StoryType * @package GraphQL\Examples\Social\Type */ -class StoryType extends BaseType +class StoryType extends ObjectType { const EDIT = 'EDIT'; const DELETE = 'DELETE'; @@ -23,8 +23,7 @@ class StoryType extends BaseType public function __construct() { - // Option #1: using composition over inheritance to define type, see ImageType for inheritance example - $this->definition = new ObjectType([ + $config = [ 'name' => 'Story', 'fields' => function() { return [ @@ -83,7 +82,8 @@ class StoryType extends BaseType return $value->{$info->fieldName}; } } - ]); + ]; + parent::__construct($config); } public function author(Story $story) diff --git a/examples/01-blog/Blog/Type/UserType.php b/examples/01-blog/Blog/Type/UserType.php index 0a7da72..347da46 100644 --- a/examples/01-blog/Blog/Type/UserType.php +++ b/examples/01-blog/Blog/Type/UserType.php @@ -8,12 +8,11 @@ use GraphQL\Examples\Blog\Types; use GraphQL\Type\Definition\ObjectType; use GraphQL\Type\Definition\ResolveInfo; -class UserType extends BaseType +class UserType extends ObjectType { public function __construct() { - // Option #1: using composition over inheritance to define type, see ImageType for inheritance example - $this->definition = new ObjectType([ + $config = [ 'name' => 'User', 'description' => 'Our blog authors', 'fields' => function() { @@ -52,7 +51,8 @@ class UserType extends BaseType return $value->{$info->fieldName}; } } - ]); + ]; + parent::__construct($config); } public function photo(User $user, $args) diff --git a/examples/01-blog/Blog/Types.php b/examples/01-blog/Blog/Types.php index da0d996..7ffc657 100644 --- a/examples/01-blog/Blog/Types.php +++ b/examples/01-blog/Blog/Types.php @@ -16,7 +16,6 @@ use GraphQL\Examples\Blog\Type\ImageType; use GraphQL\Type\Definition\ListOfType; use GraphQL\Type\Definition\NonNull; use GraphQL\Type\Definition\Type; -use GraphQL\Type\DefinitionContainer; /** * Class Types @@ -128,7 +127,7 @@ class Types public static function email() { - return self::$emailType ?: (self::$emailType = new EmailType()); + return self::$emailType ?: (self::$emailType = EmailType::create()); } /** @@ -191,7 +190,7 @@ class Types } /** - * @param Type|DefinitionContainer $type + * @param Type $type * @return ListOfType */ public static function listOf($type) @@ -200,7 +199,7 @@ class Types } /** - * @param Type|DefinitionContainer $type + * @param Type $type * @return NonNull */ public static function nonNull($type) diff --git a/src/Executor/Executor.php b/src/Executor/Executor.php index 0d87840..e0f9a2d 100644 --- a/src/Executor/Executor.php +++ b/src/Executor/Executor.php @@ -20,7 +20,6 @@ use GraphQL\Type\Definition\NonNull; use GraphQL\Type\Definition\ObjectType; use GraphQL\Type\Definition\ResolveInfo; use GraphQL\Type\Definition\Type; -use GraphQL\Type\DefinitionContainer; use GraphQL\Type\Introspection; use GraphQL\Utils; @@ -754,11 +753,6 @@ class Executor $runtimeType = $exeContext->schema->getType($runtimeType); } - // FIXME: Executor should never face with DefinitionContainer - it should be unwrapped in Schema - if ($runtimeType instanceof DefinitionContainer) { - $runtimeType = $runtimeType->getDefinition(); - } - if (!($runtimeType instanceof ObjectType)) { throw new Error( "Abstract type {$returnType} must resolve to an Object type at runtime " . diff --git a/src/Schema.php b/src/Schema.php index b8c8a62..a1c527d 100644 --- a/src/Schema.php +++ b/src/Schema.php @@ -9,7 +9,6 @@ use GraphQL\Type\Definition\ObjectType; use GraphQL\Type\Definition\Type; use GraphQL\Type\Definition\UnionType; use GraphQL\Type\Definition\WrappingType; -use GraphQL\Type\DefinitionContainer; use GraphQL\Type\Introspection; /** @@ -114,18 +113,6 @@ class Schema 'validate' => true ]; - if ($config['query'] instanceof DefinitionContainer) { - $config['query'] = $config['query']->getDefinition(); - } - - if ($config['mutation'] instanceof DefinitionContainer) { - $config['mutation'] = $config['mutation']->getDefinition(); - } - - if ($config['subscription'] instanceof DefinitionContainer) { - $config['subscription'] = $config['subscription']->getDefinition(); - } - Utils::invariant( $config['query'] instanceof ObjectType, "Schema query must be Object Type but got: " . Utils::getVariableType($config['query']) @@ -301,9 +288,6 @@ class Schema if (!$type) { return $this->typeMap; } - if ($type instanceof DefinitionContainer) { - $type = $type->getDefinition(); - } if ($type instanceof WrappingType) { return $this->extractTypes($type->getWrappedType(true)); diff --git a/src/Type/Definition/Config.php b/src/Type/Definition/Config.php index 0d1a171..bffef16 100644 --- a/src/Type/Definition/Config.php +++ b/src/Type/Definition/Config.php @@ -2,7 +2,6 @@ namespace GraphQL\Type\Definition; use GraphQL\Error\InvariantViolation; -use GraphQL\Type\DefinitionContainer; use GraphQL\Utils; /** @@ -190,10 +189,6 @@ class Config $err = 'Error in "'.$typeName.'" type definition: ' . "Each entry at '$pathStr' must be an array, but '%s' is '%s'"; foreach ($value as $arrKey => $arrValue) { - if ($arrValue instanceof DefinitionContainer) { - $arrValue = $arrValue->getDefinition(); - } - if (is_array($def->definition)) { if ($def->flags & self::MAYBE_TYPE && $arrValue instanceof Type) { $arrValue = ['type' => $arrValue]; @@ -226,10 +221,6 @@ class Config return ; // Allow nulls for non-required fields } - if ($value instanceof DefinitionContainer) { - $value = $value->getDefinition(); - } - switch (true) { case $def & self::ANY: break; diff --git a/src/Type/Definition/ListOfType.php b/src/Type/Definition/ListOfType.php index f4f892f..4784e81 100644 --- a/src/Type/Definition/ListOfType.php +++ b/src/Type/Definition/ListOfType.php @@ -1,7 +1,6 @@ getDefinition(); - } if (!$type instanceof Type) { throw new InvariantViolation(sprintf( diff --git a/src/Type/Definition/UnionType.php b/src/Type/Definition/UnionType.php index 7fce6bd..ec83914 100644 --- a/src/Type/Definition/UnionType.php +++ b/src/Type/Definition/UnionType.php @@ -1,7 +1,6 @@