1
|
|
|
<?php |
2
|
|
|
namespace GraphQL\Examples\Blog\Type; |
3
|
|
|
|
4
|
|
|
use GraphQL\Examples\Blog\AppContext; |
5
|
|
|
use GraphQL\Examples\Blog\Data\Image; |
6
|
|
|
use GraphQL\Examples\Blog\Types; |
7
|
|
|
use GraphQL\Type\Definition\EnumType; |
8
|
|
|
use GraphQL\Type\Definition\ObjectType; |
9
|
|
|
|
10
|
|
|
class ImageType extends ObjectType |
11
|
|
|
{ |
12
|
|
|
public function __construct() |
13
|
|
|
{ |
14
|
|
|
$config = [ |
15
|
|
|
'name' => 'ImageType', |
16
|
|
|
'fields' => [ |
17
|
|
|
'id' => Types::id(), |
18
|
|
|
'type' => new EnumType([ |
19
|
|
|
'name' => 'ImageTypeEnum', |
20
|
|
|
'values' => [ |
21
|
|
|
'USERPIC' => Image::TYPE_USERPIC |
22
|
|
|
] |
23
|
|
|
]), |
24
|
|
|
'size' => Types::imageSizeEnum(), |
25
|
|
|
'width' => Types::int(), |
26
|
|
|
'height' => Types::int(), |
27
|
|
|
'url' => [ |
28
|
|
|
'type' => Types::url(), |
29
|
|
|
'resolve' => [$this, 'resolveUrl'] |
30
|
|
|
], |
31
|
|
|
|
32
|
|
|
// Just for the sake of example |
33
|
|
|
'fieldWithError' => [ |
34
|
|
|
'type' => Types::string(), |
35
|
|
|
'resolve' => function() { |
36
|
|
|
throw new \Exception("Field with exception"); |
37
|
|
|
} |
38
|
|
|
], |
39
|
|
|
'nonNullFieldWithError' => [ |
40
|
|
|
'type' => Types::nonNull(Types::string()), |
41
|
|
|
'resolve' => function() { |
42
|
|
|
throw new \Exception("Non-null field with exception"); |
43
|
|
|
} |
44
|
|
|
] |
45
|
|
|
] |
46
|
|
|
]; |
47
|
|
|
|
48
|
|
|
parent::__construct($config); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function resolveUrl(Image $value, $args, AppContext $context) |
52
|
|
|
{ |
53
|
|
|
switch ($value->type) { |
54
|
|
|
case Image::TYPE_USERPIC: |
55
|
|
|
$path = "/images/user/{$value->id}-{$value->size}.jpg"; |
56
|
|
|
break; |
57
|
|
|
default: |
58
|
|
|
throw new \UnexpectedValueException("Unexpected image type: " . $value->type); |
59
|
|
|
} |
60
|
|
|
return $context->rootUrl . $path; |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|