mirror of
https://github.com/retailcrm/graphql-php.git
synced 2024-11-22 12:56:05 +03:00
getValue() for EnumType (and getEnumValue() for TypeInfo)
This commit is contained in:
parent
14ef8ef835
commit
29c1132554
@ -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
|
||||
|
@ -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
|
||||
*/
|
||||
@ -448,6 +462,15 @@ class TypeInfo
|
||||
}
|
||||
$this->inputTypeStack[] = $fieldType;
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
*/
|
||||
|
@ -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) {
|
||||
|
Loading…
Reference in New Issue
Block a user