1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Date: 23.11.15 |
4
|
|
|
* |
5
|
|
|
* @author Portey Vasil <[email protected]> |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Youshido\GraphQL\Execution; |
9
|
|
|
|
10
|
|
|
use Youshido\GraphQL\Exception\Parser\InvalidRequestException; |
11
|
|
|
use Youshido\GraphQL\Parser\Ast\ArgumentValue\Variable; |
12
|
|
|
use Youshido\GraphQL\Parser\Ast\ArgumentValue\VariableReference; |
13
|
|
|
use Youshido\GraphQL\Parser\Ast\Fragment; |
14
|
|
|
use Youshido\GraphQL\Parser\Ast\FragmentReference; |
15
|
|
|
use Youshido\GraphQL\Parser\Ast\Mutation; |
16
|
|
|
use Youshido\GraphQL\Parser\Ast\Query; |
17
|
|
|
|
18
|
|
|
class Request |
19
|
|
|
{ |
20
|
|
|
|
21
|
|
|
/** @var Query[] */ |
22
|
|
|
private $queries = []; |
23
|
|
|
|
24
|
|
|
/** @var Fragment[] */ |
25
|
|
|
private $fragments = []; |
26
|
|
|
|
27
|
|
|
/** @var Mutation[] */ |
28
|
|
|
private $mutations = []; |
29
|
|
|
|
30
|
|
|
/** @var array */ |
31
|
|
|
private $variables = []; |
32
|
|
|
|
33
|
|
|
/** @var VariableReference[] */ |
34
|
|
|
private $variableReferences = []; |
35
|
|
|
|
36
|
|
|
/** @var array */ |
37
|
|
|
private $queryVariables = []; |
38
|
|
|
|
39
|
|
|
/** @var array */ |
40
|
|
|
private $fragmentReferences = []; |
41
|
|
|
|
42
|
71 |
|
public function __construct($data = [], $variables = []) |
43
|
|
|
{ |
44
|
71 |
|
if (array_key_exists('queries', $data)) { |
45
|
70 |
|
$this->addQueries($data['queries']); |
46
|
70 |
|
} |
47
|
|
|
|
48
|
71 |
|
if (array_key_exists('mutations', $data)) { |
49
|
70 |
|
$this->addMutations($data['mutations']); |
50
|
70 |
|
} |
51
|
|
|
|
52
|
71 |
|
if (array_key_exists('fragments', $data)) { |
53
|
70 |
|
$this->addFragments($data['fragments']); |
54
|
70 |
|
} |
55
|
|
|
|
56
|
71 |
|
if (array_key_exists('fragmentReferences', $data)) { |
57
|
69 |
|
$this->addFragmentReferences($data['fragmentReferences']); |
58
|
69 |
|
} |
59
|
|
|
|
60
|
71 |
|
if (array_key_exists('variables', $data)) { |
61
|
69 |
|
$this->addQueryVariables($data['variables']); |
62
|
69 |
|
} |
63
|
|
|
|
64
|
71 |
|
if (array_key_exists('variableReferences', $data)) { |
65
|
69 |
|
foreach ($data['variableReferences'] as $ref) { |
66
|
9 |
|
if (!array_key_exists($ref->getName(), $variables)) { |
67
|
|
|
/** @var Variable $variable */ |
68
|
|
|
$variable = $ref->getVariable(); |
69
|
|
|
if ($variable->hasDefaultValue()) { |
70
|
|
|
$variables[$variable->getName()] = $variable->getDefaultValue()->getValue(); |
71
|
|
|
continue; |
72
|
|
|
} |
73
|
|
|
throw new InvalidRequestException(sprintf("Variable %s hasn't been submitted", $ref->getName()), $ref->getLocation()); |
74
|
|
|
} |
75
|
69 |
|
} |
76
|
|
|
|
77
|
69 |
|
$this->addVariableReferences($data['variableReferences']); |
78
|
69 |
|
} |
79
|
|
|
|
80
|
71 |
|
$this->setVariables($variables); |
81
|
71 |
|
} |
82
|
|
|
|
83
|
70 |
|
public function addQueries($queries) |
84
|
|
|
{ |
85
|
70 |
|
foreach ($queries as $query) { |
86
|
62 |
|
$this->queries[] = $query; |
87
|
70 |
|
} |
88
|
70 |
|
} |
89
|
|
|
|
90
|
70 |
|
public function addMutations($mutations) |
91
|
|
|
{ |
92
|
70 |
|
foreach ($mutations as $mutation) { |
93
|
10 |
|
$this->mutations[] = $mutation; |
94
|
70 |
|
} |
95
|
70 |
|
} |
96
|
|
|
|
97
|
69 |
|
public function addQueryVariables($queryVariables) |
98
|
|
|
{ |
99
|
69 |
|
foreach ($queryVariables as $queryVariable) { |
100
|
10 |
|
$this->queryVariables[] = $queryVariable; |
101
|
69 |
|
} |
102
|
69 |
|
} |
103
|
|
|
|
104
|
69 |
|
public function addVariableReferences($variableReferences) |
105
|
|
|
{ |
106
|
69 |
|
foreach ($variableReferences as $variableReference) { |
107
|
9 |
|
$this->variableReferences[] = $variableReference; |
108
|
69 |
|
} |
109
|
69 |
|
} |
110
|
|
|
|
111
|
69 |
|
public function addFragmentReferences($fragmentReferences) |
112
|
|
|
{ |
113
|
69 |
|
foreach ($fragmentReferences as $fragmentReference) { |
114
|
9 |
|
$this->fragmentReferences[] = $fragmentReference; |
115
|
69 |
|
} |
116
|
69 |
|
} |
117
|
|
|
|
118
|
70 |
|
public function addFragments($fragments) |
119
|
|
|
{ |
120
|
70 |
|
foreach ($fragments as $fragment) { |
121
|
9 |
|
$this->addFragment($fragment); |
122
|
70 |
|
} |
123
|
70 |
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @return Query[] |
127
|
|
|
*/ |
128
|
68 |
|
public function getAllOperations() |
129
|
|
|
{ |
130
|
68 |
|
return array_merge($this->mutations, $this->queries); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @return Query[] |
135
|
|
|
*/ |
136
|
1 |
|
public function getQueries() |
137
|
|
|
{ |
138
|
1 |
|
return $this->queries; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @return Fragment[] |
143
|
|
|
*/ |
144
|
72 |
|
public function getFragments() |
145
|
|
|
{ |
146
|
72 |
|
return $this->fragments; |
147
|
|
|
} |
148
|
|
|
|
149
|
9 |
|
public function addFragment(Fragment $fragment) |
150
|
|
|
{ |
151
|
9 |
|
$this->fragments[] = $fragment; |
152
|
9 |
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @param $name |
156
|
|
|
* |
157
|
|
|
* @return null|Fragment |
158
|
|
|
*/ |
159
|
13 |
|
public function getFragment($name) |
160
|
|
|
{ |
161
|
13 |
|
foreach ($this->fragments as $fragment) { |
162
|
11 |
|
if ($fragment->getName() == $name) { |
163
|
11 |
|
return $fragment; |
164
|
|
|
} |
165
|
10 |
|
} |
166
|
|
|
|
167
|
4 |
|
return null; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @return Mutation[] |
172
|
|
|
*/ |
173
|
1 |
|
public function getMutations() |
174
|
|
|
{ |
175
|
1 |
|
return $this->mutations; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @return bool |
180
|
|
|
*/ |
181
|
1 |
|
public function hasQueries() |
182
|
|
|
{ |
183
|
1 |
|
return (bool)count($this->queries); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @return bool |
188
|
|
|
*/ |
189
|
1 |
|
public function hasMutations() |
190
|
|
|
{ |
191
|
1 |
|
return (bool)count($this->mutations); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* @return bool |
196
|
|
|
*/ |
197
|
1 |
|
public function hasFragments() |
198
|
|
|
{ |
199
|
1 |
|
return (bool)count($this->fragments); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* @return array |
204
|
|
|
*/ |
205
|
2 |
|
public function getVariables() |
206
|
|
|
{ |
207
|
2 |
|
return $this->variables; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* @param array|string $variables |
212
|
|
|
* |
213
|
|
|
* @return $this |
214
|
|
|
*/ |
215
|
71 |
|
public function setVariables($variables) |
216
|
|
|
{ |
217
|
71 |
|
if (!is_array($variables)) { |
218
|
1 |
|
$variables = json_decode($variables, true); |
219
|
1 |
|
} |
220
|
|
|
|
221
|
71 |
|
$this->variables = $variables; |
|
|
|
|
222
|
71 |
|
foreach ($this->variableReferences as $reference) { |
223
|
|
|
/** invalid request with no variable */ |
224
|
9 |
|
if (!$reference->getVariable()) continue; |
225
|
9 |
|
$variableName = $reference->getVariable()->getName(); |
226
|
|
|
|
227
|
|
|
/** no variable was set at the time */ |
228
|
9 |
|
if (!array_key_exists($variableName, $variables)) continue; |
229
|
|
|
|
230
|
9 |
|
$reference->getVariable()->setValue($variables[$variableName]); |
231
|
9 |
|
$reference->setValue($variables[$variableName]); |
232
|
71 |
|
} |
233
|
|
|
|
234
|
71 |
|
return $this; |
235
|
|
|
} |
236
|
|
|
|
237
|
9 |
|
public function getVariable($name) |
238
|
|
|
{ |
239
|
9 |
|
return $this->hasVariable($name) ? $this->variables[$name] : null; |
240
|
|
|
} |
241
|
|
|
|
242
|
9 |
|
public function hasVariable($name) |
243
|
|
|
{ |
244
|
9 |
|
return array_key_exists($name, $this->variables); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* @return array|Variable[] |
249
|
|
|
*/ |
250
|
69 |
|
public function getQueryVariables() |
251
|
|
|
{ |
252
|
69 |
|
return $this->queryVariables; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* @param array $queryVariables |
257
|
|
|
*/ |
258
|
|
|
public function setQueryVariables($queryVariables) |
259
|
|
|
{ |
260
|
|
|
$this->queryVariables = $queryVariables; |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* @return array|FragmentReference[] |
265
|
|
|
*/ |
266
|
74 |
|
public function getFragmentReferences() |
267
|
|
|
{ |
268
|
74 |
|
return $this->fragmentReferences; |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* @param array $fragmentReferences |
273
|
|
|
*/ |
274
|
|
|
public function setFragmentReferences($fragmentReferences) |
275
|
|
|
{ |
276
|
|
|
$this->fragmentReferences = $fragmentReferences; |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* @return array|VariableReference[] |
281
|
|
|
*/ |
282
|
70 |
|
public function getVariableReferences() |
283
|
|
|
{ |
284
|
70 |
|
return $this->variableReferences; |
285
|
|
|
} |
286
|
|
|
} |
287
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..