|
1
|
|
|
<?php |
|
2
|
|
|
namespace GraphQL\Examples\Blog\Type; |
|
3
|
|
|
|
|
4
|
|
|
use GraphQL\Examples\Blog\AppContext; |
|
5
|
|
|
use GraphQL\Examples\Blog\Data\Comment; |
|
6
|
|
|
use GraphQL\Examples\Blog\Data\DataSource; |
|
7
|
|
|
use GraphQL\Examples\Blog\Types; |
|
8
|
|
|
use GraphQL\Type\Definition\ObjectType; |
|
9
|
|
|
use GraphQL\Type\Definition\ResolveInfo; |
|
10
|
|
|
|
|
11
|
|
|
class CommentType extends ObjectType |
|
12
|
|
|
{ |
|
13
|
|
|
public function __construct() |
|
14
|
|
|
{ |
|
15
|
|
|
$config = [ |
|
16
|
|
|
'name' => 'Comment', |
|
17
|
|
|
'fields' => function() { |
|
18
|
|
|
return [ |
|
19
|
|
|
'id' => Types::id(), |
|
20
|
|
|
'author' => Types::user(), |
|
21
|
|
|
'parent' => Types::comment(), |
|
22
|
|
|
'isAnonymous' => Types::boolean(), |
|
23
|
|
|
'replies' => [ |
|
24
|
|
|
'type' => Types::listOf(Types::comment()), |
|
25
|
|
|
'args' => [ |
|
26
|
|
|
'after' => Types::int(), |
|
27
|
|
|
'limit' => [ |
|
28
|
|
|
'type' => Types::int(), |
|
29
|
|
|
'defaultValue' => 5 |
|
30
|
|
|
] |
|
31
|
|
|
] |
|
32
|
|
|
], |
|
33
|
|
|
'totalReplyCount' => Types::int(), |
|
34
|
|
|
|
|
35
|
|
|
Types::htmlField('body') |
|
36
|
|
|
]; |
|
37
|
|
|
}, |
|
38
|
|
|
'resolveField' => function($comment, $args, $context, ResolveInfo $info) { |
|
39
|
|
|
$method = 'resolve' . ucfirst($info->fieldName); |
|
40
|
|
|
if (method_exists($this, $method)) { |
|
41
|
|
|
return $this->{$method}($comment, $args, $context, $info); |
|
42
|
|
|
} else { |
|
43
|
|
|
return $comment->{$info->fieldName}; |
|
44
|
|
|
} |
|
45
|
|
|
} |
|
46
|
|
|
]; |
|
47
|
|
|
parent::__construct($config); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function resolveAuthor(Comment $comment) |
|
51
|
|
|
{ |
|
52
|
|
|
if ($comment->isAnonymous) { |
|
53
|
|
|
return null; |
|
54
|
|
|
} |
|
55
|
|
|
return DataSource::findUser($comment->authorId); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function resolveParent(Comment $comment) |
|
59
|
|
|
{ |
|
60
|
|
|
if ($comment->parentId) { |
|
61
|
|
|
return DataSource::findComment($comment->parentId); |
|
62
|
|
|
} |
|
63
|
|
|
return null; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function resolveReplies(Comment $comment, $args) |
|
67
|
|
|
{ |
|
68
|
|
|
$args += ['after' => null]; |
|
69
|
|
|
return DataSource::findReplies($comment->id, $args['limit'], $args['after']); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function resolveTotalReplyCount(Comment $comment) |
|
73
|
|
|
{ |
|
74
|
|
|
return DataSource::countReplies($comment->id); |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|