Skip to content

Commit

Permalink
Fix: JsonType doesn't work (#632)
Browse files Browse the repository at this point in the history
  • Loading branch information
robertSt7 authored Nov 15, 2022
1 parent 9c01ef1 commit d3e4424
Showing 1 changed file with 6 additions and 35 deletions.
41 changes: 6 additions & 35 deletions src/GraphQL/SharedType/JsonType.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,56 +15,27 @@

namespace Pimcore\Bundle\DataHubBundle\GraphQL\SharedType;

use GraphQL\Error\Error;
use GraphQL\Type\Definition\ScalarType;
use GraphQL\Utils\Utils as GraphQLUtils;
use Safe\Exceptions\JsonException;

class JsonType extends ScalarType
{
public $description /** @lang Markdown */
= 'Arbitrary data encoded in JavaScript Object Notation. See https://www.json.org.';

public function serialize(mixed $value): string
{
return \Safe\json_encode($value);
return json_encode($value);
}

public function parseValue(mixed $value)
public function parseValue(mixed $value): mixed
{
return $this->decodeJSON($value);
return json_decode($value);
}

public function parseLiteral(mixed $valueNode, ?array $variables = null)
public function parseLiteral(mixed $valueNode, ?array $variables = null): mixed
{
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);
}

/**
* Try to decode a user-given JSON value.
*
* @param mixed $value A user given JSON
*
* @throws Error
*
* @return mixed The decoded value
*/
protected function decodeJSON(mixed $value): mixed
{
try {
$decoded = \Safe\json_decode($value);
} catch (JsonException $jsonException) {
throw new Error(
$jsonException->getMessage()
);
throw new \Exception('Can only parse objects with a value property. Input: ' . GraphQLUtils::printSafeJson($valueNode));
}

return $decoded;
return json_decode($valueNode->value);
}
}

0 comments on commit d3e4424

Please sign in to comment.