|
1
|
|
|
<?php |
|
2
|
|
|
namespace GraphQL\Examples\Blog\Type; |
|
3
|
|
|
|
|
4
|
|
|
use GraphQL\Examples\Blog\AppContext; |
|
5
|
|
|
use GraphQL\Examples\Blog\Data\DataSource; |
|
6
|
|
|
use GraphQL\Examples\Blog\Data\User; |
|
7
|
|
|
use GraphQL\Examples\Blog\Types; |
|
8
|
|
|
use GraphQL\Type\Definition\ObjectType; |
|
9
|
|
|
use GraphQL\Type\Definition\ResolveInfo; |
|
10
|
|
|
|
|
11
|
|
|
class UserType extends ObjectType |
|
12
|
|
|
{ |
|
13
|
|
|
public function __construct() |
|
14
|
|
|
{ |
|
15
|
|
|
$config = [ |
|
16
|
|
|
'name' => 'User', |
|
17
|
|
|
'description' => 'Our blog authors', |
|
18
|
|
|
'fields' => function() { |
|
19
|
|
|
return [ |
|
20
|
|
|
'id' => Types::id(), |
|
21
|
|
|
'email' => Types::email(), |
|
22
|
|
|
'photo' => [ |
|
23
|
|
|
'type' => Types::image(), |
|
24
|
|
|
'description' => 'User photo URL', |
|
25
|
|
|
'args' => [ |
|
26
|
|
|
'size' => Types::nonNull(Types::imageSizeEnum()), |
|
27
|
|
|
] |
|
28
|
|
|
], |
|
29
|
|
|
'firstName' => [ |
|
30
|
|
|
'type' => Types::string(), |
|
31
|
|
|
], |
|
32
|
|
|
'lastName' => [ |
|
33
|
|
|
'type' => Types::string(), |
|
34
|
|
|
], |
|
35
|
|
|
'lastStoryPosted' => Types::story(), |
|
36
|
|
|
'fieldWithError' => [ |
|
37
|
|
|
'type' => Types::string(), |
|
38
|
|
|
'resolve' => function() { |
|
39
|
|
|
throw new \Exception("This is error field"); |
|
40
|
|
|
} |
|
41
|
|
|
] |
|
42
|
|
|
]; |
|
43
|
|
|
}, |
|
44
|
|
|
'interfaces' => [ |
|
45
|
|
|
Types::node() |
|
46
|
|
|
], |
|
47
|
|
|
'resolveField' => function($user, $args, $context, ResolveInfo $info) { |
|
48
|
|
|
$method = 'resolve' . ucfirst($info->fieldName); |
|
49
|
|
|
if (method_exists($this, $method)) { |
|
50
|
|
|
return $this->{$method}($user, $args, $context, $info); |
|
51
|
|
|
} else { |
|
52
|
|
|
return $user->{$info->fieldName}; |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
]; |
|
56
|
|
|
parent::__construct($config); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function resolvePhoto(User $user, $args) |
|
60
|
|
|
{ |
|
61
|
|
|
return DataSource::getUserPhoto($user->id, $args['size']); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public function resolveLastStoryPosted(User $user) |
|
65
|
|
|
{ |
|
66
|
|
|
return DataSource::findLastStoryFor($user->id); |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|