File tree 1 file changed +21
-2
lines changed
1 file changed +21
-2
lines changed Original file line number Diff line number Diff line change @@ -20,7 +20,7 @@ class GraphQLString extends GraphQLScalarType
20
20
*/
21
21
public function serialize ($ outputValue ): ?string
22
22
{
23
- if (!is_string ($ outputValue ) and $ outputValue !== null ) {
23
+ if (!$ this -> isStringableValue ($ outputValue )) {
24
24
throw new GraphQLError (
25
25
"Value \"$ outputValue \" is not of type \"{$ this ->getName ()}\". "
26
26
);
@@ -45,19 +45,38 @@ public function parseLiteral($valueNode, $variables)
45
45
return $ valueNode ["value " ];
46
46
}
47
47
48
+
48
49
/**
49
50
* @param $value
50
51
* @return string|null
51
52
* @throws GraphQLError
52
53
*/
53
54
public function parseValue ($ value ): ?string
54
55
{
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
+
56
62
throw new GraphQLError (
57
63
"Value \"$ value \" is not of type \"{$ this ->getName ()}\". "
58
64
);
59
65
}
60
66
return $ value ;
61
67
}
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
+ }
62
81
}
63
82
You can’t perform that action at this time.
0 commit comments