Skip to content

Commit

Permalink
[GraphQL] No BC for types (#3654)
Browse files Browse the repository at this point in the history
  • Loading branch information
alanpoulain committed Jul 23, 2020
1 parent 97f92a7 commit 0a0c7c5
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 23 deletions.
55 changes: 40 additions & 15 deletions src/GraphQl/Type/Definition/IterableType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -35,6 +73,8 @@
*/
final class IterableType extends ScalarType implements TypeInterface
{
use IterableTypeParseLiteralTrait;

public function __construct()
{
$this->name = 'Iterable';
Expand Down Expand Up @@ -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
*/
Expand Down
34 changes: 26 additions & 8 deletions src/GraphQl/Type/Definition/UploadType.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,39 @@
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 @@ -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);
}
}

0 comments on commit 0a0c7c5

Please sign in to comment.