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:46 PM |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Youshido\GraphQL\Relay\Field; |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
use Youshido\GraphQL\Config\Field\FieldConfig; |
13
|
|
|
use Youshido\GraphQL\Execution\ResolveInfo; |
14
|
|
|
use Youshido\GraphQL\Field\AbstractField; |
15
|
|
|
use Youshido\GraphQL\Field\InputField; |
16
|
|
|
use Youshido\GraphQL\Relay\Fetcher\FetcherInterface; |
17
|
|
|
use Youshido\GraphQL\Relay\Node; |
18
|
|
|
use Youshido\GraphQL\Relay\NodeInterfaceType; |
19
|
|
|
use Youshido\GraphQL\Type\NonNullType; |
20
|
|
|
use Youshido\GraphQL\Type\Scalar\IdType; |
21
|
|
|
|
22
|
|
|
class NodeField extends AbstractField |
23
|
|
|
{ |
24
|
|
|
|
25
|
|
|
/** @var FetcherInterface */ |
26
|
|
|
protected $fetcher; |
27
|
|
|
|
28
|
|
|
/** @var NodeInterfaceType */ |
29
|
|
|
protected $type; |
30
|
|
|
|
31
|
1 |
|
public function __construct(FetcherInterface $fetcher) |
32
|
|
|
{ |
33
|
1 |
|
$this->fetcher = $fetcher; |
34
|
1 |
|
$this->type = (new NodeInterfaceType())->setFetcher($this->fetcher); |
35
|
|
|
|
36
|
1 |
|
parent::__construct([]); |
37
|
1 |
|
} |
38
|
|
|
|
39
|
1 |
|
public function getName() |
40
|
|
|
{ |
41
|
1 |
|
return 'node'; |
42
|
|
|
} |
43
|
|
|
|
44
|
1 |
|
public function getDescription() |
45
|
|
|
{ |
46
|
1 |
|
return 'Fetches an object given its ID'; |
47
|
|
|
} |
48
|
|
|
|
49
|
1 |
|
public function build(FieldConfig $config) |
50
|
|
|
{ |
51
|
1 |
|
$config->addArgument(new InputField([ |
52
|
1 |
|
'name' => 'id', |
53
|
1 |
|
'type' => new NonNullType(new IdType()), |
54
|
|
|
'description' => 'The ID of an object' |
55
|
1 |
|
])); |
56
|
1 |
|
} |
57
|
|
|
|
58
|
1 |
|
public function getType() |
59
|
|
|
{ |
60
|
1 |
|
return $this->type; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function resolve($value, array $args, ResolveInfo $info) |
64
|
|
|
{ |
65
|
|
|
list($type, $id) = Node::fromGlobalId($args['id']); |
66
|
|
|
|
67
|
|
|
return $this->fetcher->resolveNode($type, $id); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
|
71
|
|
|
} |
72
|
|
|
|