mirror of
https://github.com/retailcrm/graphql-php.git
synced 2024-11-22 12:56:05 +03:00
Infer type name from class name for those using inheritance
This commit is contained in:
parent
221ec6f792
commit
e4fa881cc3
@ -25,6 +25,10 @@ class InputObjectType extends Type implements InputType
|
||||
*/
|
||||
public function __construct(array $config)
|
||||
{
|
||||
if (!isset($config['name'])) {
|
||||
$config['name'] = $this->tryInferName();
|
||||
}
|
||||
|
||||
Config::validate($config, [
|
||||
'name' => Config::STRING | Config::REQUIRED,
|
||||
'fields' => Config::arrayOf([
|
||||
@ -32,7 +36,7 @@ class InputObjectType extends Type implements InputType
|
||||
'type' => Config::INPUT_TYPE | Config::REQUIRED,
|
||||
'defaultValue' => Config::ANY,
|
||||
'description' => Config::STRING
|
||||
], Config::KEY_AS_NAME | Config::MAYBE_THUNK),
|
||||
], Config::KEY_AS_NAME | Config::MAYBE_THUNK | Config::MAYBE_TYPE),
|
||||
'description' => Config::STRING
|
||||
]);
|
||||
|
||||
|
@ -35,6 +35,10 @@ class InterfaceType extends Type implements AbstractType, OutputType, CompositeT
|
||||
*/
|
||||
public function __construct(array $config)
|
||||
{
|
||||
if (!isset($config['name'])) {
|
||||
$config['name'] = $this->tryInferName();
|
||||
}
|
||||
|
||||
Config::validate($config, [
|
||||
'name' => Config::STRING,
|
||||
'fields' => Config::arrayOf(
|
||||
|
@ -79,6 +79,10 @@ class ObjectType extends Type implements OutputType, CompositeType
|
||||
*/
|
||||
public function __construct(array $config)
|
||||
{
|
||||
if (!isset($config['name'])) {
|
||||
$config['name'] = $this->tryInferName();
|
||||
}
|
||||
|
||||
Utils::invariant(!empty($config['name']), 'Every type is expected to have name');
|
||||
|
||||
// Note: this validation is disabled by default, because it is resource-consuming
|
||||
|
@ -242,6 +242,27 @@ abstract class Type
|
||||
*/
|
||||
public $description;
|
||||
|
||||
/**
|
||||
* @return null|string
|
||||
*/
|
||||
protected function tryInferName()
|
||||
{
|
||||
if ($this->name) {
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
// If class is extended - infer name from className
|
||||
// QueryType -> Type
|
||||
// SomeOtherType -> SomeOther
|
||||
$tmp = new \ReflectionClass($this);
|
||||
$name = $tmp->getShortName();
|
||||
|
||||
if ($tmp->getNamespaceName() !== __NAMESPACE__) {
|
||||
return preg_replace('~Type$~', '', $name);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
|
@ -36,6 +36,10 @@ class UnionType extends Type implements AbstractType, OutputType, CompositeType
|
||||
*/
|
||||
public function __construct($config)
|
||||
{
|
||||
if (!isset($config['name'])) {
|
||||
$config['name'] = $this->tryInferName();
|
||||
}
|
||||
|
||||
Config::validate($config, [
|
||||
'name' => Config::STRING | Config::REQUIRED,
|
||||
'types' => Config::arrayOf(Config::OBJECT_TYPE, Config::MAYBE_THUNK | Config::REQUIRED),
|
||||
|
@ -1,6 +1,8 @@
|
||||
<?php
|
||||
namespace GraphQL\Tests\Type;
|
||||
|
||||
require_once __DIR__ . '/TestClasses.php';
|
||||
|
||||
use GraphQL\Schema;
|
||||
use GraphQL\Type\Definition\Config;
|
||||
use GraphQL\Type\Definition\EnumType;
|
||||
@ -666,4 +668,14 @@ class DefinitionTest extends \PHPUnit_Framework_TestCase
|
||||
$this->assertEquals($interface, $testField->getType());
|
||||
$this->assertEquals('test', $testField->name);
|
||||
}
|
||||
|
||||
public function testInfersNameFromClassname()
|
||||
{
|
||||
$myObj = new MyCustomType();
|
||||
$this->assertEquals('MyCustom', $myObj->name);
|
||||
|
||||
$otherCustom = new OtherCustom();
|
||||
$this->assertEquals('OtherCustom', $otherCustom->name);
|
||||
}
|
||||
}
|
||||
|
||||
|
31
tests/Type/TestClasses.php
Normal file
31
tests/Type/TestClasses.php
Normal file
@ -0,0 +1,31 @@
|
||||
<?php
|
||||
namespace GraphQL\Tests\Type;
|
||||
|
||||
use GraphQL\Type\Definition\ObjectType;
|
||||
use GraphQL\Type\Definition\Type;
|
||||
|
||||
class MyCustomType extends ObjectType
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
$config = [
|
||||
'fields' => [
|
||||
'a' => Type::string()
|
||||
]
|
||||
];
|
||||
parent::__construct($config);
|
||||
}
|
||||
}
|
||||
|
||||
class OtherCustom extends ObjectType
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
$config = [
|
||||
'fields' => [
|
||||
'b' => Type::string()
|
||||
]
|
||||
];
|
||||
parent::__construct($config);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user