|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* PostType.php |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
namespace Examples\Blog\Schema; |
|
7
|
|
|
|
|
8
|
|
|
use Youshido\GraphQL\Type\Config\TypeConfigInterface; |
|
9
|
|
|
use Youshido\GraphQL\Type\NonNullType; |
|
10
|
|
|
use Youshido\GraphQL\Type\Object\AbstractObjectType; |
|
11
|
|
|
use Youshido\GraphQL\Type\Scalar\BooleanType; |
|
12
|
|
|
use Youshido\GraphQL\Type\Scalar\IntType; |
|
13
|
|
|
use Youshido\GraphQL\Type\Scalar\StringType; |
|
14
|
|
|
use Youshido\GraphQL\Type\Traits\AutoNameTrait; |
|
15
|
|
|
|
|
16
|
|
|
class PostType extends AbstractObjectType |
|
17
|
|
|
{ |
|
18
|
|
|
public function build(TypeConfigInterface $config) |
|
19
|
|
|
{ |
|
20
|
|
|
$config |
|
21
|
|
|
->addField('oldTitle', new NonNullType(new StringType()), [ |
|
22
|
|
|
'description' => 'This field contains a post title', |
|
23
|
|
|
'isDeprecated' => true, |
|
24
|
|
|
'deprecationReason' => 'field title is now deprecated', |
|
25
|
|
|
'args' => [ |
|
26
|
|
|
'truncated' => new BooleanType() |
|
27
|
|
|
], |
|
28
|
|
View Code Duplication |
'resolve' => function ($value, $args) { |
|
|
|
|
|
|
29
|
|
|
return (!empty($args['truncated'])) ? explode(' ', $value)[0] . '...' : $value; |
|
30
|
|
|
} |
|
31
|
|
|
]) |
|
32
|
|
|
->addField('title', new NonNullType(new StringType())) |
|
33
|
|
|
->addField('status', new PostStatus()) |
|
34
|
|
|
->addField('summary', new StringType()) |
|
35
|
|
|
->addField('likeCount', new IntType()); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function getInterfaces() |
|
39
|
|
|
{ |
|
40
|
|
|
return [new ContentBlockInterface()]; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function resolve($value = null, $args = [], $type = null) |
|
44
|
|
|
{ |
|
45
|
|
|
return DataProvider::getPost(empty($args['id']) ? 1 : $args['id']); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
} |
|
49
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.