1
|
|
|
<?php |
2
|
|
|
/******************************************************************************* |
3
|
|
|
* This file is part of the GraphQL Bundle package. |
4
|
|
|
* |
5
|
|
|
* (c) YnloUltratech <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
******************************************************************************/ |
10
|
|
|
|
11
|
|
|
namespace Ynlo\GraphQLBundle\Resolver; |
12
|
|
|
|
13
|
|
|
use GraphQL\Type\Definition\ResolveInfo; |
14
|
|
|
use Ynlo\GraphQLBundle\Definition\ExecutableDefinitionInterface; |
15
|
|
|
use Ynlo\GraphQLBundle\Definition\InterfaceDefinition; |
16
|
|
|
use Ynlo\GraphQLBundle\Definition\NodeAwareDefinitionInterface; |
17
|
|
|
use Ynlo\GraphQLBundle\Definition\ObjectDefinition; |
18
|
|
|
use Ynlo\GraphQLBundle\Definition\ObjectDefinitionInterface; |
19
|
|
|
use Ynlo\GraphQLBundle\Definition\Registry\Endpoint; |
20
|
|
|
|
21
|
|
|
final class ContextBuilder |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var Endpoint |
25
|
|
|
*/ |
26
|
|
|
private $endpoint; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var ExecutableDefinitionInterface |
30
|
|
|
*/ |
31
|
|
|
private $definition; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var mixed |
35
|
|
|
*/ |
36
|
|
|
private $root; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var ObjectDefinitionInterface |
40
|
|
|
*/ |
41
|
|
|
private $node; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Array of arguments given |
45
|
|
|
* |
46
|
|
|
* @var array |
47
|
|
|
*/ |
48
|
|
|
private $args = []; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var ResolveInfo |
52
|
|
|
*/ |
53
|
|
|
private $resolveInfo; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* ContextBuilder constructor. |
57
|
|
|
* |
58
|
|
|
* @param Endpoint $endpoint |
59
|
|
|
*/ |
60
|
3 |
|
private function __construct(Endpoint $endpoint) |
61
|
|
|
{ |
62
|
3 |
|
$this->endpoint = $endpoint; |
63
|
3 |
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param Endpoint $endpoint |
67
|
|
|
* |
68
|
|
|
* @return ContextBuilder |
69
|
|
|
*/ |
70
|
3 |
|
public static function create(Endpoint $endpoint) |
71
|
|
|
{ |
72
|
3 |
|
return new self($endpoint); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @return ResolverContext |
77
|
|
|
*/ |
78
|
3 |
|
public function build(): ResolverContext |
79
|
|
|
{ |
80
|
3 |
|
return new ResolverContext( |
81
|
3 |
|
$this->endpoint, |
82
|
3 |
|
$this->definition, |
83
|
3 |
|
$this->root, |
84
|
3 |
|
$this->node, |
85
|
3 |
|
$this->args, |
86
|
3 |
|
$this->resolveInfo |
87
|
|
|
); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param ExecutableDefinitionInterface $definition |
92
|
|
|
* |
93
|
|
|
* @return ContextBuilder |
94
|
|
|
*/ |
95
|
3 |
|
public function setDefinition(ExecutableDefinitionInterface $definition): ContextBuilder |
96
|
|
|
{ |
97
|
3 |
|
$this->definition = $definition; |
98
|
|
|
|
99
|
3 |
|
$type = null; |
100
|
3 |
|
if ($definition instanceof NodeAwareDefinitionInterface && $definition->getNode()) { |
101
|
1 |
|
$type = $definition->getNode(); |
102
|
|
|
} |
103
|
|
|
|
104
|
3 |
|
if (!$type) { |
105
|
2 |
|
$type = $definition->getType(); |
106
|
|
|
} |
107
|
|
|
|
108
|
3 |
|
if ($this->endpoint->hasType($type) |
109
|
3 |
|
&& ($nodeDefinition = $this->endpoint->getType($type)) |
110
|
3 |
|
&& ($nodeDefinition instanceof ObjectDefinition || $nodeDefinition instanceof InterfaceDefinition)) { |
111
|
1 |
|
$this->node = $nodeDefinition; |
112
|
|
|
} |
113
|
|
|
|
114
|
3 |
|
return $this; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param mixed $root |
119
|
|
|
* |
120
|
|
|
* @return ContextBuilder |
121
|
|
|
*/ |
122
|
1 |
|
public function setRoot($root): ContextBuilder |
123
|
|
|
{ |
124
|
1 |
|
$this->root = $root; |
125
|
|
|
|
126
|
1 |
|
return $this; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @param array $args |
131
|
|
|
* |
132
|
|
|
* @return ContextBuilder |
133
|
|
|
*/ |
134
|
1 |
|
public function setArgs(array $args): ContextBuilder |
135
|
|
|
{ |
136
|
1 |
|
$this->args = $args; |
137
|
|
|
|
138
|
1 |
|
return $this; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @param ResolveInfo $resolveInfo |
143
|
|
|
* |
144
|
|
|
* @return ContextBuilder |
145
|
|
|
*/ |
146
|
1 |
|
public function setResolveInfo(ResolveInfo $resolveInfo): ContextBuilder |
147
|
|
|
{ |
148
|
1 |
|
$this->resolveInfo = $resolveInfo; |
149
|
|
|
|
150
|
1 |
|
return $this; |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|