getValue() for EnumType (and getEnumValue() for TypeInfo)

This commit is contained in:
Vladimir Razuvaev 2017-07-03 23:23:12 +07:00
parent 14ef8ef835
commit 29c1132554
4 changed files with 57 additions and 3 deletions

View File

@ -71,6 +71,16 @@ class EnumType extends Type implements InputType, OutputType, LeafType
return $this->values; return $this->values;
} }
/**
* @param $name
* @return mixed|null
*/
public function getValue($name)
{
$lookup = $this->getNameLookup();
return is_scalar($name) && isset($lookup[$name]) ? $lookup[$name] : null;
}
/** /**
* @param $value * @param $value
* @return null * @return null

View File

@ -11,6 +11,7 @@ use GraphQL\Schema;
use GraphQL\Type\Definition\AbstractType; use GraphQL\Type\Definition\AbstractType;
use GraphQL\Type\Definition\CompositeType; use GraphQL\Type\Definition\CompositeType;
use GraphQL\Type\Definition\Directive; use GraphQL\Type\Definition\Directive;
use GraphQL\Type\Definition\EnumType;
use GraphQL\Type\Definition\FieldArgument; use GraphQL\Type\Definition\FieldArgument;
use GraphQL\Type\Definition\FieldDefinition; use GraphQL\Type\Definition\FieldDefinition;
use GraphQL\Type\Definition\InputObjectType; use GraphQL\Type\Definition\InputObjectType;
@ -291,6 +292,11 @@ class TypeInfo
*/ */
private $argument; private $argument;
/**
* @var mixed
*/
private $enumValue;
/** /**
* TypeInfo constructor. * TypeInfo constructor.
* @param Schema $schema * @param Schema $schema
@ -364,6 +370,14 @@ class TypeInfo
return $this->argument; return $this->argument;
} }
/**
* @return mixed
*/
function getEnumValue()
{
return $this->enumValue;
}
/** /**
* @param Node $node * @param Node $node
*/ */
@ -448,6 +462,15 @@ class TypeInfo
} }
$this->inputTypeStack[] = $fieldType; $this->inputTypeStack[] = $fieldType;
break; break;
case NodeKind::ENUM:
$enumType = Type::getNamedType($this->getInputType());
$enumValue = null;
if ($enumType instanceof EnumType) {
$enumValue = $enumType->getValue($node->value);
}
$this->enumValue = $enumValue;
break;
} }
} }
@ -486,6 +509,9 @@ class TypeInfo
case NodeKind::OBJECT_FIELD: case NodeKind::OBJECT_FIELD:
array_pop($this->inputTypeStack); array_pop($this->inputTypeStack);
break; break;
case NodeKind::ENUM:
$this->enumValue = null;
break;
} }
} }
} }

View File

@ -383,9 +383,9 @@ class EnumTypeTest extends \PHPUnit_Framework_TestCase
} }
/** /**
* @it may present a values API for complex enums * @it presents a getValues() API for complex enums
*/ */
public function testMayPresentValuesAPIForComplexEnums() public function testPresentsGetValuesAPIForComplexEnums()
{ {
$ComplexEnum = $this->ComplexEnum; $ComplexEnum = $this->ComplexEnum;
$values = $ComplexEnum->getValues(); $values = $ComplexEnum->getValues();
@ -397,6 +397,19 @@ class EnumTypeTest extends \PHPUnit_Framework_TestCase
$this->assertEquals($this->Complex2, $values[1]->value); $this->assertEquals($this->Complex2, $values[1]->value);
} }
/**
* @it presents a getValue() API for complex enums
*/
public function testPresentsGetValueAPIForComplexEnums()
{
$oneValue = $this->ComplexEnum->getValue('ONE');
$this->assertEquals('ONE', $oneValue->name);
$this->assertEquals($this->Complex1, $oneValue->value);
$badUsage = $this->ComplexEnum->getValue($this->Complex1);
$this->assertEquals(null, $badUsage);
}
/** /**
* @it may be internally represented with complex values * @it may be internally represented with complex values
*/ */

View File

@ -243,6 +243,11 @@ class ValidationTest extends \PHPUnit_Framework_TestCase
// TODO: does not allow isDeprecated without deprecationReason on field // TODO: does not allow isDeprecated without deprecationReason on field
// TODO: does not allow isDeprecated without deprecationReason on enum // TODO: does not allow isDeprecated without deprecationReason on enum
// Type System: Object fields must have valid resolve values
// TODO: accepts a lambda as an Object field resolver
// TODO: rejects an empty Object field resolver
// TODO: rejects a constant scalar value resolver
private function assertEachCallableThrows($closures, $expectedError) private function assertEachCallableThrows($closures, $expectedError)
{ {
foreach ($closures as $index => $factory) { foreach ($closures as $index => $factory) {