1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Drupal\graphql\GraphQL\Visitors; |
4
|
|
|
|
5
|
|
|
use GraphQL\Language\AST\NodeKind; |
6
|
|
|
use GraphQL\Language\AST\OperationDefinitionNode; |
7
|
|
|
use GraphQL\Language\AST\SelectionSetNode; |
8
|
|
|
use GraphQL\Language\AST\VariableDefinition; |
9
|
|
|
use GraphQL\Validator\Rules\AbstractQuerySecurity; |
10
|
|
|
use GraphQL\Validator\ValidationContext; |
11
|
|
|
|
12
|
|
|
class QueryEdgeCollector extends AbstractQuerySecurity { |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var array |
16
|
|
|
*/ |
17
|
|
|
private $calculators; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* QueryEdgeCollector constructor. |
21
|
|
|
* |
22
|
|
|
* @param array $calculators |
23
|
|
|
*/ |
24
|
|
|
public function __construct(array $calculators) { |
25
|
|
|
$this->calculators = $calculators; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* {@inheritdoc} |
30
|
|
|
*/ |
31
|
|
|
protected function isEnabled() { |
32
|
|
|
return !empty($this->calculators); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* {@inheritdoc} |
37
|
|
|
*/ |
38
|
|
|
public function getVisitor(ValidationContext $context) { |
39
|
|
|
$variables = new \ArrayObject(); |
40
|
|
|
$structure = new \ArrayObject(); |
41
|
|
|
|
42
|
|
|
return $this->invokeIfNeeded($context, [ |
43
|
|
|
NodeKind::SELECTION_SET => function (SelectionSetNode $set) use ($context, &$structure) { |
44
|
|
|
$parent = $context->getParentType(); |
45
|
|
|
$structure = $this->collectFieldASTsAndDefs($context, $parent, $set, NULL, $structure); |
46
|
|
|
}, |
47
|
|
|
NodeKind::VARIABLE_DEFINITION => function (VariableDefinition $definition) use ($variables) { |
48
|
|
|
$variables->append($definition); |
49
|
|
|
}, |
50
|
|
|
NodeKind::OPERATION_DEFINITION => [ |
51
|
|
|
'leave' => function (OperationDefinitionNode $definition) use ($context, $variables, $structure) { |
52
|
|
|
foreach ($this->calculators as $visitor) { |
53
|
|
|
$visitor->calculate($definition, $context, $variables, $structure); |
54
|
|
|
} |
55
|
|
|
}, |
56
|
|
|
], |
57
|
|
|
]); |
58
|
|
|
} |
59
|
|
|
} |