Completed
Pull Request — master (#133)
by Adrien
04:00
created

GlobalIdField::getType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
/*
3
* This file is a part of GraphQL project.
4
*
5
* @author Alexandr Viniychuk <[email protected]>
6
* created: 5/10/16 11:23 PM
7
*/
8
9
namespace Youshido\GraphQL\Relay\Field;
10
11
12
use Youshido\GraphQL\Execution\ResolveInfo;
13
use Youshido\GraphQL\Field\AbstractField;
14
use Youshido\GraphQL\Relay\Node;
15
use Youshido\GraphQL\Type\NonNullType;
16
use Youshido\GraphQL\Type\Scalar\IdType;
17
18
class GlobalIdField extends AbstractField
19
{
20
21
    /** @var  string */
22
    protected $typeName;
23
24
    /**
25
     * @param string $typeName
26
     */
27 1
    public function __construct($typeName)
28
    {
29 1
        $this->typeName = $typeName;
30
31
        $config = [
32 1
            'type'    => $this->getType(),
33 1
            'name'    => $this->getName(),
34 1
            'resolve' => [$this, 'resolve']
35 1
        ];
36
37 1
        parent::__construct($config);
38 1
    }
39
40 1
    public function getName()
41
    {
42 1
        return 'id';
43
    }
44
45 1
    public function getDescription()
46
    {
47 1
        return 'The ID of an object';
48
    }
49
50 1
    public function getType()
51
    {
52 1
        return new NonNullType(new IdType());
53
    }
54
55
    /**
56
     * @inheritdoc
57
     */
58
    public function resolve($value, array $args, ResolveInfo $info)
59
    {
60
        return $value ? Node::toGlobalId($this->typeName, $value['id']) : null;
61
    }
62
}
63