|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace TheCodingMachine\GraphQLite\Mappers\Root; |
| 6 | + |
| 7 | +use GraphQL\Type\Definition\InputType; |
| 8 | +use GraphQL\Type\Definition\NamedType; |
| 9 | +use GraphQL\Type\Definition\OutputType; |
| 10 | +use GraphQL\Type\Definition\Type as GraphQLType; |
| 11 | +use phpDocumentor\Reflection\DocBlock; |
| 12 | +use phpDocumentor\Reflection\Type; |
| 13 | +use phpDocumentor\Reflection\Types\Void_; |
| 14 | +use ReflectionMethod; |
| 15 | +use ReflectionProperty; |
| 16 | +use TheCodingMachine\GraphQLite\Mappers\CannotMapTypeException; |
| 17 | +use TheCodingMachine\GraphQLite\Types\VoidType; |
| 18 | + |
| 19 | +class VoidTypeMapper implements RootTypeMapperInterface |
| 20 | +{ |
| 21 | + private static VoidType $voidType; |
| 22 | + |
| 23 | + public function __construct( |
| 24 | + private readonly RootTypeMapperInterface $next, |
| 25 | + ) |
| 26 | + { |
| 27 | + } |
| 28 | + |
| 29 | + public function toGraphQLOutputType(Type $type, OutputType|null $subType, ReflectionMethod|ReflectionProperty $reflector, DocBlock $docBlockObj): OutputType&GraphQLType |
| 30 | + { |
| 31 | + if (! $type instanceof Void_) { |
| 32 | + return $this->next->toGraphQLOutputType($type, $subType, $reflector, $docBlockObj); |
| 33 | + } |
| 34 | + |
| 35 | + return self::getVoidType(); |
| 36 | + } |
| 37 | + |
| 38 | + public function toGraphQLInputType(Type $type, InputType|null $subType, string $argumentName, ReflectionMethod|ReflectionProperty $reflector, DocBlock $docBlockObj): InputType&GraphQLType |
| 39 | + { |
| 40 | + if (! $type instanceof Void_) { |
| 41 | + return $this->next->toGraphQLInputType($type, $subType, $argumentName, $reflector, $docBlockObj); |
| 42 | + } |
| 43 | + |
| 44 | + throw CannotMapTypeException::mustBeOutputType(self::getVoidType()->name); |
| 45 | + } |
| 46 | + |
| 47 | + public function mapNameToType(string $typeName): NamedType&GraphQLType |
| 48 | + { |
| 49 | + return match ($typeName) { |
| 50 | + self::getVoidType()->name => self::getVoidType(), |
| 51 | + default => $this->next->mapNameToType($typeName), |
| 52 | + }; |
| 53 | + } |
| 54 | + |
| 55 | + private static function getVoidType(): VoidType |
| 56 | + { |
| 57 | + return self::$voidType ??= new VoidType(); |
| 58 | + } |
| 59 | +} |
0 commit comments