Skip to content

Commit f1d696a

Browse files
committed
improve string value debuging
1 parent 5c9a005 commit f1d696a

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

src/Types/GraphQLString.php

+21-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class GraphQLString extends GraphQLScalarType
2020
*/
2121
public function serialize($outputValue): ?string
2222
{
23-
if (!is_string($outputValue) and $outputValue !== null) {
23+
if (!$this->isStringableValue($outputValue)) {
2424
throw new GraphQLError(
2525
"Value \"$outputValue\" is not of type \"{$this->getName()}\"."
2626
);
@@ -45,19 +45,38 @@ public function parseLiteral($valueNode, $variables)
4545
return $valueNode["value"];
4646
}
4747

48+
4849
/**
4950
* @param $value
5051
* @return string|null
5152
* @throws GraphQLError
5253
*/
5354
public function parseValue($value): ?string
5455
{
55-
if (!is_string($value) and $value !== null) {
56+
if (!$this->isStringableValue($value)) {
57+
// cast value to a printable version
58+
$value = is_array($value)
59+
? "[Array]"
60+
: (string)$value;
61+
5662
throw new GraphQLError(
5763
"Value \"$value\" is not of type \"{$this->getName()}\"."
5864
);
5965
}
6066
return $value;
6167
}
68+
69+
/**
70+
* Returns whether the object can be casted to a string or not.
71+
*
72+
* @param mixed $value
73+
* @return bool
74+
*/
75+
private function isStringableValue(mixed $value): bool
76+
{
77+
return $value === null
78+
|| is_string($value)
79+
|| is_object($value) && method_exists($value, '__toString');
80+
}
6281
}
6382

0 commit comments

Comments
 (0)