2015-07-15 20:05:46 +03:00
|
|
|
<?php
|
|
|
|
namespace GraphQL\Executor;
|
|
|
|
|
|
|
|
|
2016-10-21 12:39:57 +03:00
|
|
|
use GraphQL\Error\Error;
|
2016-11-01 19:40:37 +03:00
|
|
|
use GraphQL\Error\InvariantViolation;
|
2016-11-19 02:12:18 +03:00
|
|
|
use GraphQL\Language\AST\ArgumentNode;
|
|
|
|
use GraphQL\Language\AST\FieldNode;
|
|
|
|
use GraphQL\Language\AST\NullValueNode;
|
|
|
|
use GraphQL\Language\AST\ValueNode;
|
|
|
|
use GraphQL\Language\AST\VariableNode;
|
|
|
|
use GraphQL\Language\AST\VariableDefinitionNode;
|
2015-07-15 20:05:46 +03:00
|
|
|
use GraphQL\Language\Printer;
|
|
|
|
use GraphQL\Schema;
|
2016-11-19 00:15:40 +03:00
|
|
|
use GraphQL\Type\Definition\Directive;
|
2015-08-17 17:01:55 +03:00
|
|
|
use GraphQL\Type\Definition\FieldArgument;
|
2016-11-19 00:15:40 +03:00
|
|
|
use GraphQL\Type\Definition\FieldDefinition;
|
2015-07-15 20:05:46 +03:00
|
|
|
use GraphQL\Type\Definition\InputObjectType;
|
2015-08-17 17:01:55 +03:00
|
|
|
use GraphQL\Type\Definition\InputType;
|
2016-11-01 19:40:37 +03:00
|
|
|
use GraphQL\Type\Definition\LeafType;
|
2015-07-15 20:05:46 +03:00
|
|
|
use GraphQL\Type\Definition\ListOfType;
|
|
|
|
use GraphQL\Type\Definition\NonNull;
|
|
|
|
use GraphQL\Type\Definition\Type;
|
|
|
|
use GraphQL\Utils;
|
2016-11-19 00:15:40 +03:00
|
|
|
use GraphQL\Validator\DocumentValidator;
|
2015-07-15 20:05:46 +03:00
|
|
|
|
|
|
|
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, a Error will be thrown.
|
2015-08-17 17:01:55 +03:00
|
|
|
*
|
|
|
|
* @param Schema $schema
|
2016-11-19 02:12:18 +03:00
|
|
|
* @param VariableDefinitionNode[] $definitionASTs
|
2015-08-17 17:01:55 +03:00
|
|
|
* @param array $inputs
|
|
|
|
* @return array
|
|
|
|
* @throws Error
|
2015-07-15 20:05:46 +03:00
|
|
|
*/
|
2015-08-17 17:01:55 +03:00
|
|
|
public static function getVariableValues(Schema $schema, $definitionASTs, array $inputs)
|
2015-07-15 20:05:46 +03:00
|
|
|
{
|
2016-11-19 00:15:40 +03:00
|
|
|
$coercedValues = [];
|
|
|
|
foreach ($definitionASTs as $definitionAST) {
|
|
|
|
$varName = $definitionAST->variable->name->value;
|
|
|
|
$varType = Utils\TypeInfo::typeFromAST($schema, $definitionAST->type);
|
|
|
|
|
|
|
|
if (!Type::isInputType($varType)) {
|
|
|
|
throw new Error(
|
|
|
|
'Variable "$'.$varName.'" expected value of type ' .
|
|
|
|
'"' . Printer::doPrint($definitionAST->type) . '" which cannot be used as an input type.',
|
|
|
|
[$definitionAST->type]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!array_key_exists($varName, $inputs)) {
|
|
|
|
$defaultValue = $definitionAST->defaultValue;
|
|
|
|
if ($defaultValue) {
|
|
|
|
$coercedValues[$varName] = Utils\AST::valueFromAST($defaultValue, $varType);
|
|
|
|
}
|
|
|
|
if ($varType instanceof NonNull) {
|
|
|
|
throw new Error(
|
|
|
|
'Variable "$'.$varName .'" of required type ' .
|
|
|
|
'"'. Utils::printSafe($varType) . '" was not provided.',
|
|
|
|
[$definitionAST]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$value = $inputs[$varName];
|
|
|
|
$errors = self::isValidPHPValue($value, $varType);
|
|
|
|
if (!empty($errors)) {
|
|
|
|
$message = "\n" . implode("\n", $errors);
|
|
|
|
throw new Error(
|
|
|
|
'Variable "$' . $varName . '" got invalid value ' .
|
|
|
|
json_encode($value) . '.' . $message,
|
|
|
|
[$definitionAST]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$coercedValue = self::coerceValue($varType, $value);
|
|
|
|
Utils::invariant($coercedValue !== Utils::undefined(), 'Should have reported error.');
|
|
|
|
$coercedValues[$varName] = $coercedValue;
|
|
|
|
}
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|
2016-11-19 00:15:40 +03:00
|
|
|
return $coercedValues;
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Prepares an object map of argument values given a list of argument
|
|
|
|
* definitions and list of argument AST nodes.
|
2015-08-17 17:01:55 +03:00
|
|
|
*
|
2016-11-19 00:15:40 +03:00
|
|
|
* @param FieldDefinition|Directive $def
|
2016-11-19 02:12:18 +03:00
|
|
|
* @param FieldNode|\GraphQL\Language\AST\DirectiveNode $node
|
2015-08-17 17:01:55 +03:00
|
|
|
* @param $variableValues
|
|
|
|
* @return array
|
2016-11-19 00:15:40 +03:00
|
|
|
* @throws Error
|
2015-07-15 20:05:46 +03:00
|
|
|
*/
|
2016-11-19 00:15:40 +03:00
|
|
|
public static function getArgumentValues($def, $node, $variableValues)
|
2015-07-15 20:05:46 +03:00
|
|
|
{
|
2016-11-19 00:15:40 +03:00
|
|
|
$argDefs = $def->args;
|
|
|
|
$argASTs = $node->arguments;
|
|
|
|
|
|
|
|
if (!$argDefs || null === $argASTs) {
|
2015-08-17 17:01:55 +03:00
|
|
|
return [];
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|
2016-11-19 00:15:40 +03:00
|
|
|
|
|
|
|
$coercedValues = [];
|
|
|
|
$undefined = Utils::undefined();
|
|
|
|
|
2016-11-19 02:12:18 +03:00
|
|
|
/** @var ArgumentNode[] $argASTMap */
|
|
|
|
$argASTMap = $argASTs ? Utils::keyMap($argASTs, function (ArgumentNode $arg) {
|
2015-07-15 20:05:46 +03:00
|
|
|
return $arg->name->value;
|
|
|
|
}) : [];
|
2016-11-19 00:15:40 +03:00
|
|
|
|
2015-07-15 20:05:46 +03:00
|
|
|
foreach ($argDefs as $argDef) {
|
|
|
|
$name = $argDef->name;
|
2016-11-19 00:15:40 +03:00
|
|
|
$argType = $argDef->getType();
|
|
|
|
$argumentAST = isset($argASTMap[$name]) ? $argASTMap[$name] : null;
|
2015-08-17 17:01:55 +03:00
|
|
|
|
2016-11-19 00:15:40 +03:00
|
|
|
if (!$argumentAST) {
|
|
|
|
if ($argDef->defaultValueExists()) {
|
|
|
|
$coercedValues[$name] = $argDef->defaultValue;
|
|
|
|
} else if ($argType instanceof NonNull) {
|
|
|
|
throw new Error(
|
|
|
|
'Argument "' . $name . '" of required type ' .
|
|
|
|
'"' . Utils::printSafe($argType) . '" was not provided.',
|
|
|
|
[$node]
|
|
|
|
);
|
|
|
|
}
|
2016-11-19 02:12:18 +03:00
|
|
|
} else if ($argumentAST->value instanceof VariableNode) {
|
2016-11-19 00:15:40 +03:00
|
|
|
$variableName = $argumentAST->value->name->value;
|
2016-11-18 19:59:28 +03:00
|
|
|
|
2016-11-19 00:15:40 +03:00
|
|
|
if ($variableValues && array_key_exists($variableName, $variableValues)) {
|
|
|
|
// Note: this does not check that this variable value is correct.
|
|
|
|
// This assumes that this query has been validated and the variable
|
|
|
|
// usage here is of the correct type.
|
|
|
|
$coercedValues[$name] = $variableValues[$variableName];
|
|
|
|
} else if ($argDef->defaultValueExists()) {
|
|
|
|
$coercedValues[$name] = $argDef->defaultValue;
|
|
|
|
} else if ($argType instanceof NonNull) {
|
|
|
|
throw new Error(
|
|
|
|
'Argument "' . $name . '" of required type "' . Utils::printSafe($argType) . '" was ' .
|
|
|
|
'provided the variable "$' . $variableName . '" which was not provided ' .
|
|
|
|
'a runtime value.',
|
|
|
|
[ $argumentAST->value ]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$valueAST = $argumentAST->value;
|
|
|
|
$coercedValue = Utils\AST::valueFromAST($valueAST, $argType, $variableValues);
|
|
|
|
if ($coercedValue === $undefined) {
|
|
|
|
$errors = DocumentValidator::isValidLiteralValue($argType, $valueAST);
|
|
|
|
$message = !empty($errors) ? ("\n" . implode("\n", $errors)) : '';
|
|
|
|
throw new Error(
|
|
|
|
'Argument "' . $name . '" got invalid value ' . Printer::doPrint($valueAST) . '.' . $message,
|
|
|
|
[ $argumentAST->value ]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
$coercedValues[$name] = $coercedValue;
|
2015-08-17 17:01:55 +03:00
|
|
|
}
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|
2016-11-19 00:15:40 +03:00
|
|
|
return $coercedValues;
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|
|
|
|
|
2016-11-18 19:59:28 +03:00
|
|
|
/**
|
|
|
|
* @deprecated Moved to Utils\AST::valueFromAST
|
|
|
|
*
|
|
|
|
* @param $valueAST
|
|
|
|
* @param InputType $type
|
|
|
|
* @param null $variables
|
|
|
|
* @return array|null|\stdClass
|
|
|
|
*/
|
2015-08-17 17:01:55 +03:00
|
|
|
public static function valueFromAST($valueAST, InputType $type, $variables = null)
|
2015-07-15 20:05:46 +03:00
|
|
|
{
|
2016-05-01 00:02:04 +03:00
|
|
|
return Utils\AST::valueFromAST($valueAST, $type, $variables);
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-08-17 17:01:55 +03:00
|
|
|
* Given a PHP value and a GraphQL type, determine if the value will be
|
|
|
|
* accepted for that type. This is primarily useful for validating the
|
|
|
|
* runtime values of query variables.
|
|
|
|
*
|
|
|
|
* @param $value
|
2016-11-01 20:41:44 +03:00
|
|
|
* @param InputType $type
|
2016-05-01 00:02:04 +03:00
|
|
|
* @return array
|
2015-07-15 20:05:46 +03:00
|
|
|
*/
|
2016-05-01 00:02:04 +03:00
|
|
|
private static function isValidPHPValue($value, InputType $type)
|
2015-07-15 20:05:46 +03:00
|
|
|
{
|
2016-05-01 00:02:04 +03:00
|
|
|
// A value must be provided if the type is non-null.
|
2015-07-15 20:05:46 +03:00
|
|
|
if ($type instanceof NonNull) {
|
|
|
|
if (null === $value) {
|
2016-11-19 00:15:40 +03:00
|
|
|
return ['Expected "' . Utils::printSafe($type) . '", found null.'];
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|
2016-11-19 00:15:40 +03:00
|
|
|
return self::isValidPHPValue($value, $type->getWrappedType());
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|
|
|
|
|
2016-05-01 00:02:04 +03:00
|
|
|
if (null === $value) {
|
|
|
|
return [];
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|
|
|
|
|
2016-05-01 00:02:04 +03:00
|
|
|
// Lists accept a non-list value as a list of one.
|
2015-07-15 20:05:46 +03:00
|
|
|
if ($type instanceof ListOfType) {
|
|
|
|
$itemType = $type->getWrappedType();
|
|
|
|
if (is_array($value)) {
|
2016-05-02 00:42:05 +03:00
|
|
|
$tmp = [];
|
|
|
|
foreach ($value as $index => $item) {
|
|
|
|
$errors = self::isValidPHPValue($item, $itemType);
|
|
|
|
$tmp = array_merge($tmp, Utils::map($errors, function ($error) use ($index) {
|
|
|
|
return "In element #$index: $error";
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
return $tmp;
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|
2016-05-01 00:02:04 +03:00
|
|
|
return self::isValidPHPValue($value, $itemType);
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|
|
|
|
|
2016-05-01 00:02:04 +03:00
|
|
|
// Input objects check each defined field.
|
2015-07-15 20:05:46 +03:00
|
|
|
if ($type instanceof InputObjectType) {
|
2016-05-01 00:02:04 +03:00
|
|
|
if (!is_object($value) && !is_array($value)) {
|
|
|
|
return ["Expected \"{$type->name}\", found not an object."];
|
2015-08-17 17:01:55 +03:00
|
|
|
}
|
2015-07-15 20:05:46 +03:00
|
|
|
$fields = $type->getFields();
|
2016-05-01 00:02:04 +03:00
|
|
|
$errors = [];
|
2015-08-17 17:01:55 +03:00
|
|
|
|
2016-05-01 00:02:04 +03:00
|
|
|
// Ensure every provided field is defined.
|
|
|
|
$props = is_object($value) ? get_object_vars($value) : $value;
|
|
|
|
foreach ($props as $providedField => $tmp) {
|
|
|
|
if (!isset($fields[$providedField])) {
|
|
|
|
$errors[] = "In field \"{$providedField}\": Unknown field.";
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-01 00:02:04 +03:00
|
|
|
// Ensure every defined field is valid.
|
|
|
|
foreach ($fields as $fieldName => $tmp) {
|
2016-05-02 00:42:05 +03:00
|
|
|
$newErrors = self::isValidPHPValue(isset($value[$fieldName]) ? $value[$fieldName] : null, $fields[$fieldName]->getType());
|
2016-05-01 00:02:04 +03:00
|
|
|
$errors = array_merge(
|
|
|
|
$errors,
|
|
|
|
Utils::map($newErrors, function ($error) use ($fieldName) {
|
|
|
|
return "In field \"{$fieldName}\": {$error}";
|
|
|
|
})
|
|
|
|
);
|
2015-08-17 17:01:55 +03:00
|
|
|
}
|
2016-05-01 00:02:04 +03:00
|
|
|
return $errors;
|
|
|
|
}
|
|
|
|
|
2016-11-01 19:40:37 +03:00
|
|
|
if ($type instanceof LeafType) {
|
|
|
|
// Scalar/Enum input checks to ensure the type can parse the value to
|
|
|
|
// a non-null value.
|
|
|
|
$parseResult = $type->parseValue($value);
|
|
|
|
if (null === $parseResult) {
|
|
|
|
$v = json_encode($value);
|
|
|
|
return [
|
|
|
|
"Expected type \"{$type->name}\", found $v."
|
|
|
|
];
|
|
|
|
}
|
|
|
|
return [];
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|
|
|
|
|
2016-11-01 19:40:37 +03:00
|
|
|
throw new InvariantViolation('Must be input type');
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Given a type and any value, return a runtime value coerced to match the type.
|
|
|
|
*/
|
|
|
|
private static function coerceValue(Type $type, $value)
|
|
|
|
{
|
2016-11-19 00:15:40 +03:00
|
|
|
$undefined = Utils::undefined();
|
|
|
|
if ($value === $undefined) {
|
|
|
|
return $undefined;
|
|
|
|
}
|
|
|
|
|
2015-07-15 20:05:46 +03:00
|
|
|
if ($type instanceof NonNull) {
|
2016-11-19 00:15:40 +03:00
|
|
|
if ($value === null) {
|
|
|
|
// Intentionally return no value.
|
|
|
|
return $undefined;
|
|
|
|
}
|
2015-07-15 20:05:46 +03:00
|
|
|
return self::coerceValue($type->getWrappedType(), $value);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (null === $value) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($type instanceof ListOfType) {
|
|
|
|
$itemType = $type->getWrappedType();
|
2016-10-18 21:34:46 +03:00
|
|
|
if (is_array($value) || $value instanceof \Traversable) {
|
2016-11-19 00:15:40 +03:00
|
|
|
$coercedValues = [];
|
|
|
|
foreach ($value as $item) {
|
|
|
|
$itemValue = self::coerceValue($itemType, $item);
|
|
|
|
if ($undefined === $itemValue) {
|
|
|
|
// Intentionally return no value.
|
|
|
|
return $undefined;
|
|
|
|
}
|
|
|
|
$coercedValues[] = $itemValue;
|
|
|
|
}
|
|
|
|
return $coercedValues;
|
2015-07-15 20:05:46 +03:00
|
|
|
} else {
|
2016-11-19 00:15:40 +03:00
|
|
|
$coercedValue = self::coerceValue($itemType, $value);
|
|
|
|
if ($coercedValue === $undefined) {
|
|
|
|
// Intentionally return no value.
|
|
|
|
return $undefined;
|
|
|
|
}
|
|
|
|
return [$coercedValue];
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($type instanceof InputObjectType) {
|
2016-11-19 00:15:40 +03:00
|
|
|
$coercedObj = [];
|
2015-07-15 20:05:46 +03:00
|
|
|
$fields = $type->getFields();
|
|
|
|
foreach ($fields as $fieldName => $field) {
|
2016-11-19 00:15:40 +03:00
|
|
|
if (!array_key_exists($fieldName, $value)) {
|
|
|
|
if ($field->defaultValueExists()) {
|
|
|
|
$coercedObj[$fieldName] = $field->defaultValue;
|
|
|
|
} else if ($field->getType() instanceof NonNull) {
|
|
|
|
// Intentionally return no value.
|
|
|
|
return $undefined;
|
|
|
|
}
|
|
|
|
continue;
|
2015-08-17 17:01:55 +03:00
|
|
|
}
|
2016-11-19 00:15:40 +03:00
|
|
|
$fieldValue = self::coerceValue($field->getType(), $value[$fieldName]);
|
|
|
|
if ($fieldValue === $undefined) {
|
|
|
|
// Intentionally return no value.
|
|
|
|
return $undefined;
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|
2016-11-19 00:15:40 +03:00
|
|
|
$coercedObj[$fieldName] = $fieldValue;
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|
2016-11-19 00:15:40 +03:00
|
|
|
return $coercedObj;
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|
2016-11-01 20:41:44 +03:00
|
|
|
|
|
|
|
if ($type instanceof LeafType) {
|
2016-11-19 00:15:40 +03:00
|
|
|
$parsed = $type->parseValue($value);
|
|
|
|
if (null === $parsed) {
|
|
|
|
// null or invalid values represent a failure to parse correctly,
|
|
|
|
// in which case no value is returned.
|
|
|
|
return $undefined;
|
|
|
|
}
|
|
|
|
return $parsed;
|
2016-11-01 20:41:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
throw new InvariantViolation('Must be input type');
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|
|
|
|
}
|