1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Digia\GraphQL\Execution; |
4
|
|
|
|
5
|
|
|
use Digia\GraphQL\Error\GraphQLError; |
6
|
|
|
use Digia\GraphQL\Language\AST\Node\ArgumentNode; |
7
|
|
|
use Digia\GraphQL\Language\AST\Node\FieldNode; |
8
|
|
|
use Digia\GraphQL\Language\AST\Node\FragmentDefinitionNode; |
9
|
|
|
use Digia\GraphQL\Language\AST\Node\InputValueDefinitionNode; |
10
|
|
|
use Digia\GraphQL\Language\AST\Node\OperationDefinitionNode; |
11
|
|
|
use Digia\GraphQL\Language\AST\Node\SelectionSetNode; |
12
|
|
|
use Digia\GraphQL\Language\AST\NodeKindEnum; |
13
|
|
|
use Digia\GraphQL\Type\Definition\ObjectType; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class AbstractStrategy |
17
|
|
|
* @package Digia\GraphQL\Execution\Strategies |
18
|
|
|
*/ |
19
|
|
|
abstract class ExecutionStrategy |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var ExecutionContext |
23
|
|
|
*/ |
24
|
|
|
protected $context; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var OperationDefinitionNode |
28
|
|
|
*/ |
29
|
|
|
protected $operation; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var mixed |
33
|
|
|
*/ |
34
|
|
|
protected $rootValue; |
35
|
|
|
|
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var array |
39
|
|
|
*/ |
40
|
|
|
protected $finalResult; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* AbstractStrategy constructor. |
44
|
|
|
* @param ExecutionContext $context |
45
|
|
|
* |
46
|
|
|
* @param OperationDefinitionNode $operation |
47
|
|
|
*/ |
48
|
|
|
public function __construct( |
49
|
|
|
ExecutionContext $context, |
50
|
|
|
OperationDefinitionNode $operation, |
51
|
|
|
$rootValue) |
52
|
|
|
{ |
53
|
|
|
$this->context = $context; |
54
|
|
|
$this->operation = $operation; |
55
|
|
|
$this->rootValue = $rootValue; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @return array|null |
60
|
|
|
*/ |
61
|
|
|
abstract function execute(): ?array; |
|
|
|
|
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param ObjectType $runtimeType |
65
|
|
|
* @param SelectionSetNode $selectionSet |
66
|
|
|
* @param $fields |
67
|
|
|
* @param $visitedFragmentNames |
68
|
|
|
* @return \ArrayObject |
69
|
|
|
*/ |
70
|
|
|
protected function collectFields( |
71
|
|
|
ObjectType $runtimeType, |
72
|
|
|
SelectionSetNode $selectionSet, |
73
|
|
|
$fields, |
74
|
|
|
$visitedFragmentNames |
75
|
|
|
) { |
76
|
|
|
foreach ($selectionSet->getSelections() as $selection) { |
77
|
|
|
/** @var FieldNode $selection */ |
78
|
|
|
switch ($selection->getKind()) { |
79
|
|
|
case NodeKindEnum::FIELD: |
80
|
|
|
$name = $this->getFieldNameKey($selection); |
81
|
|
|
if (!isset($fields[$name])) { |
82
|
|
|
$fields[$name] = new \ArrayObject(); |
83
|
|
|
} |
84
|
|
|
$fields[$name][] = $selection; |
85
|
|
|
break; |
86
|
|
|
case NodeKindEnum::INLINE_FRAGMENT: |
87
|
|
|
//TODO check if should include this node |
88
|
|
|
$this->collectFields( |
89
|
|
|
$runtimeType, |
90
|
|
|
$selection->getSelectionSet(), |
91
|
|
|
$fields, |
92
|
|
|
$visitedFragmentNames |
93
|
|
|
); |
94
|
|
|
break; |
95
|
|
|
case NodeKindEnum::FRAGMENT_SPREAD: |
96
|
|
|
//TODO check if should include this node |
97
|
|
|
if (!empty($visitedFragmentNames[$selection->getNameValue()])) { |
98
|
|
|
continue; |
99
|
|
|
} |
100
|
|
|
$visitedFragmentNames[$selection->getNameValue()] = true; |
101
|
|
|
/** @var FragmentDefinitionNode $fragment */ |
102
|
|
|
$fragment = $this->context->getFragments()[$selection->getNameValue()]; |
103
|
|
|
$this->collectFields( |
104
|
|
|
$runtimeType, |
105
|
|
|
$fragment->getSelectionSet(), |
106
|
|
|
$fields, |
107
|
|
|
$visitedFragmentNames |
108
|
|
|
); |
109
|
|
|
break; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return $fields; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param FieldNode $node |
118
|
|
|
* @return string |
119
|
|
|
*/ |
120
|
|
|
private function getFieldNameKey(FieldNode $node) |
121
|
|
|
{ |
122
|
|
|
return $node->getAlias() |
123
|
|
|
? $node->getAlias()->getValue() |
124
|
|
|
: $node->getNameValue(); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Implements the "Evaluating selection sets" section of the spec |
129
|
|
|
* for "read" mode. |
130
|
|
|
* @param ObjectType $parentType |
131
|
|
|
* @param $source |
132
|
|
|
* @param $path |
133
|
|
|
* @param $fields |
134
|
|
|
* |
135
|
|
|
* @return array |
136
|
|
|
* |
137
|
|
|
* @throws GraphQLError|\Exception |
138
|
|
|
*/ |
139
|
|
|
protected function executeFields( |
140
|
|
|
ObjectType $parentType, |
141
|
|
|
$source, |
|
|
|
|
142
|
|
|
$path, |
143
|
|
|
$fields): array |
144
|
|
|
{ |
145
|
|
|
$finalResults = []; |
146
|
|
|
|
147
|
|
|
foreach ($fields as $fieldName => $fieldNodes) { |
148
|
|
|
$fieldPath = $path; |
149
|
|
|
$fieldPath[] = $fieldName; |
150
|
|
|
|
151
|
|
|
$result = $this->resolveField($parentType, |
152
|
|
|
[], |
153
|
|
|
$fieldNodes, |
154
|
|
|
$fieldPath |
155
|
|
|
); |
156
|
|
|
|
157
|
|
|
$finalResults[$fieldName] = $result; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
return $finalResults; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @param ObjectType $parentType |
165
|
|
|
* @param $source |
166
|
|
|
* @param $fieldNodes |
167
|
|
|
* @param $path |
168
|
|
|
* |
169
|
|
|
* @return mixed |
170
|
|
|
* |
171
|
|
|
* @throws GraphQLError|\Exception |
172
|
|
|
*/ |
173
|
|
|
protected function resolveField( |
174
|
|
|
ObjectType $parentType, |
175
|
|
|
$source, |
176
|
|
|
$fieldNodes, |
177
|
|
|
$path) |
178
|
|
|
{ |
179
|
|
|
$result = []; |
180
|
|
|
/** @var FieldNode $fieldNode */ |
181
|
|
|
foreach ($fieldNodes as $fieldNode) { |
182
|
|
|
$field = $parentType->getFields()[$fieldNode->getNameValue()]; |
183
|
|
|
$inputValues = $fieldNode->getArguments() ?? []; |
184
|
|
|
$args = []; |
185
|
|
|
$subResult = []; |
|
|
|
|
186
|
|
|
|
187
|
|
|
foreach ($inputValues as $value) { |
188
|
|
|
if ($value instanceof ArgumentNode) { |
189
|
|
|
$args[] = $value->getValue()->getValue(); |
|
|
|
|
190
|
|
|
} elseif ($value instanceof InputValueDefinitionNode) { |
191
|
|
|
$args[] = $value->getDefaultValue()->getValue(); |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
$subResult = $field->resolve(...$args); |
196
|
|
|
|
197
|
|
|
if (!empty($fieldNode->getSelectionSet())) { |
198
|
|
|
$fields = $this->collectFields( |
199
|
|
|
$parentType, |
200
|
|
|
$fieldNode->getSelectionSet(), |
201
|
|
|
new \ArrayObject(), |
202
|
|
|
new \ArrayObject() |
203
|
|
|
); |
204
|
|
|
|
205
|
|
|
$data = $this->executeFields( |
206
|
|
|
$parentType, |
207
|
|
|
$source, |
208
|
|
|
$path, |
209
|
|
|
$fields |
210
|
|
|
); |
211
|
|
|
|
212
|
|
|
$subResult = array_merge_recursive($result, $data); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
$result = $subResult; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
return $result; |
219
|
|
|
} |
220
|
|
|
} |
221
|
|
|
|
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.