graphql-php/src/Executor/Values.php

342 lines
13 KiB
PHP
Raw Normal View History

2015-07-15 20:05:46 +03:00
<?php
namespace GraphQL\Executor;
use GraphQL\Error\Error;
use GraphQL\Error\InvariantViolation;
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;
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;
use GraphQL\Type\Definition\InputType;
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.
*
* @param Schema $schema
* @param VariableDefinitionNode[] $definitionNodes
* @param array $inputs
* @return array
* @throws Error
2015-07-15 20:05:46 +03:00
*/
public static function getVariableValues(Schema $schema, $definitionNodes, array $inputs)
2015-07-15 20:05:46 +03:00
{
2016-11-19 00:15:40 +03:00
$coercedValues = [];
foreach ($definitionNodes as $definitionNode) {
$varName = $definitionNode->variable->name->value;
$varType = Utils\TypeInfo::typeFromAST($schema, $definitionNode->type);
2016-11-19 00:15:40 +03:00
if (!Type::isInputType($varType)) {
throw new Error(
'Variable "$'.$varName.'" expected value of type ' .
'"' . Printer::doPrint($definitionNode->type) . '" which cannot be used as an input type.',
[$definitionNode->type]
2016-11-19 00:15:40 +03:00
);
}
if (!array_key_exists($varName, $inputs)) {
$defaultValue = $definitionNode->defaultValue;
2016-11-19 00:15:40 +03:00
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.',
[$definitionNode]
2016-11-19 00:15:40 +03:00
);
}
} 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,
[$definitionNode]
2016-11-19 00:15:40 +03:00
);
}
$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.
*
2016-11-19 00:15:40 +03:00
* @param FieldDefinition|Directive $def
* @param FieldNode|\GraphQL\Language\AST\DirectiveNode $node
* @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;
$argNodes = $node->arguments;
2016-11-19 00:15:40 +03:00
if (!$argDefs || null === $argNodes) {
return [];
2015-07-15 20:05:46 +03:00
}
2016-11-19 00:15:40 +03:00
$coercedValues = [];
$undefined = Utils::undefined();
/** @var ArgumentNode[] $argNodeMap */
$argNodeMap = $argNodes ? Utils::keyMap($argNodes, 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();
$argumentNode = isset($argNodeMap[$name]) ? $argNodeMap[$name] : null;
if (!$argumentNode) {
2016-11-19 00:15:40 +03:00
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]
);
}
} else if ($argumentNode->value instanceof VariableNode) {
$variableName = $argumentNode->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.',
[ $argumentNode->value ]
2016-11-19 00:15:40 +03:00
);
}
} else {
$valueNode = $argumentNode->value;
$coercedValue = Utils\AST::valueFromAST($valueNode, $argType, $variableValues);
2016-11-19 00:15:40 +03:00
if ($coercedValue === $undefined) {
$errors = DocumentValidator::isValidLiteralValue($argType, $valueNode);
2016-11-19 00:15:40 +03:00
$message = !empty($errors) ? ("\n" . implode("\n", $errors)) : '';
throw new Error(
'Argument "' . $name . '" got invalid value ' . Printer::doPrint($valueNode) . '.' . $message,
[ $argumentNode->value ]
2016-11-19 00:15:40 +03:00
);
}
$coercedValues[$name] = $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
}
2016-11-18 19:59:28 +03:00
/**
* @deprecated as of 8.0 (Moved to Utils\AST::valueFromAST)
2016-11-18 19:59:28 +03:00
*
* @param $valueNode
2016-11-18 19:59:28 +03:00
* @param InputType $type
* @param null $variables
* @return array|null|\stdClass
*/
public static function valueFromAST($valueNode, InputType $type, $variables = null)
2015-07-15 20:05:46 +03:00
{
return Utils\AST::valueFromAST($valueNode, $type, $variables);
2015-07-15 20:05:46 +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)) {
$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-07-15 20:05:46 +03:00
$fields = $type->getFields();
2016-05-01 00:02:04 +03:00
$errors = [];
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) {
$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}";
})
);
}
2016-05-01 00:02:04 +03:00
return $errors;
}
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
}
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();
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;
}
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
}
}