2016-05-01 00:02:04 +03:00
|
|
|
<?php
|
|
|
|
namespace GraphQL\Utils;
|
|
|
|
|
2016-11-01 20:11:33 +03:00
|
|
|
use GraphQL\Error\InvariantViolation;
|
2016-11-19 02:12:18 +03:00
|
|
|
use GraphQL\Language\AST\BooleanValueNode;
|
|
|
|
use GraphQL\Language\AST\EnumValueNode;
|
|
|
|
use GraphQL\Language\AST\FieldNode;
|
|
|
|
use GraphQL\Language\AST\FloatValueNode;
|
|
|
|
use GraphQL\Language\AST\IntValueNode;
|
|
|
|
use GraphQL\Language\AST\ListValueNode;
|
|
|
|
use GraphQL\Language\AST\NameNode;
|
|
|
|
use GraphQL\Language\AST\NullValueNode;
|
|
|
|
use GraphQL\Language\AST\ObjectFieldNode;
|
|
|
|
use GraphQL\Language\AST\ObjectValueNode;
|
|
|
|
use GraphQL\Language\AST\StringValueNode;
|
|
|
|
use GraphQL\Language\AST\ValueNode;
|
|
|
|
use GraphQL\Language\AST\VariableNode;
|
2016-05-01 00:02:04 +03:00
|
|
|
use GraphQL\Type\Definition\EnumType;
|
2016-11-01 20:11:33 +03:00
|
|
|
use GraphQL\Type\Definition\IDType;
|
2016-05-01 00:02:04 +03:00
|
|
|
use GraphQL\Type\Definition\InputObjectType;
|
|
|
|
use GraphQL\Type\Definition\InputType;
|
2016-11-01 20:11:33 +03:00
|
|
|
use GraphQL\Type\Definition\LeafType;
|
2016-05-01 00:02:04 +03:00
|
|
|
use GraphQL\Type\Definition\ListOfType;
|
|
|
|
use GraphQL\Type\Definition\NonNull;
|
|
|
|
use GraphQL\Utils;
|
|
|
|
|
2016-10-17 14:33:47 +03:00
|
|
|
/**
|
|
|
|
* Class AST
|
|
|
|
* @package GraphQL\Utils
|
|
|
|
*/
|
2016-05-01 00:02:04 +03:00
|
|
|
class AST
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Produces a GraphQL Value AST given a PHP value.
|
|
|
|
*
|
|
|
|
* Optionally, a GraphQL type may be provided, which will be used to
|
|
|
|
* disambiguate between value primitives.
|
|
|
|
*
|
2016-11-01 20:11:33 +03:00
|
|
|
* | PHP Value | GraphQL Value |
|
2016-05-01 00:02:04 +03:00
|
|
|
* | ------------- | -------------------- |
|
|
|
|
* | Object | Input Object |
|
|
|
|
* | Assoc Array | Input Object |
|
|
|
|
* | Array | List |
|
|
|
|
* | Boolean | Boolean |
|
|
|
|
* | String | String / Enum Value |
|
|
|
|
* | Int | Int |
|
2016-11-01 20:11:33 +03:00
|
|
|
* | Float | Int / Float |
|
|
|
|
* | Mixed | Enum Value |
|
2016-11-18 19:59:28 +03:00
|
|
|
* | null | NullValue |
|
2016-11-01 20:11:33 +03:00
|
|
|
*
|
|
|
|
* @param $value
|
|
|
|
* @param InputType $type
|
2016-11-19 02:12:18 +03:00
|
|
|
* @return ObjectValueNode|ListValueNode|BooleanValueNode|IntValueNode|FloatValueNode|EnumValueNode|StringValueNode|NullValueNode
|
2016-05-01 00:02:04 +03:00
|
|
|
*/
|
2016-11-01 20:11:33 +03:00
|
|
|
static function astFromValue($value, InputType $type)
|
2016-05-01 00:02:04 +03:00
|
|
|
{
|
|
|
|
if ($type instanceof NonNull) {
|
2016-11-18 19:59:28 +03:00
|
|
|
$astValue = self::astFromValue($value, $type->getWrappedType());
|
2016-11-19 02:12:18 +03:00
|
|
|
if ($astValue instanceof NullValueNode) {
|
2016-11-18 19:59:28 +03:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return $astValue;
|
2016-05-01 00:02:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($value === null) {
|
2016-11-19 02:12:18 +03:00
|
|
|
return new NullValueNode([]);
|
2016-05-01 00:02:04 +03:00
|
|
|
}
|
|
|
|
|
2016-11-01 20:11:33 +03:00
|
|
|
// Convert PHP array to GraphQL list. If the GraphQLType is a list, but
|
|
|
|
// the value is not an array, convert the value using the list's item type.
|
|
|
|
if ($type instanceof ListOfType) {
|
|
|
|
$itemType = $type->getWrappedType();
|
|
|
|
if (is_array($value) || ($value instanceof \Traversable)) {
|
2016-11-19 02:47:55 +03:00
|
|
|
$valuesNodes = [];
|
2016-11-01 20:11:33 +03:00
|
|
|
foreach ($value as $item) {
|
2016-11-19 02:47:55 +03:00
|
|
|
$itemNode = self::astFromValue($item, $itemType);
|
|
|
|
if ($itemNode) {
|
|
|
|
$valuesNodes[] = $itemNode;
|
2016-11-01 20:11:33 +03:00
|
|
|
}
|
|
|
|
}
|
2016-11-19 02:47:55 +03:00
|
|
|
return new ListValueNode(['values' => $valuesNodes]);
|
2016-05-01 00:02:04 +03:00
|
|
|
}
|
2016-11-01 20:11:33 +03:00
|
|
|
return self::astFromValue($value, $itemType);
|
2016-05-01 00:02:04 +03:00
|
|
|
}
|
|
|
|
|
2016-11-01 20:11:33 +03:00
|
|
|
// Populate the fields of the input object by creating ASTs from each value
|
|
|
|
// in the PHP object according to the fields in the input type.
|
|
|
|
if ($type instanceof InputObjectType) {
|
2016-11-18 19:59:28 +03:00
|
|
|
$isArray = is_array($value);
|
|
|
|
$isArrayLike = $isArray || $value instanceof \ArrayAccess;
|
2016-11-01 20:11:33 +03:00
|
|
|
if ($value === null || (!$isArrayLike && !is_object($value))) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$fields = $type->getFields();
|
2016-11-19 02:47:55 +03:00
|
|
|
$fieldNodes = [];
|
2016-11-01 20:11:33 +03:00
|
|
|
foreach ($fields as $fieldName => $field) {
|
|
|
|
if ($isArrayLike) {
|
|
|
|
$fieldValue = isset($value[$fieldName]) ? $value[$fieldName] : null;
|
|
|
|
} else {
|
|
|
|
$fieldValue = isset($value->{$fieldName}) ? $value->{$fieldName} : null;
|
|
|
|
}
|
|
|
|
|
2016-11-18 19:59:28 +03:00
|
|
|
// Have to check additionally if key exists, since we differentiate between
|
|
|
|
// "no key" and "value is null":
|
|
|
|
if (null !== $fieldValue) {
|
|
|
|
$fieldExists = true;
|
|
|
|
} else if ($isArray) {
|
|
|
|
$fieldExists = array_key_exists($fieldName, $value);
|
|
|
|
} else if ($isArrayLike) {
|
|
|
|
/** @var \ArrayAccess $value */
|
|
|
|
$fieldExists = $value->offsetExists($fieldName);
|
|
|
|
} else {
|
|
|
|
$fieldExists = property_exists($value, $fieldName);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($fieldExists) {
|
|
|
|
$fieldNode = self::astFromValue($fieldValue, $field->getType());
|
|
|
|
|
|
|
|
if ($fieldNode) {
|
2016-11-19 02:47:55 +03:00
|
|
|
$fieldNodes[] = new ObjectFieldNode([
|
2016-11-19 02:12:18 +03:00
|
|
|
'name' => new NameNode(['value' => $fieldName]),
|
2016-11-18 19:59:28 +03:00
|
|
|
'value' => $fieldNode
|
|
|
|
]);
|
|
|
|
}
|
2016-11-01 20:11:33 +03:00
|
|
|
}
|
|
|
|
}
|
2016-11-19 02:47:55 +03:00
|
|
|
return new ObjectValueNode(['fields' => $fieldNodes]);
|
2016-05-01 00:02:04 +03:00
|
|
|
}
|
|
|
|
|
2016-11-01 20:11:33 +03:00
|
|
|
// Since value is an internally represented value, it must be serialized
|
|
|
|
// to an externally represented value before converting into an AST.
|
|
|
|
if ($type instanceof LeafType) {
|
|
|
|
$serialized = $type->serialize($value);
|
|
|
|
} else {
|
|
|
|
throw new InvariantViolation("Must provide Input Type, cannot use: " . Utils::printSafe($type));
|
2016-05-01 00:02:04 +03:00
|
|
|
}
|
|
|
|
|
2016-11-01 20:11:33 +03:00
|
|
|
if (null === $serialized) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Others serialize based on their corresponding PHP scalar types.
|
|
|
|
if (is_bool($serialized)) {
|
2016-11-19 02:12:18 +03:00
|
|
|
return new BooleanValueNode(['value' => $serialized]);
|
2016-11-01 20:11:33 +03:00
|
|
|
}
|
|
|
|
if (is_int($serialized)) {
|
2016-11-19 02:12:18 +03:00
|
|
|
return new IntValueNode(['value' => $serialized]);
|
2016-11-01 20:11:33 +03:00
|
|
|
}
|
|
|
|
if (is_float($serialized)) {
|
|
|
|
if ((int) $serialized == $serialized) {
|
2016-11-19 02:12:18 +03:00
|
|
|
return new IntValueNode(['value' => $serialized]);
|
2016-05-01 00:02:04 +03:00
|
|
|
}
|
2016-11-19 02:12:18 +03:00
|
|
|
return new FloatValueNode(['value' => $serialized]);
|
2016-05-01 00:02:04 +03:00
|
|
|
}
|
2016-11-01 20:11:33 +03:00
|
|
|
if (is_string($serialized)) {
|
|
|
|
// Enum types use Enum literals.
|
|
|
|
if ($type instanceof EnumType) {
|
2016-11-19 02:12:18 +03:00
|
|
|
return new EnumValueNode(['value' => $serialized]);
|
2016-11-01 20:11:33 +03:00
|
|
|
}
|
2016-05-01 00:02:04 +03:00
|
|
|
|
2016-11-01 20:11:33 +03:00
|
|
|
// ID types can use Int literals.
|
|
|
|
$asInt = (int) $serialized;
|
|
|
|
if ($type instanceof IDType && (string) $asInt === $serialized) {
|
2016-11-19 02:12:18 +03:00
|
|
|
return new IntValueNode(['value' => $serialized]);
|
2016-05-01 00:02:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Use json_encode, which uses the same string encoding as GraphQL,
|
|
|
|
// then remove the quotes.
|
2016-11-19 02:12:18 +03:00
|
|
|
return new StringValueNode([
|
2016-11-01 20:11:33 +03:00
|
|
|
'value' => substr(json_encode($serialized), 1, -1)
|
2016-05-01 00:02:04 +03:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2016-11-01 20:11:33 +03:00
|
|
|
throw new InvariantViolation('Cannot convert value to AST: ' . Utils::printSafe($serialized));
|
2016-05-01 00:02:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-11-01 20:11:33 +03:00
|
|
|
* Produces a PHP value given a GraphQL Value AST.
|
|
|
|
*
|
|
|
|
* A GraphQL type must be provided, which will be used to interpret different
|
|
|
|
* GraphQL Value literals.
|
|
|
|
*
|
2016-11-19 00:15:40 +03:00
|
|
|
* Returns `null` when the value could not be validly coerced according to
|
|
|
|
* the provided type.
|
|
|
|
*
|
2016-11-01 20:11:33 +03:00
|
|
|
* | GraphQL Value | PHP Value |
|
|
|
|
* | -------------------- | ------------- |
|
|
|
|
* | Input Object | Assoc Array |
|
|
|
|
* | List | Array |
|
|
|
|
* | Boolean | Boolean |
|
|
|
|
* | String | String |
|
|
|
|
* | Int / Float | Int / Float |
|
|
|
|
* | Enum Value | Mixed |
|
2016-11-19 00:15:40 +03:00
|
|
|
* | Null Value | stdClass | instance of NullValue::getNullValue()
|
2016-11-01 20:11:33 +03:00
|
|
|
*
|
2016-11-19 02:47:55 +03:00
|
|
|
* @param $valueNode
|
2016-05-01 00:02:04 +03:00
|
|
|
* @param InputType $type
|
|
|
|
* @param null $variables
|
2016-11-18 19:59:28 +03:00
|
|
|
* @return array|null|\stdClass
|
2016-05-01 00:02:04 +03:00
|
|
|
* @throws \Exception
|
|
|
|
*/
|
2016-11-19 02:47:55 +03:00
|
|
|
public static function valueFromAST($valueNode, InputType $type, $variables = null)
|
2016-05-01 00:02:04 +03:00
|
|
|
{
|
2016-11-19 00:15:40 +03:00
|
|
|
$undefined = Utils::undefined();
|
2016-05-01 00:02:04 +03:00
|
|
|
|
2016-11-19 02:47:55 +03:00
|
|
|
if (!$valueNode) {
|
2016-11-18 19:59:28 +03:00
|
|
|
// When there is no AST, then there is also no value.
|
|
|
|
// Importantly, this is different from returning the GraphQL null value.
|
2016-11-19 00:15:40 +03:00
|
|
|
return $undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($type instanceof NonNull) {
|
2016-11-19 02:47:55 +03:00
|
|
|
if ($valueNode instanceof NullValueNode) {
|
2016-11-19 00:15:40 +03:00
|
|
|
// Invalid: intentionally return no value.
|
|
|
|
return $undefined;
|
|
|
|
}
|
2016-11-19 02:47:55 +03:00
|
|
|
return self::valueFromAST($valueNode, $type->getWrappedType(), $variables);
|
2016-11-18 19:59:28 +03:00
|
|
|
}
|
|
|
|
|
2016-11-19 02:47:55 +03:00
|
|
|
if ($valueNode instanceof NullValueNode) {
|
2016-11-18 19:59:28 +03:00
|
|
|
// This is explicitly returning the value null.
|
2016-11-19 00:15:40 +03:00
|
|
|
return null;
|
2016-05-01 00:02:04 +03:00
|
|
|
}
|
|
|
|
|
2016-11-19 02:47:55 +03:00
|
|
|
if ($valueNode instanceof VariableNode) {
|
|
|
|
$variableName = $valueNode->name->value;
|
2016-05-01 00:02:04 +03:00
|
|
|
|
2016-11-19 00:15:40 +03:00
|
|
|
if (!$variables || !array_key_exists($variableName, $variables)) {
|
2016-11-18 19:59:28 +03:00
|
|
|
// No valid return value.
|
2016-11-19 00:15:40 +03:00
|
|
|
return $undefined;
|
2016-05-01 00:02:04 +03:00
|
|
|
}
|
2016-11-01 20:11:33 +03:00
|
|
|
// Note: we're not doing any checking that this variable is correct. We're
|
|
|
|
// assuming that this query has been validated and the variable usage here
|
|
|
|
// is of the correct type.
|
2016-05-01 00:02:04 +03:00
|
|
|
return $variables[$variableName];
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($type instanceof ListOfType) {
|
|
|
|
$itemType = $type->getWrappedType();
|
2016-11-19 00:15:40 +03:00
|
|
|
|
2016-11-19 02:47:55 +03:00
|
|
|
if ($valueNode instanceof ListValueNode) {
|
2016-11-19 00:15:40 +03:00
|
|
|
$coercedValues = [];
|
2016-11-19 02:47:55 +03:00
|
|
|
$itemNodes = $valueNode->values;
|
|
|
|
foreach ($itemNodes as $itemNode) {
|
|
|
|
if (self::isMissingVariable($itemNode, $variables)) {
|
2016-11-19 00:15:40 +03:00
|
|
|
// If an array contains a missing variable, it is either coerced to
|
|
|
|
// null or if the item type is non-null, it considered invalid.
|
|
|
|
if ($itemType instanceof NonNull) {
|
|
|
|
// Invalid: intentionally return no value.
|
|
|
|
return $undefined;
|
|
|
|
}
|
|
|
|
$coercedValues[] = null;
|
|
|
|
} else {
|
2016-11-19 02:47:55 +03:00
|
|
|
$itemValue = self::valueFromAST($itemNode, $itemType, $variables);
|
2016-11-19 00:15:40 +03:00
|
|
|
if ($undefined === $itemValue) {
|
|
|
|
// Invalid: intentionally return no value.
|
|
|
|
return $undefined;
|
|
|
|
}
|
|
|
|
$coercedValues[] = $itemValue;
|
|
|
|
}
|
2016-11-18 19:59:28 +03:00
|
|
|
}
|
2016-11-19 00:15:40 +03:00
|
|
|
return $coercedValues;
|
2016-05-01 00:02:04 +03:00
|
|
|
}
|
2016-11-19 02:47:55 +03:00
|
|
|
$coercedValue = self::valueFromAST($valueNode, $itemType, $variables);
|
2016-11-19 00:15:40 +03:00
|
|
|
if ($undefined === $coercedValue) {
|
|
|
|
// Invalid: intentionally return no value.
|
|
|
|
return $undefined;
|
|
|
|
}
|
|
|
|
return [$coercedValue];
|
2016-05-01 00:02:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($type instanceof InputObjectType) {
|
2016-11-19 02:47:55 +03:00
|
|
|
if (!$valueNode instanceof ObjectValueNode) {
|
2016-11-19 00:15:40 +03:00
|
|
|
// Invalid: intentionally return no value.
|
|
|
|
return $undefined;
|
2016-05-01 00:02:04 +03:00
|
|
|
}
|
2016-11-19 00:15:40 +03:00
|
|
|
|
|
|
|
$coercedObj = [];
|
|
|
|
$fields = $type->getFields();
|
2016-11-19 02:47:55 +03:00
|
|
|
$fieldNodes = Utils::keyMap($valueNode->fields, function($field) {return $field->name->value;});
|
2016-05-01 00:02:04 +03:00
|
|
|
foreach ($fields as $field) {
|
2016-11-19 02:47:55 +03:00
|
|
|
/** @var ValueNode $fieldNode */
|
2016-11-19 00:15:40 +03:00
|
|
|
$fieldName = $field->name;
|
2016-11-19 02:47:55 +03:00
|
|
|
$fieldNode = isset($fieldNodes[$fieldName]) ? $fieldNodes[$fieldName] : null;
|
2016-05-01 00:02:04 +03:00
|
|
|
|
2016-11-19 02:47:55 +03:00
|
|
|
if (!$fieldNode || self::isMissingVariable($fieldNode->value, $variables)) {
|
2016-11-19 00:15:40 +03:00
|
|
|
if ($field->defaultValueExists()) {
|
|
|
|
$coercedObj[$fieldName] = $field->defaultValue;
|
|
|
|
} else if ($field->getType() instanceof NonNull) {
|
|
|
|
// Invalid: intentionally return no value.
|
|
|
|
return $undefined;
|
|
|
|
}
|
|
|
|
continue ;
|
2016-05-01 00:02:04 +03:00
|
|
|
}
|
2016-11-18 19:59:28 +03:00
|
|
|
|
2016-11-19 02:47:55 +03:00
|
|
|
$fieldValue = self::valueFromAST($fieldNode ? $fieldNode->value : null, $field->getType(), $variables);
|
2016-11-18 19:59:28 +03:00
|
|
|
|
2016-11-19 00:15:40 +03:00
|
|
|
if ($undefined === $fieldValue) {
|
|
|
|
// Invalid: intentionally return no value.
|
|
|
|
return $undefined;
|
|
|
|
}
|
|
|
|
$coercedObj[$fieldName] = $fieldValue;
|
2016-05-01 00:02:04 +03:00
|
|
|
}
|
2016-11-19 00:15:40 +03:00
|
|
|
return $coercedObj;
|
2016-05-01 00:02:04 +03:00
|
|
|
}
|
|
|
|
|
2016-11-01 20:11:33 +03:00
|
|
|
if ($type instanceof LeafType) {
|
2016-11-19 02:47:55 +03:00
|
|
|
$parsed = $type->parseLiteral($valueNode);
|
2016-11-19 00:15:40 +03:00
|
|
|
|
2017-07-04 16:19:52 +03:00
|
|
|
if (null === $parsed && !$type->isValidLiteral($valueNode)) {
|
|
|
|
// Invalid values represent a failure to parse correctly, in which case
|
|
|
|
// no value is returned.
|
2016-11-19 00:15:40 +03:00
|
|
|
return $undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $parsed;
|
2016-11-01 20:11:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
throw new InvariantViolation('Must be input type');
|
2016-05-01 00:02:04 +03:00
|
|
|
}
|
2016-11-19 00:15:40 +03:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
2016-11-19 02:47:55 +03:00
|
|
|
* Returns true if the provided valueNode is a variable which is not defined
|
2016-11-19 00:15:40 +03:00
|
|
|
* in the set of variables.
|
2016-11-19 02:47:55 +03:00
|
|
|
* @param $valueNode
|
2016-11-19 00:15:40 +03:00
|
|
|
* @param $variables
|
|
|
|
* @return bool
|
|
|
|
*/
|
2016-11-19 02:47:55 +03:00
|
|
|
private static function isMissingVariable($valueNode, $variables)
|
2016-11-19 00:15:40 +03:00
|
|
|
{
|
2016-11-19 02:47:55 +03:00
|
|
|
return $valueNode instanceof VariableNode &&
|
|
|
|
(!$variables || !array_key_exists($valueNode->name->value, $variables));
|
2016-11-19 00:15:40 +03:00
|
|
|
}
|
2016-05-01 00:02:04 +03:00
|
|
|
}
|