-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for void return type (#574)
* Add support for void return type * Extract VoidTypeMapper separately to make sure it only matches standalone usages in return types * Revert Types class and rename LastTopRootTypeMapper
- Loading branch information
1 parent
db4d604
commit 5a9c03b
Showing
14 changed files
with
245 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace TheCodingMachine\GraphQLite\Mappers\Root; | ||
|
||
use GraphQL\Type\Definition\InputType; | ||
use GraphQL\Type\Definition\NamedType; | ||
use GraphQL\Type\Definition\OutputType; | ||
use GraphQL\Type\Definition\Type as GraphQLType; | ||
use phpDocumentor\Reflection\DocBlock; | ||
use phpDocumentor\Reflection\Type; | ||
use ReflectionMethod; | ||
use ReflectionProperty; | ||
|
||
/** | ||
* The last root type mapper that always calls the dynamicly set "next" mapper. | ||
*/ | ||
class LastDelegatingTypeMapper implements RootTypeMapperInterface | ||
{ | ||
private RootTypeMapperInterface $next; | ||
|
||
public function setNext(RootTypeMapperInterface $next): void | ||
{ | ||
$this->next = $next; | ||
} | ||
|
||
public function toGraphQLOutputType(Type $type, OutputType|null $subType, ReflectionMethod|ReflectionProperty $reflector, DocBlock $docBlockObj): OutputType&GraphQLType | ||
{ | ||
return $this->next->toGraphQLOutputType($type, $subType, $reflector, $docBlockObj); | ||
} | ||
|
||
public function toGraphQLInputType(Type $type, InputType|null $subType, string $argumentName, ReflectionMethod|ReflectionProperty $reflector, DocBlock $docBlockObj): InputType&GraphQLType | ||
{ | ||
return $this->next->toGraphQLInputType($type, $subType, $argumentName, $reflector, $docBlockObj); | ||
} | ||
|
||
public function mapNameToType(string $typeName): NamedType&GraphQLType | ||
{ | ||
return $this->next->mapNameToType($typeName); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace TheCodingMachine\GraphQLite\Mappers\Root; | ||
|
||
use GraphQL\Type\Definition\InputType; | ||
use GraphQL\Type\Definition\NamedType; | ||
use GraphQL\Type\Definition\OutputType; | ||
use GraphQL\Type\Definition\Type as GraphQLType; | ||
use phpDocumentor\Reflection\DocBlock; | ||
use phpDocumentor\Reflection\Type; | ||
use phpDocumentor\Reflection\Types\Void_; | ||
use ReflectionMethod; | ||
use ReflectionProperty; | ||
use TheCodingMachine\GraphQLite\Mappers\CannotMapTypeException; | ||
use TheCodingMachine\GraphQLite\Types\VoidType; | ||
|
||
class VoidTypeMapper implements RootTypeMapperInterface | ||
{ | ||
private static VoidType $voidType; | ||
|
||
public function __construct( | ||
private readonly RootTypeMapperInterface $next, | ||
) | ||
{ | ||
} | ||
|
||
public function toGraphQLOutputType(Type $type, OutputType|null $subType, ReflectionMethod|ReflectionProperty $reflector, DocBlock $docBlockObj): OutputType&GraphQLType | ||
{ | ||
if (! $type instanceof Void_) { | ||
return $this->next->toGraphQLOutputType($type, $subType, $reflector, $docBlockObj); | ||
} | ||
|
||
return self::getVoidType(); | ||
} | ||
|
||
public function toGraphQLInputType(Type $type, InputType|null $subType, string $argumentName, ReflectionMethod|ReflectionProperty $reflector, DocBlock $docBlockObj): InputType&GraphQLType | ||
{ | ||
if (! $type instanceof Void_) { | ||
return $this->next->toGraphQLInputType($type, $subType, $argumentName, $reflector, $docBlockObj); | ||
} | ||
|
||
throw CannotMapTypeException::mustBeOutputType(self::getVoidType()->name); | ||
} | ||
|
||
public function mapNameToType(string $typeName): NamedType&GraphQLType | ||
{ | ||
return match ($typeName) { | ||
self::getVoidType()->name => self::getVoidType(), | ||
default => $this->next->mapNameToType($typeName), | ||
}; | ||
} | ||
|
||
private static function getVoidType(): VoidType | ||
{ | ||
return self::$voidType ??= new VoidType(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace TheCodingMachine\GraphQLite\Types; | ||
|
||
use GraphQL\Language\AST\Node; | ||
use GraphQL\Type\Definition\ScalarType; | ||
use TheCodingMachine\GraphQLite\GraphQLRuntimeException; | ||
|
||
class VoidType extends ScalarType | ||
{ | ||
public string $name = 'Void'; | ||
|
||
public string|null $description = 'The `Void` scalar type represents no value being returned.'; | ||
|
||
public function serialize(mixed $value): bool|null | ||
{ | ||
// Return type contains `bool` because `null` is only allowed as a standalone type since PHP 8.2. | ||
return null; | ||
} | ||
|
||
public function parseValue(mixed $value): never | ||
{ | ||
throw new GraphQLRuntimeException(); | ||
} | ||
|
||
public function parseLiteral(Node $valueNode, array|null $variables = null): never | ||
{ | ||
throw new GraphQLRuntimeException(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.