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
|
|
|
* AbstractStrategy constructor. |
38
|
|
|
* @param ExecutionContext $context |
39
|
|
|
* |
40
|
|
|
* @param OperationDefinitionNode $operation |
41
|
|
|
*/ |
42
|
|
|
public function __construct( |
43
|
|
|
ExecutionContext $context, |
44
|
|
|
OperationDefinitionNode $operation, |
45
|
|
|
$rootValue) |
46
|
|
|
{ |
47
|
|
|
$this->context = $context; |
48
|
|
|
$this->operation = $operation; |
49
|
|
|
$this->rootValue = $rootValue; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @return array|null |
54
|
|
|
*/ |
55
|
|
|
abstract function execute(): ?array; |
|
|
|
|
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param ObjectType $runtimeType |
59
|
|
|
* @param SelectionSetNode $selectionSet |
60
|
|
|
* @param $fields |
61
|
|
|
* @param $visitedFragmentNames |
62
|
|
|
* @return \ArrayObject |
63
|
|
|
*/ |
64
|
|
|
protected function collectFields( |
65
|
|
|
ObjectType $runtimeType, |
66
|
|
|
SelectionSetNode $selectionSet, |
67
|
|
|
$fields, |
68
|
|
|
$visitedFragmentNames |
69
|
|
|
) { |
70
|
|
|
foreach ($selectionSet->getSelections() as $selection) { |
71
|
|
|
/** @var FieldNode $selection */ |
72
|
|
|
switch ($selection->getKind()) { |
73
|
|
|
case NodeKindEnum::FIELD: |
74
|
|
|
$name = $selection->getNameValue(); |
75
|
|
|
if (!isset($fields[$name])) { |
76
|
|
|
$fields[$name] = new \ArrayObject(); |
77
|
|
|
} |
78
|
|
|
$fields[$name][] = $selection; |
79
|
|
|
break; |
80
|
|
|
case NodeKindEnum::INLINE_FRAGMENT: |
81
|
|
|
//TODO check if should include this node |
82
|
|
|
$this->collectFields( |
83
|
|
|
$runtimeType, |
84
|
|
|
$selection->getSelectionSet(), |
85
|
|
|
$fields, |
86
|
|
|
$visitedFragmentNames |
87
|
|
|
); |
88
|
|
|
break; |
89
|
|
|
case NodeKindEnum::FRAGMENT_SPREAD: |
90
|
|
|
//TODO check if should include this node |
91
|
|
|
if (!empty($visitedFragmentNames[$selection->getNameValue()])) { |
92
|
|
|
continue; |
93
|
|
|
} |
94
|
|
|
$visitedFragmentNames[$selection->getNameValue()] = true; |
95
|
|
|
/** @var FragmentDefinitionNode $fragment */ |
96
|
|
|
$fragment = $this->context->getFragments()[$selection->getNameValue()]; |
97
|
|
|
$this->collectFields( |
98
|
|
|
$runtimeType, |
99
|
|
|
$fragment->getSelectionSet(), |
100
|
|
|
$fields, |
101
|
|
|
$visitedFragmentNames |
102
|
|
|
); |
103
|
|
|
break; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return $fields; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Implements the "Evaluating selection sets" section of the spec |
112
|
|
|
* for "read" mode. |
113
|
|
|
* @param ObjectType $parentType |
114
|
|
|
* @param $source |
115
|
|
|
* @param $path |
116
|
|
|
* @param $fields |
117
|
|
|
* |
118
|
|
|
* @return array |
119
|
|
|
* |
120
|
|
|
* @throws GraphQLError|\Exception |
121
|
|
|
*/ |
122
|
|
|
protected function executeFields( |
123
|
|
|
ObjectType $parentType, |
124
|
|
|
$source, |
125
|
|
|
$path, |
126
|
|
|
$fields): array |
127
|
|
|
{ |
128
|
|
|
$finalResults = []; |
129
|
|
|
|
130
|
|
|
foreach ($fields as $fieldName => $fieldNodes) { |
131
|
|
|
$fieldPath = $path; |
132
|
|
|
$fieldPath[] = $fieldName; |
133
|
|
|
if (!$this->isDefinedField($parentType, $fieldName)) { |
134
|
|
|
continue; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
$result = $this->resolveField($parentType, |
138
|
|
|
$source, |
139
|
|
|
$fieldNodes, |
140
|
|
|
$fieldPath |
141
|
|
|
); |
142
|
|
|
|
143
|
|
|
$finalResults[$fieldName] = $result; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
return $finalResults; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @param ObjectType $parentType |
151
|
|
|
* @param string $fieldName |
152
|
|
|
* @return bool |
153
|
|
|
* @throws \Exception |
154
|
|
|
*/ |
155
|
|
|
protected function isDefinedField(ObjectType $parentType, string $fieldName) |
156
|
|
|
{ |
157
|
|
|
return isset($parentType->getFields()[$fieldName]); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @param ObjectType $parentType |
162
|
|
|
* @param $source |
163
|
|
|
* @param $fieldNodes |
164
|
|
|
* @param $path |
165
|
|
|
* |
166
|
|
|
* @return mixed |
167
|
|
|
* |
168
|
|
|
* @throws GraphQLError|\Exception |
169
|
|
|
*/ |
170
|
|
|
protected function resolveField( |
171
|
|
|
ObjectType $parentType, |
172
|
|
|
$source, |
|
|
|
|
173
|
|
|
$fieldNodes, |
174
|
|
|
$path) |
|
|
|
|
175
|
|
|
{ |
176
|
|
|
/** @var FieldNode $fieldNode */ |
177
|
|
|
$fieldNode = $fieldNodes[0]; |
178
|
|
|
|
179
|
|
|
$field = $parentType->getFields()[$fieldNode->getNameValue()]; |
180
|
|
|
|
181
|
|
|
$inputValues = $fieldNode->getArguments() ?? []; |
182
|
|
|
|
183
|
|
|
$args = []; |
184
|
|
|
|
185
|
|
|
foreach ($inputValues as $value) { |
186
|
|
|
if ($value instanceof ArgumentNode) { |
187
|
|
|
$args[] = $value->getValue()->getValue(); |
|
|
|
|
188
|
|
|
} elseif ($value instanceof InputValueDefinitionNode) { |
189
|
|
|
$args[] = $value->getDefaultValue()->getValue(); |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
$result = $field->resolve(...$args); |
194
|
|
|
|
195
|
|
|
//TODO Resolve sub fields |
196
|
|
|
|
197
|
|
|
return $result; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
private function completeValue( |
|
|
|
|
201
|
|
|
$returnType, |
|
|
|
|
202
|
|
|
$fieldNodes, |
|
|
|
|
203
|
|
|
$info, |
|
|
|
|
204
|
|
|
$path, |
|
|
|
|
205
|
|
|
&$result) |
|
|
|
|
206
|
|
|
{ |
207
|
|
|
|
208
|
|
|
|
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
|
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.