Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions src/Type/Json.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ class Json extends ScalarType
public string|null $description = 'The `json` scalar type represents json data.';

#[Override]
public function parseLiteral(ASTNode $valueNode, array|null $variables = null): string
public function parseLiteral(ASTNode $valueNode, array|null $variables = null): array|null
{
// @codeCoverageIgnoreStart
if (! $valueNode instanceof StringValueNode) {
throw new Error('Query error: Can only parse strings got: ' . $valueNode->kind, $valueNode);
}

return $valueNode->value;
return $this->parseValue($valueNode->value);
// @codeCoverageIgnoreEnd
}

Expand All @@ -56,8 +56,14 @@ public function parseValue(mixed $value): array|null
}

#[Override]
public function serialize(mixed $value): false|string
public function serialize(mixed $value): string|false
{
return json_encode($value);
$return = json_encode($value);

if (! $return) {
throw new Error('Could not serialize JSON data');
}

return $return;
}
}
12 changes: 10 additions & 2 deletions test/Feature/Type/JsonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,18 @@ public function testParseLiteral(): void
{
$jsonType = new Json();
$node = new StringValueNode([]);
$node->value = 'search string';
$node->value = '{"field": "value"}';
$result = $jsonType->parseLiteral($node);

$this->assertTrue(true);
$this->assertEquals(['field' => 'value'], $result);
}

public function testSerializeFails(): void
{
$this->expectException(Error::class);

$jsonType = new Json();
$jsonType->serialize(["name" => "\xB1\x31"]);
}

public function testContains(): void
Expand Down
Loading