mirror of
https://github.com/retailcrm/graphql-php.git
synced 2025-02-11 10:09:24 +03:00
Allow to define enum values by simply passing array of strings, e.g. 'values' => ['ONE', 'TWO', 'THREE']
This commit is contained in:
parent
0a79be8409
commit
09cc313072
@ -44,6 +44,11 @@ class EnumType extends Type implements InputType, OutputType, LeafType
|
|||||||
|
|
||||||
if (!empty($config['values'])) {
|
if (!empty($config['values'])) {
|
||||||
foreach ($config['values'] as $name => $value) {
|
foreach ($config['values'] as $name => $value) {
|
||||||
|
|
||||||
|
if (is_string($value) && is_int($name)) {
|
||||||
|
$value = ['name' => $value, 'value' => $value];
|
||||||
|
}
|
||||||
|
|
||||||
// value will be equal to name only if 'value' is not set in definition
|
// value will be equal to name only if 'value' is not set in definition
|
||||||
$this->values[] = new EnumValueDefinition($value + ['name' => $name, 'value' => $name]);
|
$this->values[] = new EnumValueDefinition($value + ['name' => $name, 'value' => $name]);
|
||||||
}
|
}
|
||||||
|
@ -37,6 +37,13 @@ class EnumTypeTest extends \PHPUnit_Framework_TestCase
|
|||||||
]
|
]
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
$simpleEnum = new EnumType([
|
||||||
|
'name' => 'SimpleEnum',
|
||||||
|
'values' => [
|
||||||
|
'ONE', 'TWO', 'THREE'
|
||||||
|
]
|
||||||
|
]);
|
||||||
|
|
||||||
$Complex1 = ['someRandomFunction' => function() {}];
|
$Complex1 = ['someRandomFunction' => function() {}];
|
||||||
$Complex2 = new \ArrayObject(['someRandomValue' => 123]);
|
$Complex2 = new \ArrayObject(['someRandomValue' => 123]);
|
||||||
|
|
||||||
@ -70,6 +77,21 @@ class EnumTypeTest extends \PHPUnit_Framework_TestCase
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
|
'simpleEnum' => [
|
||||||
|
'type' => $simpleEnum,
|
||||||
|
'args' => [
|
||||||
|
'fromName' => ['type' => Type::string()],
|
||||||
|
'fromValue' => ['type' => Type::string()]
|
||||||
|
],
|
||||||
|
'resolve' => function($value, $args) {
|
||||||
|
if (isset($args['fromName'])) {
|
||||||
|
return $args['fromName'];
|
||||||
|
}
|
||||||
|
if (isset($args['fromValue'])) {
|
||||||
|
return $args['fromValue'];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
'colorInt' => [
|
'colorInt' => [
|
||||||
'type' => Type::int(),
|
'type' => Type::int(),
|
||||||
'args' => [
|
'args' => [
|
||||||
@ -413,6 +435,26 @@ class EnumTypeTest extends \PHPUnit_Framework_TestCase
|
|||||||
$this->assertArrayNotHasKey('errors', $result);
|
$this->assertArrayNotHasKey('errors', $result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testAllowsSimpleArrayAsValues()
|
||||||
|
{
|
||||||
|
$q = '{
|
||||||
|
first: simpleEnum(fromName: "ONE")
|
||||||
|
second: simpleEnum(fromValue: "TWO")
|
||||||
|
third: simpleEnum(fromValue: "WRONG")
|
||||||
|
}';
|
||||||
|
|
||||||
|
$this->assertEquals(
|
||||||
|
[
|
||||||
|
'data' => ['first' => 'ONE', 'second' => 'TWO', 'third' => null],
|
||||||
|
'errors' => [[
|
||||||
|
'message' => 'Expected a value of type "SimpleEnum" but received: WRONG',
|
||||||
|
'locations' => [['line' => 4, 'column' => 13]]
|
||||||
|
]]
|
||||||
|
],
|
||||||
|
GraphQL::execute($this->schema, $q)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
private function expectFailure($query, $vars, $err)
|
private function expectFailure($query, $vars, $err)
|
||||||
{
|
{
|
||||||
$result = GraphQL::executeAndReturnResult($this->schema, $query, null, null, $vars);
|
$result = GraphQL::executeAndReturnResult($this->schema, $query, null, null, $vars);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user