Skip to content

Commit cfb37cf

Browse files
committed
Implement parseLiteral for Blob
1 parent 884ba1c commit cfb37cf

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

src/Type/Blob.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use GraphQL\Error\Error;
88
use GraphQL\Language\AST\Node as ASTNode;
9+
use GraphQL\Language\AST\StringValueNode;
910
use GraphQL\Type\Definition\ScalarType;
1011

1112
use function base64_decode;
@@ -23,7 +24,14 @@ class Blob extends ScalarType
2324

2425
public function parseLiteral(ASTNode $valueNode, array|null $variables = null): string
2526
{
26-
throw new Error('Blob fields cannot be parsed literally.', $valueNode);
27+
// @codeCoverageIgnoreStart
28+
if (! $valueNode instanceof StringValueNode) {
29+
throw new Error('Query error: Can only parse strings got: ' . $valueNode->kind, $valueNode);
30+
}
31+
32+
// @codeCoverageIgnoreEnd
33+
34+
return $this->parseValue($valueNode->value);
2735
}
2836

2937
public function parseValue(mixed $value): mixed

test/Feature/Type/BlobTest.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,15 @@ public function testParseValueInvalid(): void
5555

5656
public function testParseLiteral(): void
5757
{
58-
$this->expectException(Error::class);
58+
$jsonType = new Blob();
59+
$file = file_get_contents(__DIR__ . '/../../../docs/banner.png');
60+
$encoded = base64_encode($file);
5961

60-
$jsonType = new Blob();
6162
$node = new StringValueNode([]);
62-
$node->value = 'search string';
63+
$node->value = $encoded;
6364
$result = $jsonType->parseLiteral($node);
65+
66+
$this->assertEquals($file, $result);
6467
}
6568

6669
public function testSerialize(): void

0 commit comments

Comments
 (0)