graphql-php/src/Utils/AST.php

280 lines
10 KiB
PHP
Raw Normal View History

2016-05-01 00:02:04 +03:00
<?php
namespace GraphQL\Utils;
use GraphQL\Error\InvariantViolation;
2016-05-01 00:02:04 +03:00
use GraphQL\Language\AST\BooleanValue;
use GraphQL\Language\AST\EnumValue;
use GraphQL\Language\AST\FloatValue;
use GraphQL\Language\AST\IntValue;
use GraphQL\Language\AST\ListValue;
use GraphQL\Language\AST\Name;
2016-11-18 19:59:28 +03:00
use GraphQL\Language\AST\NullValue;
2016-05-01 00:02:04 +03:00
use GraphQL\Language\AST\ObjectField;
use GraphQL\Language\AST\ObjectValue;
use GraphQL\Language\AST\StringValue;
use GraphQL\Language\AST\Variable;
use GraphQL\Type\Definition\EnumType;
use GraphQL\Type\Definition\IDType;
2016-05-01 00:02:04 +03:00
use GraphQL\Type\Definition\InputObjectType;
use GraphQL\Type\Definition\InputType;
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;
/**
* 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.
*
* | 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 |
* | Float | Int / Float |
* | Mixed | Enum Value |
2016-11-18 19:59:28 +03:00
* | null | NullValue |
*
* @param $value
* @param InputType $type
2016-11-18 19:59:28 +03:00
* @return ObjectValue|ListValue|BooleanValue|IntValue|FloatValue|EnumValue|StringValue|NullValue
2016-05-01 00:02:04 +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());
if ($astValue instanceof NullValue) {
return null;
}
return $astValue;
2016-05-01 00:02:04 +03:00
}
if ($value === null) {
2016-11-18 19:59:28 +03:00
return new NullValue([]);
2016-05-01 00:02:04 +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)) {
$valuesASTs = [];
foreach ($value as $item) {
$itemAST = self::astFromValue($item, $itemType);
if ($itemAST) {
$valuesASTs[] = $itemAST;
}
}
return new ListValue(['values' => $valuesASTs]);
2016-05-01 00:02:04 +03:00
}
return self::astFromValue($value, $itemType);
2016-05-01 00:02:04 +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;
if ($value === null || (!$isArrayLike && !is_object($value))) {
return null;
}
$fields = $type->getFields();
$fieldASTs = [];
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) {
$fieldASTs[] = new ObjectField([
'name' => new Name(['value' => $fieldName]),
'value' => $fieldNode
]);
}
}
}
return new ObjectValue(['fields' => $fieldASTs]);
2016-05-01 00:02:04 +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
}
if (null === $serialized) {
return null;
}
// Others serialize based on their corresponding PHP scalar types.
if (is_bool($serialized)) {
return new BooleanValue(['value' => $serialized]);
}
if (is_int($serialized)) {
return new IntValue(['value' => $serialized]);
}
if (is_float($serialized)) {
if ((int) $serialized == $serialized) {
return new IntValue(['value' => $serialized]);
2016-05-01 00:02:04 +03:00
}
return new FloatValue(['value' => $serialized]);
2016-05-01 00:02:04 +03:00
}
if (is_string($serialized)) {
// Enum types use Enum literals.
if ($type instanceof EnumType) {
return new EnumValue(['value' => $serialized]);
}
2016-05-01 00:02:04 +03:00
// ID types can use Int literals.
$asInt = (int) $serialized;
if ($type instanceof IDType && (string) $asInt === $serialized) {
return new IntValue(['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.
return new StringValue([
'value' => substr(json_encode($serialized), 1, -1)
2016-05-01 00:02:04 +03:00
]);
}
throw new InvariantViolation('Cannot convert value to AST: ' . Utils::printSafe($serialized));
2016-05-01 00:02:04 +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.
*
* | GraphQL Value | PHP Value |
* | -------------------- | ------------- |
* | Input Object | Assoc Array |
* | List | Array |
* | Boolean | Boolean |
* | String | String |
* | Int / Float | Int / Float |
* | Enum Value | Mixed |
2016-11-18 19:59:28 +03:00
* | Null Value | null |
*
2016-05-01 00:02:04 +03:00
* @param $valueAST
* @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
*/
public static function valueFromAST($valueAST, InputType $type, $variables = null)
{
if ($type instanceof NonNull) {
// Note: we're not checking that the result of valueFromAST is non-null.
// We're assuming that this query has been validated and the value used
// here is of the correct type.
2016-05-01 00:02:04 +03:00
return self::valueFromAST($valueAST, $type->getWrappedType(), $variables);
}
if (!$valueAST) {
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.
return ;
}
if ($valueAST instanceof NullValue) {
// This is explicitly returning the value null.
return NullValue::getNullValue();
2016-05-01 00:02:04 +03:00
}
if ($valueAST instanceof Variable) {
$variableName = $valueAST->name->value;
if (!$variables || !isset($variables[$variableName])) {
2016-11-18 19:59:28 +03:00
// No valid return value.
return ;
2016-05-01 00:02:04 +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-18 19:59:28 +03:00
$items = $valueAST instanceof ListValue ? $valueAST->values : [$valueAST];
$result = [];
foreach ($items as $itemAST) {
$value = self::valueFromAST($itemAST, $itemType, $variables);
if ($value === NullValue::getNullValue()) {
$value = null;
}
$result[] = $value;
2016-05-01 00:02:04 +03:00
}
2016-11-18 19:59:28 +03:00
return $result;
2016-05-01 00:02:04 +03:00
}
if ($type instanceof InputObjectType) {
$fields = $type->getFields();
if (!$valueAST instanceof ObjectValue) {
2016-11-18 19:59:28 +03:00
// No valid return value.
return ;
2016-05-01 00:02:04 +03:00
}
$fieldASTs = Utils::keyMap($valueAST->fields, function($field) {return $field->name->value;});
$values = [];
foreach ($fields as $field) {
$fieldAST = isset($fieldASTs[$field->name]) ? $fieldASTs[$field->name] : null;
$fieldValue = self::valueFromAST($fieldAST ? $fieldAST->value : null, $field->getType(), $variables);
2016-11-18 19:59:28 +03:00
// If field is not in AST and defaultValue was never set for this field - do not include it in result
if (null === $fieldValue && null === $field->defaultValue && !$field->defaultValueExists()) {
continue;
2016-05-01 00:02:04 +03:00
}
2016-11-18 19:59:28 +03:00
// Set Explicit null value or default value:
if (NullValue::getNullValue() === $fieldValue) {
$fieldValue = null;
} else if (null === $fieldValue) {
$fieldValue = $field->defaultValue;
2016-05-01 00:02:04 +03:00
}
2016-11-18 19:59:28 +03:00
$values[$field->name] = $fieldValue;
2016-05-01 00:02:04 +03:00
}
return $values;
}
if ($type instanceof LeafType) {
return $type->parseLiteral($valueAST);
}
throw new InvariantViolation('Must be input type');
2016-05-01 00:02:04 +03:00
}
}