|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace TheCodingMachine\GraphQLite\Mappers\Root; |
| 6 | + |
| 7 | +use Closure; |
| 8 | +use GraphQL\Type\Definition\InputType; |
| 9 | +use GraphQL\Type\Definition\NamedType; |
| 10 | +use GraphQL\Type\Definition\OutputType; |
| 11 | +use GraphQL\Type\Definition\Type as GraphQLType; |
| 12 | +use phpDocumentor\Reflection\DocBlock; |
| 13 | +use phpDocumentor\Reflection\Fqsen; |
| 14 | +use phpDocumentor\Reflection\Type; |
| 15 | +use phpDocumentor\Reflection\Types\Callable_; |
| 16 | +use phpDocumentor\Reflection\Types\Compound; |
| 17 | +use phpDocumentor\Reflection\Types\Object_; |
| 18 | +use ReflectionMethod; |
| 19 | +use ReflectionProperty; |
| 20 | +use TheCodingMachine\GraphQLite\Mappers\CannotMapTypeException; |
| 21 | + |
| 22 | +use function count; |
| 23 | +use function iterator_to_array; |
| 24 | + |
| 25 | +/** |
| 26 | + * This mapper maps callable types into their return types, so that fields can defer their execution. |
| 27 | + */ |
| 28 | +class ClosureTypeMapper implements RootTypeMapperInterface |
| 29 | +{ |
| 30 | + private Object_ $closureType; |
| 31 | + |
| 32 | + public function __construct( |
| 33 | + private readonly RootTypeMapperInterface $next, |
| 34 | + private readonly RootTypeMapperInterface $topRootTypeMapper, |
| 35 | + ) { |
| 36 | + $this->closureType = new Object_(new Fqsen('\\' . Closure::class)); |
| 37 | + } |
| 38 | + |
| 39 | + public function toGraphQLOutputType(Type $type, OutputType|null $subType, ReflectionMethod|ReflectionProperty $reflector, DocBlock $docBlockObj): OutputType&GraphQLType |
| 40 | + { |
| 41 | + // This check exists because any string may be a callable (referring to a global function), |
| 42 | + // so if a string that looks like a callable is returned from a resolver, it will get wrapped |
| 43 | + // in `Deferred`, even though it wasn't supposed to be a deferred value. This could be fixed |
| 44 | + // by combining `QueryField`'s resolver and `CallableTypeMapper` into one place, but |
| 45 | + // that's not currently possible with GraphQLite's design. |
| 46 | + if ($type instanceof Callable_) { |
| 47 | + throw CannotMapTypeException::createForUnexpectedCallable(); |
| 48 | + } |
| 49 | + |
| 50 | + if (! $type instanceof Compound || ! $type->contains($this->closureType)) { |
| 51 | + return $this->next->toGraphQLOutputType($type, $subType, $reflector, $docBlockObj); |
| 52 | + } |
| 53 | + |
| 54 | + $allTypes = iterator_to_array($type); |
| 55 | + |
| 56 | + if (count($allTypes) !== 2) { |
| 57 | + return $this->next->toGraphQLOutputType($type, $subType, $reflector, $docBlockObj); |
| 58 | + } |
| 59 | + |
| 60 | + $callableType = $this->findCallableType($allTypes); |
| 61 | + $returnType = $callableType?->getReturnType(); |
| 62 | + |
| 63 | + if (! $returnType) { |
| 64 | + throw CannotMapTypeException::createForMissingClosureReturnType(); |
| 65 | + } |
| 66 | + |
| 67 | + if ($callableType->getParameters()) { |
| 68 | + throw CannotMapTypeException::createForUnexpectedClosureParameters(); |
| 69 | + } |
| 70 | + |
| 71 | + return $this->topRootTypeMapper->toGraphQLOutputType($returnType, null, $reflector, $docBlockObj); |
| 72 | + } |
| 73 | + |
| 74 | + public function toGraphQLInputType(Type $type, InputType|null $subType, string $argumentName, ReflectionMethod|ReflectionProperty $reflector, DocBlock $docBlockObj): InputType&GraphQLType |
| 75 | + { |
| 76 | + if (! $type instanceof Callable_) { |
| 77 | + return $this->next->toGraphQLInputType($type, $subType, $argumentName, $reflector, $docBlockObj); |
| 78 | + } |
| 79 | + |
| 80 | + throw CannotMapTypeException::createForClosureAsInput(); |
| 81 | + } |
| 82 | + |
| 83 | + public function mapNameToType(string $typeName): NamedType&GraphQLType |
| 84 | + { |
| 85 | + return $this->next->mapNameToType($typeName); |
| 86 | + } |
| 87 | + |
| 88 | + /** @param array<int, Type> $types */ |
| 89 | + private function findCallableType(array $types): Callable_|null |
| 90 | + { |
| 91 | + foreach ($types as $type) { |
| 92 | + if ($type instanceof Callable_) { |
| 93 | + return $type; |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + return null; |
| 98 | + } |
| 99 | +} |
0 commit comments