1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace GraphQL\Executor; |
6
|
|
|
|
7
|
|
|
use GraphQL\Error\Error; |
8
|
|
|
use GraphQL\Language\AST\ArgumentNode; |
9
|
|
|
use GraphQL\Language\AST\DirectiveNode; |
10
|
|
|
use GraphQL\Language\AST\EnumValueDefinitionNode; |
11
|
|
|
use GraphQL\Language\AST\FieldDefinitionNode; |
12
|
|
|
use GraphQL\Language\AST\FieldNode; |
13
|
|
|
use GraphQL\Language\AST\FragmentSpreadNode; |
14
|
|
|
use GraphQL\Language\AST\InlineFragmentNode; |
15
|
|
|
use GraphQL\Language\AST\NodeList; |
16
|
|
|
use GraphQL\Language\AST\ValueNode; |
17
|
|
|
use GraphQL\Language\AST\VariableDefinitionNode; |
18
|
|
|
use GraphQL\Language\AST\VariableNode; |
19
|
|
|
use GraphQL\Language\Printer; |
20
|
|
|
use GraphQL\Type\Definition\Directive; |
21
|
|
|
use GraphQL\Type\Definition\FieldDefinition; |
22
|
|
|
use GraphQL\Type\Definition\InputType; |
23
|
|
|
use GraphQL\Type\Definition\NonNull; |
24
|
|
|
use GraphQL\Type\Definition\Type; |
25
|
|
|
use GraphQL\Type\Schema; |
26
|
|
|
use GraphQL\Utils\AST; |
27
|
|
|
use GraphQL\Utils\TypeInfo; |
28
|
|
|
use GraphQL\Utils\Utils; |
29
|
|
|
use GraphQL\Utils\Value; |
30
|
|
|
use Throwable; |
31
|
|
|
use function array_key_exists; |
32
|
|
|
use function array_map; |
33
|
|
|
use function sprintf; |
34
|
|
|
|
35
|
|
|
class Values |
36
|
|
|
{ |
37
|
|
|
/** |
38
|
|
|
* Prepares an object map of variables of the correct type based on the provided |
39
|
|
|
* variable definitions and arbitrary input. If the input cannot be coerced |
40
|
|
|
* to match the variable definitions, a Error will be thrown. |
41
|
|
|
* |
42
|
|
|
* @param VariableDefinitionNode[] $varDefNodes |
43
|
|
|
* @param mixed[] $inputs |
44
|
|
|
* @return mixed[] |
45
|
|
|
*/ |
46
|
227 |
|
public static function getVariableValues(Schema $schema, $varDefNodes, array $inputs) |
47
|
|
|
{ |
48
|
227 |
|
$errors = []; |
49
|
227 |
|
$coercedValues = []; |
50
|
227 |
|
foreach ($varDefNodes as $varDefNode) { |
51
|
54 |
|
$varName = $varDefNode->variable->name->value; |
52
|
|
|
/** @var InputType|Type $varType */ |
53
|
54 |
|
$varType = TypeInfo::typeFromAST($schema, $varDefNode->type); |
54
|
|
|
|
55
|
54 |
|
if (! Type::isInputType($varType)) { |
56
|
2 |
|
$errors[] = new Error( |
57
|
2 |
|
sprintf( |
58
|
2 |
|
'Variable "$%s" expected value of type "%s" which cannot be used as an input type.', |
59
|
2 |
|
$varName, |
60
|
2 |
|
Printer::doPrint($varDefNode->type) |
61
|
|
|
), |
62
|
2 |
|
[$varDefNode->type] |
63
|
|
|
); |
64
|
|
|
} else { |
65
|
52 |
|
if (! array_key_exists($varName, $inputs)) { |
66
|
21 |
|
if ($varType instanceof NonNull) { |
67
|
1 |
|
$errors[] = new Error( |
68
|
1 |
|
sprintf( |
69
|
1 |
|
'Variable "$%s" of required type "%s" was not provided.', |
70
|
1 |
|
$varName, |
71
|
1 |
|
$varType |
72
|
|
|
), |
73
|
1 |
|
[$varDefNode] |
74
|
|
|
); |
75
|
20 |
|
} elseif ($varDefNode->defaultValue) { |
76
|
21 |
|
$coercedValues[$varName] = AST::valueFromAST($varDefNode->defaultValue, $varType); |
|
|
|
|
77
|
|
|
} |
78
|
|
|
} else { |
79
|
45 |
|
$value = $inputs[$varName]; |
80
|
45 |
|
$coerced = Value::coerceValue($value, $varType, $varDefNode); |
|
|
|
|
81
|
|
|
/** @var Error[] $coercionErrors */ |
82
|
45 |
|
$coercionErrors = $coerced['errors']; |
83
|
45 |
|
if (! empty($coercionErrors)) { |
84
|
16 |
|
$messagePrelude = sprintf( |
85
|
16 |
|
'Variable "$%s" got invalid value %s; ', |
86
|
16 |
|
$varName, |
87
|
16 |
|
Utils::printSafeJson($value) |
88
|
|
|
); |
89
|
|
|
|
90
|
16 |
|
foreach ($coercionErrors as $error) { |
91
|
16 |
|
$errors[] = new Error( |
92
|
16 |
|
$messagePrelude . $error->getMessage(), |
93
|
16 |
|
$error->getNodes(), |
94
|
16 |
|
$error->getSource(), |
95
|
16 |
|
$error->getPositions(), |
96
|
16 |
|
$error->getPath(), |
97
|
16 |
|
$error, |
98
|
16 |
|
$error->getExtensions() |
99
|
|
|
); |
100
|
|
|
} |
101
|
|
|
} else { |
102
|
54 |
|
$coercedValues[$varName] = $coerced['value']; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
227 |
|
return ['errors' => $errors, 'coerced' => $errors ? null : $coercedValues]; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Prepares an object map of argument values given a directive definition |
113
|
|
|
* and a AST node which may contain directives. Optionally also accepts a map |
114
|
|
|
* of variable values. |
115
|
|
|
* |
116
|
|
|
* If the directive does not exist on the node, returns undefined. |
117
|
|
|
* |
118
|
|
|
* @param FragmentSpreadNode|FieldNode|InlineFragmentNode|EnumValueDefinitionNode|FieldDefinitionNode $node |
119
|
|
|
* @param mixed[]|null $variableValues |
120
|
|
|
* |
121
|
|
|
* @return mixed[]|null |
122
|
|
|
*/ |
123
|
256 |
|
public static function getDirectiveValues(Directive $directiveDef, $node, $variableValues = null) |
124
|
|
|
{ |
125
|
256 |
|
if (isset($node->directives) && $node->directives instanceof NodeList) { |
126
|
256 |
|
$directiveNode = Utils::find( |
127
|
256 |
|
$node->directives, |
128
|
|
|
function (DirectiveNode $directive) use ($directiveDef) { |
129
|
6 |
|
return $directive->name->value === $directiveDef->name; |
130
|
256 |
|
} |
131
|
|
|
); |
132
|
|
|
|
133
|
256 |
|
if ($directiveNode) { |
134
|
6 |
|
return self::getArgumentValues($directiveDef, $directiveNode, $variableValues); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
138
|
256 |
|
return null; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Prepares an object map of argument values given a list of argument |
143
|
|
|
* definitions and list of argument AST nodes. |
144
|
|
|
* |
145
|
|
|
* @param FieldDefinition|Directive $def |
146
|
|
|
* @param FieldNode|DirectiveNode $node |
147
|
|
|
* @param mixed[] $variableValues |
148
|
|
|
* @return mixed[] |
149
|
|
|
* @throws Error |
150
|
|
|
*/ |
151
|
200 |
|
public static function getArgumentValues($def, $node, $variableValues = null) |
152
|
|
|
{ |
153
|
200 |
|
$argDefs = $def->args; |
154
|
200 |
|
$argNodes = $node->arguments; |
155
|
|
|
|
156
|
200 |
|
if (empty($argDefs) || $argNodes === null) { |
157
|
154 |
|
return []; |
158
|
|
|
} |
159
|
|
|
|
160
|
100 |
|
$coercedValues = []; |
161
|
|
|
|
162
|
|
|
/** @var ArgumentNode[] $argNodeMap */ |
163
|
100 |
|
$argNodeMap = $argNodes ? Utils::keyMap( |
164
|
100 |
|
$argNodes, |
165
|
|
|
function (ArgumentNode $arg) { |
166
|
85 |
|
return $arg->name->value; |
167
|
100 |
|
} |
168
|
100 |
|
) : []; |
169
|
|
|
|
170
|
100 |
|
foreach ($argDefs as $argDef) { |
171
|
100 |
|
$name = $argDef->name; |
172
|
100 |
|
$argType = $argDef->getType(); |
173
|
100 |
|
$argumentNode = $argNodeMap[$name] ?? null; |
174
|
|
|
|
175
|
100 |
|
if (! $argumentNode) { |
176
|
41 |
|
if ($argDef->defaultValueExists()) { |
177
|
12 |
|
$coercedValues[$name] = $argDef->defaultValue; |
178
|
31 |
|
} elseif ($argType instanceof NonNull) { |
179
|
1 |
|
throw new Error( |
180
|
1 |
|
'Argument "' . $name . '" of required type ' . |
181
|
1 |
|
'"' . Utils::printSafe($argType) . '" was not provided.', |
182
|
41 |
|
[$node] |
183
|
|
|
); |
184
|
|
|
} |
185
|
85 |
|
} elseif ($argumentNode->value instanceof VariableNode) { |
186
|
32 |
|
$variableName = $argumentNode->value->name->value; |
187
|
|
|
|
188
|
32 |
|
if ($variableValues && array_key_exists($variableName, $variableValues)) { |
189
|
|
|
// Note: this does not check that this variable value is correct. |
190
|
|
|
// This assumes that this query has been validated and the variable |
191
|
|
|
// usage here is of the correct type. |
192
|
25 |
|
$coercedValues[$name] = $variableValues[$variableName]; |
193
|
7 |
|
} elseif ($argDef->defaultValueExists()) { |
194
|
1 |
|
$coercedValues[$name] = $argDef->defaultValue; |
195
|
6 |
|
} elseif ($argType instanceof NonNull) { |
196
|
1 |
|
throw new Error( |
197
|
1 |
|
'Argument "' . $name . '" of required type "' . Utils::printSafe($argType) . '" was ' . |
198
|
1 |
|
'provided the variable "$' . $variableName . '" which was not provided ' . |
199
|
1 |
|
'a runtime value.', |
200
|
32 |
|
[$argumentNode->value] |
201
|
|
|
); |
202
|
|
|
} |
203
|
|
|
} else { |
204
|
56 |
|
$valueNode = $argumentNode->value; |
205
|
56 |
|
$coercedValue = AST::valueFromAST($valueNode, $argType, $variableValues); |
206
|
56 |
|
if (Utils::isInvalid($coercedValue)) { |
207
|
|
|
// Note: ValuesOfCorrectType validation should catch this before |
208
|
|
|
// execution. This is a runtime check to ensure execution does not |
209
|
|
|
// continue with an invalid argument value. |
210
|
3 |
|
throw new Error( |
211
|
3 |
|
'Argument "' . $name . '" has invalid value ' . Printer::doPrint($valueNode) . '.', |
212
|
3 |
|
[$argumentNode->value] |
213
|
|
|
); |
214
|
|
|
} |
215
|
97 |
|
$coercedValues[$name] = $coercedValue; |
216
|
|
|
} |
217
|
|
|
} |
218
|
|
|
|
219
|
97 |
|
return $coercedValues; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* @deprecated as of 8.0 (Moved to \GraphQL\Utils\AST::valueFromAST) |
224
|
|
|
* |
225
|
|
|
* @param ValueNode $valueNode |
226
|
|
|
* @param null $variables |
|
|
|
|
227
|
|
|
* @return mixed[]|null|\stdClass |
228
|
|
|
*/ |
229
|
|
|
public static function valueFromAST($valueNode, InputType $type, $variables = null) |
230
|
|
|
{ |
231
|
|
|
return AST::valueFromAST($valueNode, $type, $variables); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* @deprecated as of 0.12 (Use coerceValue() directly for richer information) |
236
|
|
|
* @param mixed[] $value |
237
|
|
|
* @return string[] |
238
|
|
|
*/ |
239
|
|
|
public static function isValidPHPValue($value, InputType $type) |
240
|
|
|
{ |
241
|
|
|
$errors = Value::coerceValue($value, $type)['errors']; |
242
|
|
|
|
243
|
|
|
return $errors |
244
|
|
|
? array_map( |
245
|
|
|
function (Throwable $error) { |
246
|
|
|
return $error->getMessage(); |
247
|
|
|
}, |
248
|
|
|
$errors |
249
|
|
|
) : []; |
250
|
|
|
} |
251
|
|
|
} |
252
|
|
|
|