|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Digia\GraphQL\Execution; |
|
4
|
|
|
|
|
5
|
|
|
use Digia\GraphQL\Error\ExecutionException; |
|
6
|
|
|
use Digia\GraphQL\Error\InvalidTypeException; |
|
7
|
|
|
use Digia\GraphQL\Error\InvariantException; |
|
8
|
|
|
use Digia\GraphQL\Language\Node\FieldNode; |
|
9
|
|
|
use Digia\GraphQL\Language\Node\FragmentDefinitionNode; |
|
10
|
|
|
use Digia\GraphQL\Language\Node\FragmentSpreadNode; |
|
11
|
|
|
use Digia\GraphQL\Language\Node\InlineFragmentNode; |
|
12
|
|
|
use Digia\GraphQL\Language\Node\NodeInterface; |
|
13
|
|
|
use Digia\GraphQL\Language\Node\SelectionSetNode; |
|
14
|
|
|
use Digia\GraphQL\Type\Definition\AbstractTypeInterface; |
|
15
|
|
|
use Digia\GraphQL\Type\Definition\ObjectType; |
|
16
|
|
|
use function Digia\GraphQL\Util\typeFromAST; |
|
17
|
|
|
|
|
18
|
|
|
class FieldCollector |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @var ExecutionContext |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $context; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* FieldCollector constructor. |
|
27
|
|
|
* @param ExecutionContext $context |
|
28
|
|
|
*/ |
|
29
|
|
|
public function __construct(ExecutionContext $context) |
|
30
|
|
|
{ |
|
31
|
|
|
$this->context = $context; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @param ObjectType $runtimeType |
|
36
|
|
|
* @param SelectionSetNode $selectionSet |
|
37
|
|
|
* @param array $fields |
|
38
|
|
|
* @param array $visitedFragmentNames |
|
39
|
|
|
* @return array |
|
40
|
|
|
* @throws InvalidTypeException |
|
41
|
|
|
* @throws ExecutionException |
|
42
|
|
|
* @throws InvariantException |
|
43
|
|
|
*/ |
|
44
|
|
|
public function collectFields( |
|
45
|
|
|
ObjectType $runtimeType, |
|
46
|
|
|
SelectionSetNode $selectionSet, |
|
47
|
|
|
array &$fields, |
|
48
|
|
|
array &$visitedFragmentNames |
|
49
|
|
|
): array { |
|
50
|
|
|
foreach ($selectionSet->getSelections() as $selection) { |
|
51
|
|
|
// Check if this Node should be included first |
|
52
|
|
|
if (!$this->shouldIncludeNode($selection)) { |
|
53
|
|
|
continue; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
// Collect fields |
|
57
|
|
|
if ($selection instanceof FieldNode) { |
|
58
|
|
|
$fieldName = $selection->getAliasOrNameValue(); |
|
59
|
|
|
|
|
60
|
|
|
if (!isset($fields[$fieldName])) { |
|
61
|
|
|
$fields[$fieldName] = []; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
$fields[$fieldName][] = $selection; |
|
65
|
|
|
|
|
66
|
|
|
continue; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
if ($selection instanceof InlineFragmentNode) { |
|
70
|
|
|
if (!$this->doesFragmentConditionMatch($selection, $runtimeType)) { |
|
71
|
|
|
continue; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
$this->collectFields($runtimeType, $selection->getSelectionSet(), $fields, $visitedFragmentNames); |
|
75
|
|
|
|
|
76
|
|
|
continue; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
if ($selection instanceof FragmentSpreadNode) { |
|
80
|
|
|
$fragmentName = $selection->getNameValue(); |
|
81
|
|
|
|
|
82
|
|
|
if (!empty($visitedFragmentNames[$fragmentName])) { |
|
83
|
|
|
continue; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
$visitedFragmentNames[$fragmentName] = true; |
|
87
|
|
|
/** @var FragmentDefinitionNode $fragment */ |
|
88
|
|
|
$fragment = $this->context->getFragments()[$fragmentName]; |
|
89
|
|
|
$this->collectFields($runtimeType, $fragment->getSelectionSet(), $fields, $visitedFragmentNames); |
|
90
|
|
|
|
|
91
|
|
|
continue; |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
return $fields; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @param NodeInterface $node |
|
100
|
|
|
* @return bool |
|
101
|
|
|
*/ |
|
102
|
|
|
protected function shouldIncludeNode(NodeInterface $node): bool |
|
103
|
|
|
{ |
|
104
|
|
|
$contextVariables = $this->context->getVariableValues(); |
|
105
|
|
|
|
|
106
|
|
|
$skip = coerceDirectiveValues(SkipDirective(), $node, $contextVariables); |
|
107
|
|
|
|
|
108
|
|
|
if ($skip && $skip['if'] === true) { |
|
109
|
|
|
return false; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
$include = coerceDirectiveValues(IncludeDirective(), $node, $contextVariables); |
|
113
|
|
|
|
|
114
|
|
|
/** @noinspection IfReturnReturnSimplificationInspection */ |
|
115
|
|
|
if ($include && $include['if'] === false) { |
|
116
|
|
|
return false; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
return true; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* @param FragmentDefinitionNode|InlineFragmentNode|NodeInterface $fragment |
|
124
|
|
|
* @param ObjectType $type |
|
125
|
|
|
* @return bool |
|
126
|
|
|
*/ |
|
127
|
|
|
protected function doesFragmentConditionMatch(NodeInterface $fragment, ObjectType $type): bool |
|
128
|
|
|
{ |
|
129
|
|
|
$typeConditionNode = $fragment->getTypeCondition(); |
|
|
|
|
|
|
130
|
|
|
|
|
131
|
|
|
if (null === $typeConditionNode) { |
|
132
|
|
|
return true; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
$conditionalType = typeFromAST($this->context->getSchema(), $typeConditionNode); |
|
136
|
|
|
|
|
137
|
|
|
if ($type === $conditionalType) { |
|
|
|
|
|
|
138
|
|
|
return true; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
if ($conditionalType instanceof AbstractTypeInterface) { |
|
142
|
|
|
return $this->context->getSchema()->isPossibleType($conditionalType, $type); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
return false; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
} |
|
149
|
|
|
|