From 0a0c7c571b4045aad901ba74368f61836d989a00 Mon Sep 17 00:00:00 2001 From: Alan Poulain Date: Thu, 23 Jul 2020 15:09:13 +0200 Subject: [PATCH] [GraphQL] No BC for types (#3654) --- src/GraphQl/Type/Definition/IterableType.php | 55 ++++++++++++++------ src/GraphQl/Type/Definition/UploadType.php | 34 +++++++++--- 2 files changed, 66 insertions(+), 23 deletions(-) diff --git a/src/GraphQl/Type/Definition/IterableType.php b/src/GraphQl/Type/Definition/IterableType.php index bf488c58667..30f66d73f98 100644 --- a/src/GraphQl/Type/Definition/IterableType.php +++ b/src/GraphQl/Type/Definition/IterableType.php @@ -26,6 +26,44 @@ use GraphQL\Type\Definition\ScalarType; use GraphQL\Utils\Utils; +if (\PHP_VERSION_ID >= 70200) { + trait IterableTypeParseLiteralTrait + { + /** + * {@inheritdoc} + * + * @param ObjectValueNode|ListValueNode|IntValueNode|FloatValueNode|StringValueNode|BooleanValueNode|NullValueNode $valueNode + */ + public function parseLiteral(/*Node */$valueNode, ?array $variables = null) + { + if ($valueNode instanceof ObjectValueNode || $valueNode instanceof ListValueNode) { + return $this->parseIterableLiteral($valueNode); + } + + // Intentionally without message, as all information already in wrapped Exception + throw new \Exception(); + } + } +} else { + trait IterableTypeParseLiteralTrait + { + /** + * {@inheritdoc} + * + * @param ObjectValueNode|ListValueNode|IntValueNode|FloatValueNode|StringValueNode|BooleanValueNode|NullValueNode $valueNode + */ + public function parseLiteral(Node $valueNode, ?array $variables = null) + { + if ($valueNode instanceof ObjectValueNode || $valueNode instanceof ListValueNode) { + return $this->parseIterableLiteral($valueNode); + } + + // Intentionally without message, as all information already in wrapped Exception + throw new \Exception(); + } + } +} + /** * Represents an iterable type. * @@ -35,6 +73,8 @@ */ final class IterableType extends ScalarType implements TypeInterface { + use IterableTypeParseLiteralTrait; + public function __construct() { $this->name = 'Iterable'; @@ -72,21 +112,6 @@ public function parseValue($value) return $value; } - /** - * {@inheritdoc} - * - * @param ObjectValueNode|ListValueNode|IntValueNode|FloatValueNode|StringValueNode|BooleanValueNode|NullValueNode $valueNode - */ - public function parseLiteral(Node $valueNode, ?array $variables = null) - { - if ($valueNode instanceof ObjectValueNode || $valueNode instanceof ListValueNode) { - return $this->parseIterableLiteral($valueNode); - } - - // Intentionally without message, as all information already in wrapped Exception - throw new \Exception(); - } - /** * @param StringValueNode|BooleanValueNode|IntValueNode|FloatValueNode|ObjectValueNode|ListValueNode|ValueNode $valueNode */ diff --git a/src/GraphQl/Type/Definition/UploadType.php b/src/GraphQl/Type/Definition/UploadType.php index 36f3b715653..af804510aea 100644 --- a/src/GraphQl/Type/Definition/UploadType.php +++ b/src/GraphQl/Type/Definition/UploadType.php @@ -19,6 +19,30 @@ use GraphQL\Utils\Utils; use Symfony\Component\HttpFoundation\File\UploadedFile; +if (\PHP_VERSION_ID >= 70200) { + trait UploadTypeParseLiteralTrait + { + /** + * {@inheritdoc} + */ + public function parseLiteral(/*Node */$valueNode, array $variables = null) + { + throw new Error('`Upload` cannot be hardcoded in query, be sure to conform to GraphQL multipart request specification.', $valueNode); + } + } +} else { + trait UploadTypeParseLiteralTrait + { + /** + * {@inheritdoc} + */ + public function parseLiteral(Node $valueNode, array $variables = null) + { + throw new Error('`Upload` cannot be hardcoded in query, be sure to conform to GraphQL multipart request specification.', $valueNode); + } + } +} + /** * Represents an upload type. * @@ -26,6 +50,8 @@ */ final class UploadType extends ScalarType implements TypeInterface { + use UploadTypeParseLiteralTrait; + public function __construct() { $this->name = 'Upload'; @@ -58,12 +84,4 @@ public function parseValue($value): UploadedFile return $value; } - - /** - * {@inheritdoc} - */ - public function parseLiteral(Node $valueNode, array $variables = null) - { - throw new Error('`Upload` cannot be hardcoded in query, be sure to conform to GraphQL multipart request specification.', $valueNode); - } }