#php #graphql
Вопрос:
Я использую PropertyFilterType
класс для проверки входных данных и определяю defaultValue
role
language
поля для и с помощью getValue
функции EnumType
объекта. Проблема, которую я получаю, когда ожидается целое число
Класс PropertyFilterType :
class PropertyFilterType extends InputObjectType { public function __construct() { parent::__construct([ 'name' =gt; 'PropertyFilter', 'fields' =gt; [ 'id' =gt; [ 'type' =gt; Types::int(), 'description' =gt; 'Property id' ], 'role' =gt; [ 'type' =gt; Types::role(), 'description' =gt; 'seeker informations role', 'defaultValue'=gt;(new RoleType)-gt;getValue('SEARCH') ], 'language' =gt; [ 'type' =gt; Types::language(), 'description' =gt; 'Informations language', 'defaultValue'=gt;(new LanguageType)-gt;getValue('FR') ] ] ]); } }
Класс типа языка:
class LanguageType extends EnumType { public const EN = 0; public const FR = 1; public const AR = 2; public function __construct() { parent::__construct([ 'name' =gt; 'Language', 'values' =gt; [ 'EN' =gt; ['value' =gt; self::EN], 'FR' =gt; ['value' =gt; self::FR], 'AR' =gt; ['value' =gt; self::AR] ] ]); } }
Класс ролевого типа:
class RoleType extends EnumType { public const SEARCH = 0; public const USER = 1; public const ADMIN = 2; public function __construct() { parent::__construct([ 'name' =gt; 'Role', 'values' =gt; [ 'SEARCH' =gt; ['value' =gt; self::SEARCH], 'USER' =gt; ['value' =gt; self::USER], 'ADMIN' =gt; ['value' =gt; self::ADMIN] ] ]); } }
TypesClass :
class Types { /** @var arraylt;string, Typegt; */ private static array $types = []; public static function language(): callable { return static::get(LanguageType::class); } public static function role(): callable { return static::get(RoleType::class); } public static function propertyFilter(): callable { return static::get(PropertyFilterType::class); } /** * @return Closure(): Type */ private static function get(string $classname): Closure { return static fn () =gt; static::byClassName($classname); } private static function byClassName(string $classname): Type { $parts = explode('\', $classname); $cacheName = strtolower(preg_replace('~Type$~', '', $parts[count($parts) - 1])); $type = null; if (! isset(self::$types[$cacheName])) { if (class_exists($classname)) { $type = new $classname(); } self::$types[$cacheName] = $type; } $type = self::$types[$cacheName]; if (! $type) { throw new Exception('Unknown graphql type: ' . $classname); } return $type; } public static function int(): ScalarType { return Type::int(); } }