1
|
|
|
<?php |
2
|
|
|
/******************************************************************************* |
3
|
|
|
* This file is part of the GraphQL Bundle package. |
4
|
|
|
* |
5
|
|
|
* (c) YnloUltratech <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
******************************************************************************/ |
10
|
|
|
|
11
|
|
|
namespace Ynlo\GraphQLBundle\Type; |
12
|
|
|
|
13
|
|
|
use GraphQL\Error\Error; |
14
|
|
|
use GraphQL\Error\InvariantViolation; |
15
|
|
|
use GraphQL\Type\Definition\ScalarType; |
16
|
|
|
use GraphQL\Utils\Utils; |
17
|
|
|
use Symfony\Component\HttpFoundation\File\UploadedFile; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* The `Upload` special type represents a file to be uploaded in the same HTTP request as specified by |
21
|
|
|
* @see https://github.com/jaydenseric/graphql-multipart-request-spec |
22
|
|
|
*/ |
23
|
|
|
class UploadType extends ScalarType |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
public $name = 'Upload'; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
public $description = |
34
|
|
|
'The `Upload` special type represents a file to be uploaded in the same HTTP request as specified by |
35
|
|
|
[graphql-multipart-request-spec](https://github.com/jaydenseric/graphql-multipart-request-spec).'; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* {@inheritdoc} |
39
|
|
|
*/ |
40
|
|
|
public function serialize($value) |
41
|
|
|
{ |
42
|
|
|
throw new InvariantViolation('`Upload` cannot be serialized'); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritdoc} |
47
|
|
|
*/ |
48
|
|
|
public function parseValue($value) |
49
|
|
|
{ |
50
|
|
|
if (!$value instanceof UploadedFile) { |
51
|
|
|
throw new \UnexpectedValueException( |
52
|
|
|
'Could not get uploaded file, be sure to conform to GraphQL multipart request specification. |
53
|
|
|
Instead got: '.Utils::printSafe($value) |
54
|
|
|
); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return $value; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* {@inheritdoc} |
62
|
|
|
*/ |
63
|
|
|
public function parseLiteral($valueNode) |
64
|
|
|
{ |
65
|
|
|
throw new Error( |
66
|
|
|
'`Upload` cannot be hardcoded in query, be sure to conform to GraphQL multipart request specification. |
67
|
|
|
Instead got: '.$valueNode->kind, [$valueNode] |
68
|
|
|
); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|