diff --git a/src/Type/Definition/EnumType.php b/src/Type/Definition/EnumType.php index 828918b..4edcccd 100644 --- a/src/Type/Definition/EnumType.php +++ b/src/Type/Definition/EnumType.php @@ -71,6 +71,16 @@ class EnumType extends Type implements InputType, OutputType, LeafType 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 * @return null diff --git a/src/Utils/TypeInfo.php b/src/Utils/TypeInfo.php index 929caa1..7205720 100644 --- a/src/Utils/TypeInfo.php +++ b/src/Utils/TypeInfo.php @@ -11,6 +11,7 @@ use GraphQL\Schema; use GraphQL\Type\Definition\AbstractType; use GraphQL\Type\Definition\CompositeType; use GraphQL\Type\Definition\Directive; +use GraphQL\Type\Definition\EnumType; use GraphQL\Type\Definition\FieldArgument; use GraphQL\Type\Definition\FieldDefinition; use GraphQL\Type\Definition\InputObjectType; @@ -291,6 +292,11 @@ class TypeInfo */ private $argument; + /** + * @var mixed + */ + private $enumValue; + /** * TypeInfo constructor. * @param Schema $schema @@ -364,6 +370,14 @@ class TypeInfo return $this->argument; } + /** + * @return mixed + */ + function getEnumValue() + { + return $this->enumValue; + } + /** * @param Node $node */ @@ -447,7 +461,16 @@ class TypeInfo $fieldType = $inputField ? $inputField->getType() : null; } $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: array_pop($this->inputTypeStack); break; + case NodeKind::ENUM: + $this->enumValue = null; + break; } } } diff --git a/tests/Type/EnumTypeTest.php b/tests/Type/EnumTypeTest.php index 126d5b7..158d025 100644 --- a/tests/Type/EnumTypeTest.php +++ b/tests/Type/EnumTypeTest.php @@ -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; $values = $ComplexEnum->getValues(); @@ -397,6 +397,19 @@ class EnumTypeTest extends \PHPUnit_Framework_TestCase $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 */ diff --git a/tests/Type/ValidationTest.php b/tests/Type/ValidationTest.php index 43a5653..7f3094e 100644 --- a/tests/Type/ValidationTest.php +++ b/tests/Type/ValidationTest.php @@ -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 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) { foreach ($closures as $index => $factory) {