Skip to content

Commit

Permalink
BC is mandatory for PHP < 7.2
Browse files Browse the repository at this point in the history
  • Loading branch information
alanpoulain committed Jul 23, 2020
1 parent bab2b54 commit 912d2ed
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 23 deletions.
56 changes: 41 additions & 15 deletions src/GraphQl/Type/Definition/IterableType.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,52 @@
use GraphQL\Language\AST\FloatValueNode;
use GraphQL\Language\AST\IntValueNode;
use GraphQL\Language\AST\ListValueNode;
use GraphQL\Language\AST\Node;
use GraphQL\Language\AST\NullValueNode;
use GraphQL\Language\AST\ObjectValueNode;
use GraphQL\Language\AST\StringValueNode;
use GraphQL\Language\AST\ValueNode;
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.
*
Expand All @@ -34,6 +73,8 @@
*/
final class IterableType extends ScalarType implements TypeInterface
{
use IterableTypeParseLiteralTrait;

public function __construct()
{
$this->name = 'Iterable';
Expand Down Expand Up @@ -71,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
*/
Expand Down
35 changes: 27 additions & 8 deletions src/GraphQl/Type/Definition/UploadType.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,44 @@
namespace ApiPlatform\Core\GraphQl\Type\Definition;

use GraphQL\Error\Error;
use GraphQL\Language\AST\Node;
use GraphQL\Type\Definition\ScalarType;
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.
*
* @author Mahmood Bazdar <mahmood@bazdar.me>
*/
final class UploadType extends ScalarType implements TypeInterface
{
use UploadTypeParseLiteralTrait;

public function __construct()
{
$this->name = 'Upload';
Expand Down Expand Up @@ -57,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);
}
}

0 comments on commit 912d2ed

Please sign in to comment.