Automattic\WooCommerce\Vendor\GraphQL\Executor
Values::getVariableValues
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.
Method of the class: Values{}
No Hooks.
Returns
Array{Array
Usage
$result = Values::getVariableValues( $schema, $varDefNodes, $rawVariableValues ): array;
- $schema(Schema) (required)
- .
- $varDefNodes(NodeList
) (required) - .
- $rawVariableValues(array) (required)
- .
Values::getVariableValues() Values::getVariableValues code WC 10.9.4
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, null, $schema);
$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];
}