Completed
Push — master ( 29ef50...718f9d )
by Alexandr
03:01
created

Issue109Schema::build()   A

Complexity

Conditions 4
Paths 1

Size

Total Lines 58
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 58
rs 9.0077
cc 4
eloc 34
nc 1
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace Youshido\Tests\Issues\Issue109;
3
4
use Youshido\GraphQL\Config\Schema\SchemaConfig;
5
use Youshido\GraphQL\Execution\ResolveInfo;
6
use Youshido\GraphQL\Schema\AbstractSchema;
7
use Youshido\GraphQL\Type\ListType\ListType;
8
use Youshido\GraphQL\Type\Object\ObjectType;
9
use Youshido\GraphQL\Type\Scalar\IntType;
10
11
class Issue109Schema extends AbstractSchema
12
{
13
14
    public function build(SchemaConfig $config)
15
    {
16
        $config->setQuery(
17
            new ObjectType([
18
                'name'   => 'RootQueryType',
19
                'fields' => [
20
                    'latestPost' => [
21
                        'type'    => new ObjectType([
22
                            'name'   => 'Post',
23
                            'fields' => [
24
                                'id'       => [
25
                                    'type' => new IntType(),
26
                                    'args' => [
27
                                        'comment_id' => new IntType()
28
                                    ]
29
                                ],
30
                                'comments' => [
31
                                    'type' => new ListType(new ObjectType([
32
                                        'name'   => 'Comment',
33
                                        'fields' => [
34
                                            'comment_id' => new IntType()
35
                                        ]
36
                                    ])),
37
                                    'args' => [
38
                                        'comment_id' => new IntType()
39
                                    ]
40
                                ]
41
                            ]
42
                        ]),
43
                        'resolve' => function ($source, array $args, ResolveInfo $info) {
44
                            $internalArgs = [
45
                                'comment_id' => 200
46
                            ];
47
                            foreach ($info->getFieldASTList() as $field) {
48
                                if ($field->getName() == 'comments' && $field->hasArguments()) {
49
                                    $internalArgs = $field->getKeyValueArguments();
50
                                }
51
                            }
52
53
                            return [
54
                                "id"       => 1,
55
                                "title"    => "New approach in API has been revealed",
56
                                "summary"  => "In two words - GraphQL Rocks!",
57
                                "comments" => [
58
                                    [
59
                                        "comment_id" => $internalArgs['comment_id']
60
                                    ]
61
                                ]
62
                            ];
63
                        },
64
                        'args'    => [
65
                            'id' => new IntType()
66
                        ]
67
                    ]
68
                ]
69
            ])
70
        );
71
    }
72
73
}