Skip to content

Commit adde60a

Browse files
authored
Use AST::valueFromASTUntyped to simplify the Mixed scalar (#4)
1 parent c8a6fd8 commit adde60a

File tree

1 file changed

+4
-33
lines changed

1 file changed

+4
-33
lines changed

src/Mixed.php

Lines changed: 4 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,9 @@
44

55
namespace MLL\GraphQLScalars;
66

7-
use GraphQL\Language\AST\FloatValueNode;
8-
use GraphQL\Language\AST\IntValueNode;
9-
use GraphQL\Language\AST\ListValueNode;
10-
use GraphQL\Language\AST\ObjectFieldNode;
11-
use GraphQL\Language\AST\ObjectValueNode;
127
use GraphQL\Language\AST\ValueNode;
138
use GraphQL\Type\Definition\ScalarType;
9+
use GraphQL\Utils\AST;
1410

1511
class Mixed extends ScalarType
1612
{
@@ -55,37 +51,12 @@ public function parseValue($value)
5551
* @param ValueNode $valueNode
5652
* @param array|null $variables
5753
*
54+
* @throws \Exception
55+
*
5856
* @return \mixed
5957
*/
6058
public function parseLiteral($valueNode, array $variables = null)
6159
{
62-
if ($valueNode instanceof IntValueNode) {
63-
// This is a potentially lossy conversion as GraphQL Int literals
64-
// may be arbitrarily large, whereas PHP ints are limited in size
65-
$value = (int) $valueNode->value;
66-
}
67-
68-
if ($valueNode instanceof FloatValueNode) {
69-
// This is a potentially lossy conversion as GraphQL Float literals
70-
// may be arbitrarily large, whereas PHP floats are limited in size
71-
$value = (float) $valueNode->value;
72-
}
73-
74-
if ($valueNode instanceof ListValueNode) {
75-
$value = [];
76-
foreach ($valueNode->values as $singleValue) {
77-
$value[] = $this->parseLiteral($singleValue);
78-
}
79-
}
80-
81-
if ($valueNode instanceof ObjectValueNode) {
82-
$value = [];
83-
/** @var ObjectFieldNode $singleValue */
84-
foreach ($valueNode->fields as $singleValue) {
85-
$value[$singleValue->name->value] = $this->parseLiteral($singleValue->value);
86-
}
87-
}
88-
89-
return $value ?? $valueNode->value;
60+
return AST::valueFromASTUntyped($valueNode);
9061
}
9162
}

0 commit comments

Comments
 (0)