1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Digia\GraphQL\Execution; |
4
|
|
|
|
5
|
|
|
use Digia\GraphQL\Error\CoercingException; |
6
|
|
|
use Digia\GraphQL\Error\ExecutionException; |
7
|
|
|
use Digia\GraphQL\Error\InvalidTypeException; |
8
|
|
|
use Digia\GraphQL\Error\InvariantException; |
9
|
|
|
use Digia\GraphQL\Language\Node\ArgumentNode; |
10
|
|
|
use Digia\GraphQL\Language\Node\DirectivesTrait; |
11
|
|
|
use Digia\GraphQL\Language\Node\FieldNode; |
12
|
|
|
use Digia\GraphQL\Language\Node\NameAwareInterface; |
13
|
|
|
use Digia\GraphQL\Language\Node\NodeInterface; |
14
|
|
|
use Digia\GraphQL\Language\Node\ValueNodeInterface; |
15
|
|
|
use Digia\GraphQL\Language\Node\VariableNode; |
16
|
|
|
use Digia\GraphQL\Type\Definition\DirectiveInterface; |
17
|
|
|
use Digia\GraphQL\Type\Definition\Field; |
18
|
|
|
use Digia\GraphQL\Type\Definition\NonNullType; |
19
|
|
|
use Digia\GraphQL\Type\Definition\TypeInterface; |
20
|
|
|
use Digia\GraphQL\Util\ValueNodeCoercer; |
21
|
|
|
use function Digia\GraphQL\Util\find; |
22
|
|
|
use function Digia\GraphQL\Util\keyMap; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Class ValuesResolver |
26
|
|
|
* @package Digia\GraphQL\Execution |
27
|
|
|
*/ |
28
|
|
|
class ValuesResolver |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @var ValueNodeCoercer |
32
|
|
|
*/ |
33
|
|
|
protected $valueNodeCoercer; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* ValuesResolver constructor. |
37
|
|
|
* @param ValueNodeCoercer $valueNodeCoercer |
38
|
|
|
*/ |
39
|
|
|
public function __construct(ValueNodeCoercer $valueNodeCoercer) |
40
|
|
|
{ |
41
|
|
|
$this->valueNodeCoercer = $valueNodeCoercer; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @see http://facebook.github.io/graphql/October2016/#CoerceArgumentValues() |
46
|
|
|
* |
47
|
|
|
* @param Field|DirectiveInterface $objectType |
48
|
|
|
* @param FieldNode $field |
49
|
|
|
* @param array $variableValues |
50
|
|
|
* @return array |
51
|
|
|
* @throws ExecutionException |
52
|
|
|
* @throws InvalidTypeException |
53
|
|
|
* @throws InvariantException |
54
|
|
|
*/ |
55
|
|
|
public function coerceArgumentValues($objectType, $field, array $variableValues = []): array |
56
|
|
|
{ |
57
|
|
|
$coercedValues = []; |
58
|
|
|
$argumentDefinitions = $objectType->getArguments(); |
59
|
|
|
$argumentNodes = $field->getArguments(); |
60
|
|
|
|
61
|
|
|
if (empty($argumentDefinitions) || empty($argumentNodes)) { |
62
|
|
|
return $coercedValues; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$argumentNodeMap = keyMap($argumentNodes, function (ArgumentNode $value) { |
66
|
|
|
return $value->getNameValue(); |
67
|
|
|
}); |
68
|
|
|
|
69
|
|
|
foreach ($argumentDefinitions as $argumentDefinition) { |
70
|
|
|
$argumentName = $argumentDefinition->getName(); |
71
|
|
|
$argumentType = $argumentDefinition->getType(); |
72
|
|
|
/** @var ArgumentNode $argumentNode */ |
73
|
|
|
$argumentNode = $argumentNodeMap[$argumentName]; |
74
|
|
|
$defaultValue = $argumentDefinition->getDefaultValue(); |
75
|
|
|
|
76
|
|
|
if (null === $argumentNode) { |
77
|
|
|
if (null === $defaultValue) { |
78
|
|
|
$coercedValues[$argumentName] = $defaultValue; |
79
|
|
|
} elseif (!$argumentType instanceof NonNullType) { |
80
|
|
|
throw new ExecutionException( |
81
|
|
|
sprintf('Argument "%s" of required type "%s" was not provided.', $argumentName, $argumentType), |
82
|
|
|
[$field] |
83
|
|
|
); |
84
|
|
|
} |
85
|
|
|
} elseif ($argumentNode instanceof VariableNode) { |
86
|
|
|
$coercedValues[$argumentName] = $this->coerceValueForVariableNode( |
87
|
|
|
$argumentNode, |
88
|
|
|
$argumentType, |
89
|
|
|
$argumentName, |
90
|
|
|
$variableValues, |
91
|
|
|
$defaultValue |
92
|
|
|
); |
93
|
|
|
} else { |
94
|
|
|
$coercedValue = null; |
|
|
|
|
95
|
|
|
|
96
|
|
|
try { |
97
|
|
|
$coercedValue = $this->coerceValueFromAST($argumentNode->getValue(), $argumentType, |
98
|
|
|
$variableValues); |
99
|
|
|
} catch (CoercingException $ex) { |
100
|
|
|
// Value nodes that cannot be resolved should be treated as invalid values |
101
|
|
|
// therefore we catch the exception and leave the `$coercedValue` as `null`. |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
if (null === $coercedValue) { |
105
|
|
|
// Note: ValuesOfCorrectType validation should catch this before |
106
|
|
|
// execution. This is a runtime check to ensure execution does not |
107
|
|
|
// continue with an invalid argument value. |
108
|
|
|
throw new ExecutionException( |
109
|
|
|
sprintf('Argument "%s" has invalid value %s.', $argumentName, $valueNode), |
|
|
|
|
110
|
|
|
[$argumentNode->getValue()] |
111
|
|
|
); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
$coercedValues[$argumentName] = $coercedValue; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return $coercedValues; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param DirectiveInterface $directive |
123
|
|
|
* @param NodeInterface|DirectivesTrait $node |
124
|
|
|
* @param array $variableValues |
125
|
|
|
* @return array|null |
126
|
|
|
* @throws ExecutionException |
127
|
|
|
* @throws InvalidTypeException |
128
|
|
|
* @throws InvariantException |
129
|
|
|
*/ |
130
|
|
|
public function getDirectiveValues( |
131
|
|
|
DirectiveInterface $directive, |
132
|
|
|
NodeInterface $node, |
133
|
|
|
array $variableValues = [] |
134
|
|
|
): ?array { |
135
|
|
|
$directiveNode = $node->hasDirectives() |
|
|
|
|
136
|
|
|
? find($node->getDirectives(), function (NameAwareInterface $value) use ($directive) { |
|
|
|
|
137
|
|
|
return $value->getNameValue() === $directive->getName(); |
138
|
|
|
}) : null; |
139
|
|
|
|
140
|
|
|
if (null !== $directiveNode) { |
141
|
|
|
return $this->coerceArgumentValues($directive, $directiveNode, $variableValues); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
return null; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @param ValueNodeInterface|null $valueNode |
149
|
|
|
* @param TypeInterface $type |
150
|
|
|
* @param string $argumentName |
151
|
|
|
* @param array $variableValues |
152
|
|
|
* @return mixed|null |
153
|
|
|
* @throws ExecutionException |
154
|
|
|
* @throws InvalidTypeException |
155
|
|
|
* @throws InvariantException |
156
|
|
|
* @throws CoercingException |
157
|
|
|
*/ |
158
|
|
|
public function coerceValueFromAST( |
159
|
|
|
?ValueNodeInterface $valueNode, |
160
|
|
|
TypeInterface $type, |
161
|
|
|
$variableValues = [] |
162
|
|
|
) { |
163
|
|
|
return $this->valueNodeCoercer->coerce($valueNode, $type, $variableValues); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @param VariableNode $variableNode |
168
|
|
|
* @param TypeInterface $argumentType |
169
|
|
|
* @param string $argumentName |
170
|
|
|
* @param array $variableValues |
171
|
|
|
* @param $defaultValue |
172
|
|
|
* @return mixed |
173
|
|
|
* @throws ExecutionException |
174
|
|
|
*/ |
175
|
|
|
protected function coerceValueForVariableNode( |
176
|
|
|
VariableNode $variableNode, |
177
|
|
|
TypeInterface $argumentType, |
178
|
|
|
string $argumentName, |
179
|
|
|
array $variableValues, |
180
|
|
|
$defaultValue |
181
|
|
|
) { |
182
|
|
|
$variableName = $variableNode->getNameValue(); |
183
|
|
|
|
184
|
|
|
if (!empty($variableValues) && isset($variableValues[$variableName])) { |
185
|
|
|
// Note: this does not check that this variable value is correct. |
186
|
|
|
// This assumes that this query has been validated and the variable |
187
|
|
|
// usage here is of the correct type. |
188
|
|
|
return $variableValues[$variableName]; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
if (null !== $defaultValue) { |
192
|
|
|
return $defaultValue; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
if ($argumentType instanceof NonNullType) { |
196
|
|
|
throw new ExecutionException( |
197
|
|
|
sprintf( |
198
|
|
|
'Argument "%s" of required type "%s" was provided the variable "%s" which was not provided a runtime value.', |
199
|
|
|
$argumentName, |
200
|
|
|
$argumentType, |
201
|
|
|
$variableName |
202
|
|
|
), |
203
|
|
|
[$variableNode->getValue()] |
|
|
|
|
204
|
|
|
); |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
|