for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
declare(strict_types=1);
namespace GraphQL\Type\Definition;
use Exception;
use GraphQL\Error\Error;
use GraphQL\Language\AST\Node;
use GraphQL\Language\AST\StringValueNode;
use GraphQL\Utils\Utils;
use function is_array;
use function is_object;
use function is_scalar;
use function is_string;
use function method_exists;
class StringType extends ScalarType
{
/** @var string */
public $name = Type::STRING;
public $description =
'The `String` scalar type represents textual data, represented as UTF-8
character sequences. The String type is most often used by GraphQL to
represent free-form human-readable text.';
/**
* @param mixed $value
*
* @return mixed|string
* @throws Error
*/
public function serialize($value)
$canCast = is_scalar($value)
|| (is_object($value) && method_exists($value, '__toString'))
|| $value === null;
if (! $canCast) {
throw new Error(
'String cannot represent value: ' . Utils::printSafe($value)
);
}
return (string) $value;
* @return string
public function parseValue($value)
if (! is_string($value)) {
'String cannot represent a non string value: ' . Utils::printSafe($value)
return $value;
* @param Node $valueNode
* @param mixed[]|null $variables
* @return string|null
* @throws Exception
public function parseLiteral($valueNode, ?array $variables = null)
if ($valueNode instanceof StringValueNode) {
return $valueNode->value;
// Intentionally without message, as all information already in wrapped Exception
throw new Exception();