1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the OverblogGraphQLBundle package. |
5
|
|
|
* |
6
|
|
|
* (c) Overblog <http://github.com/overblog/> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Overblog\GraphQLBundle\Tests\Functional\App\Resolver; |
13
|
|
|
|
14
|
|
|
use Overblog\GraphQLBundle\Relay\Node\GlobalId; |
15
|
|
|
use Overblog\GraphQLBundle\Resolver\TypeResolver; |
16
|
|
|
|
17
|
|
|
class GlobalResolver |
18
|
|
|
{ |
19
|
|
|
/** @var TypeResolver */ |
20
|
|
|
private $typeResolver; |
21
|
|
|
|
22
|
|
|
private $userData = [ |
23
|
|
|
1 => [ |
24
|
|
|
'id' => 1, |
25
|
|
|
'name' => 'John Doe', |
26
|
|
|
], |
27
|
|
|
2 => [ |
28
|
|
|
'id' => 2, |
29
|
|
|
'name' => 'Jane Smith', |
30
|
|
|
], |
31
|
|
|
]; |
32
|
|
|
|
33
|
|
|
private $photoData = [ |
34
|
|
|
1 => [ |
35
|
|
|
'photoId' => 1, |
36
|
|
|
'width' => 300, |
37
|
|
|
], |
38
|
|
|
2 => [ |
39
|
|
|
'photoId' => 2, |
40
|
|
|
'width' => 400, |
41
|
|
|
], |
42
|
|
|
]; |
43
|
|
|
|
44
|
|
|
private $postData = [ |
45
|
|
|
1 => [ |
46
|
|
|
'id' => 1, |
47
|
|
|
'text' => 'lorem', |
48
|
|
|
'status' => 2, |
49
|
|
|
], |
50
|
|
|
2 => [ |
51
|
|
|
'id' => 2, |
52
|
|
|
'text' => 'ipsum', |
53
|
|
|
'status' => 1, |
54
|
|
|
], |
55
|
|
|
]; |
56
|
|
|
|
57
|
|
|
public function __construct(TypeResolver $typeResolver) |
58
|
|
|
{ |
59
|
|
|
$this->typeResolver = $typeResolver; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function idFetcher($globalId) |
63
|
|
|
{ |
64
|
|
|
list($type, $id) = array_values(GlobalId::fromGlobalId($globalId)); |
65
|
|
|
|
66
|
|
|
if ($type === 'User') { |
67
|
|
|
return $this->userData[$id]; |
68
|
|
|
} elseif ($type === 'Photo') { |
69
|
|
|
return $this->photoData[$id]; |
70
|
|
|
} else { |
71
|
|
|
return $this->postData[$id]; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function typeResolver($value) |
76
|
|
|
{ |
77
|
|
|
if (isset($value['name'])) { |
78
|
|
|
return $this->typeResolver->resolve('User'); |
79
|
|
|
} elseif (isset($value['photoId'])) { |
80
|
|
|
return $this->typeResolver->resolve('Photo'); |
81
|
|
|
} else { |
82
|
|
|
return $this->typeResolver->resolve('Post'); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function resolveAllObjects() |
87
|
|
|
{ |
88
|
|
|
return [ |
89
|
|
|
$this->userData[1], $this->userData[2], |
90
|
|
|
$this->photoData[1], $this->photoData[2], |
91
|
|
|
$this->postData[1], $this->postData[2], |
92
|
|
|
]; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|