/
ValidationContext.php
270 lines (234 loc) · 8.19 KB
/
ValidationContext.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
<?php
declare(strict_types=1);
namespace GraphQL\Validator;
use GraphQL\Error\InvariantViolation;
use GraphQL\Language\AST\DocumentNode;
use GraphQL\Language\AST\FieldNode;
use GraphQL\Language\AST\FragmentDefinitionNode;
use GraphQL\Language\AST\FragmentSpreadNode;
use GraphQL\Language\AST\HasSelectionSet;
use GraphQL\Language\AST\InlineFragmentNode;
use GraphQL\Language\AST\NodeKind;
use GraphQL\Language\AST\OperationDefinitionNode;
use GraphQL\Language\AST\SelectionSetNode;
use GraphQL\Language\AST\VariableNode;
use GraphQL\Language\Visitor;
use GraphQL\Type\Definition\CompositeType;
use GraphQL\Type\Definition\EnumType;
use GraphQL\Type\Definition\FieldDefinition;
use GraphQL\Type\Definition\InputObjectType;
use GraphQL\Type\Definition\InputType;
use GraphQL\Type\Definition\ListOfType;
use GraphQL\Type\Definition\NonNull;
use GraphQL\Type\Definition\OutputType;
use GraphQL\Type\Definition\ScalarType;
use GraphQL\Type\Definition\Type;
use GraphQL\Type\Schema;
use GraphQL\Utils\TypeInfo;
use SplObjectStorage;
use function array_merge;
use function array_pop;
use function count;
/**
* An instance of this class is passed as the "this" context to all validators,
* allowing access to commonly useful contextual information from within a
* validation rule.
*/
class ValidationContext extends ASTValidationContext
{
/** @var TypeInfo */
private $typeInfo;
/** @var FragmentDefinitionNode[] */
private $fragments;
/** @var SplObjectStorage */
private $fragmentSpreads;
/** @var SplObjectStorage */
private $recursivelyReferencedFragments;
/** @var SplObjectStorage */
private $variableUsages;
/** @var SplObjectStorage */
private $recursiveVariableUsages;
public function __construct(Schema $schema, DocumentNode $ast, TypeInfo $typeInfo)
{
parent::__construct($ast, $schema);
$this->typeInfo = $typeInfo;
$this->fragmentSpreads = new SplObjectStorage();
$this->recursivelyReferencedFragments = new SplObjectStorage();
$this->variableUsages = new SplObjectStorage();
$this->recursiveVariableUsages = new SplObjectStorage();
}
/**
* @return mixed[][] List of ['node' => VariableNode, 'type' => ?InputObjectType]
*/
public function getRecursiveVariableUsages(OperationDefinitionNode $operation)
{
$usages = $this->recursiveVariableUsages[$operation] ?? null;
if ($usages === null) {
$usages = $this->getVariableUsages($operation);
$fragments = $this->getRecursivelyReferencedFragments($operation);
$allUsages = [$usages];
foreach ($fragments as $fragment) {
$allUsages[] = $this->getVariableUsages($fragment);
}
$usages = array_merge(...$allUsages);
$this->recursiveVariableUsages[$operation] = $usages;
}
return $usages;
}
/**
* @return mixed[][] List of ['node' => VariableNode, 'type' => ?InputObjectType]
*/
private function getVariableUsages(HasSelectionSet $node)
{
$usages = $this->variableUsages[$node] ?? null;
if ($usages === null) {
$newUsages = [];
$typeInfo = new TypeInfo($this->schema);
Visitor::visit(
$node,
Visitor::visitWithTypeInfo(
$typeInfo,
[
NodeKind::VARIABLE_DEFINITION => static function () : bool {
return false;
},
NodeKind::VARIABLE => static function (VariableNode $variable) use (
&$newUsages,
$typeInfo
) : void {
$newUsages[] = [
'node' => $variable,
'type' => $typeInfo->getInputType(),
'defaultValue' => $typeInfo->getDefaultValue(),
];
},
]
)
);
$usages = $newUsages;
$this->variableUsages[$node] = $usages;
}
return $usages;
}
/**
* @return FragmentDefinitionNode[]
*/
public function getRecursivelyReferencedFragments(OperationDefinitionNode $operation)
{
$fragments = $this->recursivelyReferencedFragments[$operation] ?? null;
if ($fragments === null) {
$fragments = [];
$collectedNames = [];
$nodesToVisit = [$operation];
while (count($nodesToVisit) > 0) {
$node = array_pop($nodesToVisit);
$spreads = $this->getFragmentSpreads($node);
foreach ($spreads as $spread) {
$fragName = $spread->name->value;
if ($collectedNames[$fragName] ?? false) {
continue;
}
$collectedNames[$fragName] = true;
$fragment = $this->getFragment($fragName);
if (! $fragment) {
continue;
}
$fragments[] = $fragment;
$nodesToVisit[] = $fragment;
}
}
$this->recursivelyReferencedFragments[$operation] = $fragments;
}
return $fragments;
}
/**
* @param OperationDefinitionNode|FragmentDefinitionNode $node
*
* @return FragmentSpreadNode[]
*/
public function getFragmentSpreads(HasSelectionSet $node) : array
{
$spreads = $this->fragmentSpreads[$node] ?? null;
if ($spreads === null) {
$spreads = [];
/** @var SelectionSetNode[] $setsToVisit */
$setsToVisit = [$node->selectionSet];
while (count($setsToVisit) > 0) {
$set = array_pop($setsToVisit);
foreach ($set->selections as $selection) {
if ($selection instanceof FragmentSpreadNode) {
$spreads[] = $selection;
} else {
assert($selection instanceof FieldNode || $selection instanceof InlineFragmentNode);
$selectionSet = $selection->selectionSet;
if ($selectionSet !== null) {
$setsToVisit[] = $selectionSet;
}
}
}
}
$this->fragmentSpreads[$node] = $spreads;
}
return $spreads;
}
/**
* @param string $name
*
* @return FragmentDefinitionNode|null
*/
public function getFragment($name)
{
$fragments = $this->fragments;
if (! $fragments) {
$fragments = [];
foreach ($this->getDocument()->definitions as $statement) {
if (! ($statement instanceof FragmentDefinitionNode)) {
continue;
}
$fragments[$statement->name->value] = $statement;
}
$this->fragments = $fragments;
}
return $fragments[$name] ?? null;
}
public function getType() : ?OutputType
{
return $this->typeInfo->getType();
}
/**
* @return (CompositeType & Type) | null
*/
public function getParentType() : ?CompositeType
{
return $this->typeInfo->getParentType();
}
/**
* @return (Type & InputType) | null
*/
public function getInputType() : ?InputType
{
return $this->typeInfo->getInputType();
}
/**
* @return (Type&InputType)|null
*/
public function getParentInputType() : ?InputType
{
return $this->typeInfo->getParentInputType();
}
/**
* @return FieldDefinition
*/
public function getFieldDef()
{
return $this->typeInfo->getFieldDef();
}
public function getDirective()
{
return $this->typeInfo->getDirective();
}
public function getArgument()
{
return $this->typeInfo->getArgument();
}
}