for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
namespace TheCodingMachine\GraphQLite\Types\JSONScalar;
use Exception;
use GraphQL\Error\Error;
use GraphQL\Type\Definition\ScalarType;
use GraphQL\Utils\Utils as GraphQLUtils;
final class JSONScalarType extends ScalarType
{
public const NAME = 'JSON';
/**
* @var self
*/
private static $instance;
public function __construct()
parent::__construct([
'name' => 'JSON',
'description' => 'A GraphQL type that can contain json'
]);
}
public static function getInstance(): self
if (self::$instance === null) {
self::$instance = new self();
return self::$instance;
* @param mixed $value
* @return mixed
public function serialize($value)
return $value;
* @throws Error
public function parseValue($value)
return $this->decodeJSON($value);
* @param mixed $valueNode
* @param mixed[]|null $variables
* @throws Exception
public function parseLiteral($valueNode, ?array $variables = null)
if (!property_exists($valueNode, 'value')) {
throw new Error(
'Can only parse literals that contain a value, got '.GraphQLUtils::printSafeJson($valueNode)
);
return $this->decodeJSON($valueNode->value);
*
private function decodeJSON($value)
$decoded = is_string($value) ? json_decode($value, true) : $value;
if ($decoded === null) {
'Error decoding JSON '. GraphQLUtils::printSafeJson($value)
return $decoded;