|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace GraphQL\Upload; |
|
6
|
|
|
|
|
7
|
|
|
use GraphQL\Error\Error; |
|
8
|
|
|
use GraphQL\Error\InvariantViolation; |
|
9
|
|
|
use GraphQL\Language\AST\Node; |
|
10
|
|
|
use GraphQL\Type\Definition\ScalarType; |
|
11
|
|
|
use GraphQL\Utils\Utils; |
|
12
|
|
|
use Psr\Http\Message\UploadedFileInterface; |
|
13
|
|
|
use UnexpectedValueException; |
|
14
|
|
|
|
|
15
|
|
|
final class UploadType extends ScalarType |
|
16
|
|
|
{ |
|
17
|
|
|
public string $name = 'Upload'; |
|
18
|
|
|
|
|
19
|
|
|
public ?string $description |
|
20
|
|
|
= 'The `Upload` special type represents a file to be uploaded in the same HTTP request as specified by |
|
21
|
|
|
[graphql-multipart-request-spec](https://github.com/jaydenseric/graphql-multipart-request-spec).'; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Serializes an internal value to include in a response. |
|
25
|
|
|
*/ |
|
26
|
1 |
|
public function serialize(mixed $value): never |
|
27
|
|
|
{ |
|
28
|
1 |
|
throw new InvariantViolation('`Upload` cannot be serialized'); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Parses an externally provided value (query variable) to use as an input. |
|
33
|
|
|
*/ |
|
34
|
11 |
|
public function parseValue(mixed $value): UploadedFileInterface |
|
35
|
|
|
{ |
|
36
|
11 |
|
if (!$value instanceof UploadedFileInterface) { |
|
37
|
1 |
|
throw new UnexpectedValueException('Could not get uploaded file, be sure to conform to GraphQL multipart request specification. Instead got: ' . Utils::printSafe($value)); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
10 |
|
$error = $value->getError(); |
|
41
|
10 |
|
if ($error !== UPLOAD_ERR_OK) { |
|
42
|
8 |
|
throw new UploadError($error); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
2 |
|
return $value; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Parses an externally provided literal value (hardcoded in GraphQL query) to use as an input. |
|
50
|
|
|
*/ |
|
51
|
1 |
|
public function parseLiteral(Node $valueNode, ?array $variables = null): mixed |
|
52
|
|
|
{ |
|
53
|
1 |
|
throw new Error('`Upload` cannot be hardcoded in query, be sure to conform to GraphQL multipart request specification. Instead got: ' . $valueNode->kind, $valueNode); |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|