Completed
Pull Request — master (#204)
by Ryan
11:34
created

BlogSchema   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 14

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 14
dl 0
loc 51
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * BlogSchema.php
4
 */
5
6
namespace Examples\Blog\Schema;
7
8
use Youshido\GraphQL\Config\Schema\SchemaConfig;
9
use Youshido\GraphQL\Execution\ResolveInfo;
10
use Youshido\GraphQL\Schema\AbstractSchema;
11
use Youshido\GraphQL\Type\ListType\ListType;
12
use Youshido\GraphQL\Type\Scalar\StringType;
13
14
class BlogSchema extends AbstractSchema
15
{
16
    public function build(SchemaConfig $config)
17
    {
18
        $config->getQuery()->addFields([
19
            'latestPost'           => [
20
                'type'    => new PostType(),
21
                'resolve' => function ($value, array $args, ResolveInfo $info) {
22
                    return $info->getReturnType()->getOne(empty($args['id']) ? 1 : $args['id']);
23
                }
24
            ],
25
            'randomBanner'         => [
26
                'type'    => new BannerType(),
27
                'resolve' => function () {
28
                    return DataProvider::getBanner(rand(1, 10));
29
                }
30
            ],
31
            'pageContentUnion'     => [
32
                'type'    => new ListType(new ContentBlockUnion()),
33
                'resolve' => function () {
34
                    return [DataProvider::getPost(1), DataProvider::getBanner(1)];
35
                }
36
            ],
37
            'pageContentInterface' => [
38
                'type'    => new ListType(new ContentBlockInterface()),
39
                'resolve' => function () {
40
                    return [DataProvider::getPost(2), DataProvider::getBanner(3)];
41
                }
42
            ]
43
        ]);
44
        $config->getMutation()->addFields([
45
            new LikePostField(),
46
            'createPost' => [
47
                'type'    => new PostType(),
48
                'args'    => [
49
                    'post'   => new PostInputType(),
50
                    'author' => new StringType()
51
                ],
52
                'resolve' => function ($value, array $args, ResolveInfo $info) {
53
                    // code for creating a new post goes here
54
                    // we simple use our DataProvider for now
55
                    $post = DataProvider::getPost(10);
56
                    if (!empty($args['post']['title'])) $post['title'] = $args['post']['title'];
57
58
                    return $post;
59
                }
60
            ]
61
        ]);
62
    }
63
64
}
65