File tree Expand file tree Collapse file tree 1 file changed +21
-2
lines changed Expand file tree Collapse file tree 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
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
You can’t perform that action at this time.
0 commit comments