-
-
Notifications
You must be signed in to change notification settings - Fork 572
Expand file tree
/
Copy pathValues.php
More file actions
279 lines (253 loc) · 12.6 KB
/
Values.php
File metadata and controls
279 lines (253 loc) · 12.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
<?php declare(strict_types=1);
namespace GraphQL\Executor;
use GraphQL\Error\Error;
use GraphQL\Language\AST\ArgumentNode;
use GraphQL\Language\AST\DirectiveNode;
use GraphQL\Language\AST\EnumTypeDefinitionNode;
use GraphQL\Language\AST\EnumTypeExtensionNode;
use GraphQL\Language\AST\EnumValueDefinitionNode;
use GraphQL\Language\AST\FieldDefinitionNode;
use GraphQL\Language\AST\FieldNode;
use GraphQL\Language\AST\FragmentDefinitionNode;
use GraphQL\Language\AST\FragmentSpreadNode;
use GraphQL\Language\AST\InlineFragmentNode;
use GraphQL\Language\AST\InputObjectTypeDefinitionNode;
use GraphQL\Language\AST\InputObjectTypeExtensionNode;
use GraphQL\Language\AST\InputValueDefinitionNode;
use GraphQL\Language\AST\InterfaceTypeDefinitionNode;
use GraphQL\Language\AST\InterfaceTypeExtensionNode;
use GraphQL\Language\AST\Node;
use GraphQL\Language\AST\NodeList;
use GraphQL\Language\AST\NullValueNode;
use GraphQL\Language\AST\ObjectTypeDefinitionNode;
use GraphQL\Language\AST\ObjectTypeExtensionNode;
use GraphQL\Language\AST\OperationDefinitionNode;
use GraphQL\Language\AST\ScalarTypeDefinitionNode;
use GraphQL\Language\AST\ScalarTypeExtensionNode;
use GraphQL\Language\AST\SchemaExtensionNode;
use GraphQL\Language\AST\UnionTypeDefinitionNode;
use GraphQL\Language\AST\UnionTypeExtensionNode;
use GraphQL\Language\AST\VariableDefinitionNode;
use GraphQL\Language\AST\VariableNode;
use GraphQL\Language\Printer;
use GraphQL\Type\Definition\Directive;
use GraphQL\Type\Definition\FieldDefinition;
use GraphQL\Type\Definition\NonNull;
use GraphQL\Type\Definition\Type;
use GraphQL\Type\Schema;
use GraphQL\Utils\AST;
use GraphQL\Utils\Utils;
use GraphQL\Utils\Value;
/**
* @see ArgumentNode - force IDE import
*
* @phpstan-import-type ArgumentNodeValue from ArgumentNode
*
* @see \GraphQL\Tests\Executor\ValuesTest
*/
class Values
{
/**
* Prepares an object map of variables of the correct type based on the provided
* variable definitions and arbitrary input. If the input cannot be coerced
* to match the variable definitions, an Error will be thrown.
*
* @param NodeList<VariableDefinitionNode> $varDefNodes
* @param array<string, mixed> $rawVariableValues
*
* @throws \Exception
*
* @return array{array<int, Error>, null}|array{null, array<string, mixed>}
*/
public static function getVariableValues(Schema $schema, NodeList $varDefNodes, array $rawVariableValues): array
{
$errors = [];
$coercedValues = [];
foreach ($varDefNodes as $varDefNode) {
$varName = $varDefNode->variable->name->value;
$varType = AST::typeFromAST([$schema, 'getType'], $varDefNode->type);
if (! Type::isInputType($varType)) {
// Must use input types for variables. This should be caught during
// validation, however is checked again here for safety.
$typeStr = Printer::doPrint($varDefNode->type);
$errors[] = new Error(
"Variable \"\${$varName}\" expected value of type \"{$typeStr}\" which cannot be used as an input type.",
[$varDefNode->type]
);
} else {
$hasValue = \array_key_exists($varName, $rawVariableValues);
$value = $hasValue
? $rawVariableValues[$varName]
: Utils::undefined();
if (! $hasValue && ($varDefNode->defaultValue !== null)) {
// If no value was provided to a variable with a default value,
// use the default value.
$coercedValues[$varName] = AST::valueFromAST($varDefNode->defaultValue, $varType);
} elseif ((! $hasValue || $value === null) && ($varType instanceof NonNull)) {
// If no value or a nullish value was provided to a variable with a
// non-null type (required), produce an error.
$safeVarType = Utils::printSafe($varType);
$message = $hasValue
? "Variable \"\${$varName}\" of non-null type \"{$safeVarType}\" must not be null."
: "Variable \"\${$varName}\" of required type \"{$safeVarType}\" was not provided.";
$errors[] = new Error($message, [$varDefNode]);
} elseif ($hasValue) {
if ($value === null) {
// If the explicit value `null` was provided, an entry in the coerced
// values must exist as the value `null`.
$coercedValues[$varName] = null;
} else {
// Otherwise, a non-null value was provided, coerce it to the expected
// type or report an error if coercion fails.
$coerced = Value::coerceInputValue($value, $varType);
$coercionErrors = $coerced['errors'];
if ($coercionErrors !== null) {
foreach ($coercionErrors as $coercionError) {
$invalidValue = $coercionError->printInvalidValue();
$inputPath = $coercionError->printInputPath();
$pathMessage = $inputPath !== null
? " at \"{$varName}{$inputPath}\""
: '';
$errors[] = new Error(
"Variable \"\${$varName}\" got invalid value {$invalidValue}{$pathMessage}; {$coercionError->getMessage()}",
$varDefNode,
$coercionError->getSource(),
$coercionError->getPositions(),
$coercionError->getPath(),
$coercionError,
$coercionError->getExtensions()
);
}
} else {
$coercedValues[$varName] = $coerced['value'];
}
}
}
}
}
return $errors === []
? [null, $coercedValues]
: [$errors, null];
}
/**
* Prepares an object map of argument values given a directive definition
* and an AST node which may contain directives. Optionally also accepts a map
* of variable values.
*
* If the directive does not exist on the node, returns undefined.
*
* @param EnumTypeDefinitionNode|EnumTypeExtensionNode|EnumValueDefinitionNode|FieldDefinitionNode|FieldNode|FragmentDefinitionNode|FragmentSpreadNode|InlineFragmentNode|InputObjectTypeDefinitionNode|InputObjectTypeExtensionNode|InputValueDefinitionNode|InterfaceTypeDefinitionNode|InterfaceTypeExtensionNode|ObjectTypeDefinitionNode|ObjectTypeExtensionNode|OperationDefinitionNode|ScalarTypeDefinitionNode|ScalarTypeExtensionNode|SchemaExtensionNode|UnionTypeDefinitionNode|UnionTypeExtensionNode|VariableDefinitionNode $node
* @param array<string, mixed>|null $variableValues
*
* @throws \Exception
* @throws Error
*
* @return array<string, mixed>|null
*/
public static function getDirectiveValues(Directive $directiveDef, Node $node, ?array $variableValues = null): ?array
{
$directiveDefName = $directiveDef->name;
foreach ($node->directives as $directive) {
if ($directive->name->value === $directiveDefName) {
return self::getArgumentValues($directiveDef, $directive, $variableValues);
}
}
return null;
}
/**
* Prepares an object map of argument values given a list of argument
* definitions and list of argument AST nodes.
*
* @param FieldDefinition|Directive $def
* @param FieldNode|DirectiveNode $node
* @param array<string, mixed>|null $variableValues
*
* @throws \Exception
* @throws Error
*
* @return array<string, mixed>
*/
public static function getArgumentValues($def, Node $node, ?array $variableValues = null): array
{
if ($def->args === []) {
return [];
}
/** @var array<string, ArgumentNodeValue> $argumentValueMap */
$argumentValueMap = [];
// Might not be defined when an AST from JS is used
if (isset($node->arguments)) {
foreach ($node->arguments as $argumentNode) {
$argumentValueMap[$argumentNode->name->value] = $argumentNode->value;
}
}
return static::getArgumentValuesForMap($def, $argumentValueMap, $variableValues, $node);
}
/**
* @param FieldDefinition|Directive $def
* @param array<string, ArgumentNodeValue> $argumentValueMap
* @param array<string, mixed>|null $variableValues
*
* @throws \Exception
* @throws Error
*
* @return array<string, mixed>
*/
public static function getArgumentValuesForMap($def, array $argumentValueMap, ?array $variableValues = null, ?Node $referenceNode = null): array
{
/** @var array<string, mixed> $coercedValues */
$coercedValues = [];
foreach ($def->args as $argumentDefinition) {
$name = $argumentDefinition->name;
$argType = $argumentDefinition->getType();
$argumentValueNode = $argumentValueMap[$name] ?? null;
if ($argumentValueNode instanceof VariableNode) {
$variableName = $argumentValueNode->name->value;
$hasValue = $variableValues !== null && \array_key_exists($variableName, $variableValues);
$isNull = $hasValue && $variableValues[$variableName] === null;
} else {
$hasValue = $argumentValueNode !== null;
$isNull = $argumentValueNode instanceof NullValueNode;
}
if (! $hasValue && $argumentDefinition->defaultValueExists()) {
// If no argument was provided where the definition has a default value,
// use the default value.
$coercedValues[$name] = $argumentDefinition->defaultValue;
} elseif ((! $hasValue || $isNull) && ($argType instanceof NonNull)) {
// If no argument or a null value was provided to an argument with a
// non-null type (required), produce a field error.
$safeArgType = Utils::printSafe($argType);
if ($isNull) {
throw new Error("Argument \"{$name}\" of non-null type \"{$safeArgType}\" must not be null.", $referenceNode);
}
if ($argumentValueNode instanceof VariableNode) {
throw new Error("Argument \"{$name}\" of required type \"{$safeArgType}\" was provided the variable \"\${$argumentValueNode->name->value}\" which was not provided a runtime value.", [$argumentValueNode]);
}
throw new Error("Argument \"{$name}\" of required type \"{$safeArgType}\" was not provided.", $referenceNode);
} elseif ($hasValue) {
assert($argumentValueNode instanceof Node);
if ($argumentValueNode instanceof NullValueNode) {
// If the explicit value `null` was provided, an entry in the coerced
// values must exist as the value `null`.
$coercedValues[$name] = null;
} elseif ($argumentValueNode instanceof VariableNode) {
$variableName = $argumentValueNode->name->value;
// Note: This does no further checking that this variable is correct.
// This assumes that this query has been validated and the variable
// usage here is of the correct type.
$coercedValues[$name] = $variableValues[$variableName] ?? null;
} else {
$coercedValue = AST::valueFromAST($argumentValueNode, $argType, $variableValues);
if (Utils::undefined() === $coercedValue) {
// Note: ValuesOfCorrectType validation should catch this before
// execution. This is a runtime check to ensure execution does not
// continue with an invalid argument value.
$invalidValue = Printer::doPrint($argumentValueNode);
throw new Error("Argument \"{$name}\" has invalid value {$invalidValue}.", [$argumentValueNode]);
}
$coercedValues[$name] = $coercedValue;
}
}
}
return $coercedValues;
}
}