Skip to content

Commit

Permalink
Merge pull request #199 from moufmouf/refactor_typemappingruntimeexce…
Browse files Browse the repository at this point in the history
…ption

Migrating away from TypeMappingRuntimeException
  • Loading branch information
moufmouf committed Dec 20, 2019
2 parents 7be00ca + 65d3496 commit dd08619
Show file tree
Hide file tree
Showing 15 changed files with 111 additions and 205 deletions.
55 changes: 55 additions & 0 deletions src/Mappers/CannotMapTypeException.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,14 @@
use GraphQL\Type\Definition\NamedType;
use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\Type;
use phpDocumentor\Reflection\Type as PhpDocumentorType;
use phpDocumentor\Reflection\Types\Array_;
use phpDocumentor\Reflection\Types\Iterable_;
use phpDocumentor\Reflection\Types\Mixed_;
use ReflectionMethod;
use TheCodingMachine\GraphQLite\Annotations\ExtendType;
use function array_map;
use function assert;
use function implode;
use function sprintf;

Expand Down Expand Up @@ -109,4 +115,53 @@ public static function extendTypeWithBadTargetedClass(string $className, ExtendT
{
return new self('For ' . self::extendTypeToString($extendType) . ' annotation declared in class "' . $className . '", the pointed at GraphQL type cannot be extended. You can only target types extending the MutableObjectType (like types created with the @Type annotation).');
}

/**
* @param Array_|Iterable_|Mixed_ $type
*/
public static function createForMissingPhpDoc(PhpDocumentorType $type, ReflectionMethod $refMethod, ?string $argumentName = null): self
{
$typeStr = '';
if ($type instanceof Array_) {
$typeStr = 'array';
} elseif ($type instanceof Iterable_) {
$typeStr = 'iterable';
} elseif ($type instanceof Mixed_) {
$typeStr = 'mixed';
}
assert($typeStr !== '');
if ($argumentName === null) {
if ($typeStr === 'mixed') {
return new self('a type-hint is missing (or PHPDoc specifies a "mixed" type-hint). Please provide a better type-hint.');
}

return new self(sprintf('please provide an additional @return in the PHPDoc block to further specify the return type of %s. For instance: @return string[]', $typeStr));
}

if ($typeStr === 'mixed') {
return new self(sprintf('a type-hint is missing (or PHPDoc specifies a "mixed" type-hint). Please provide a better type-hint. For instance: "string $%s".', $argumentName));
}

return new self(sprintf('please provide an additional @param in the PHPDoc block to further specify the type of the %s. For instance: @param string[] $%s.', $typeStr, $argumentName));
}

public static function createForDateTime(): self
{
return new self('type-hinting against DateTime is not allowed. Please use the DateTimeImmutable type instead.');
}

public static function createForNull(): self
{
return new self('type-hinting against null only in the PHPDoc is not allowed.');
}

public static function createForInputUnionType(PhpDocumentorType $type): self
{
return new self('parameter is type-hinted to "' . $type . '". Type-hinting a parameter to a union type is forbidden in GraphQL. Only return types can be union types.');
}

public static function createForPhpDocType(PhpDocumentorType $type): self
{
return new self("don't know how to handle type " . (string) $type);
}
}
8 changes: 2 additions & 6 deletions src/Mappers/Parameters/TypeHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@
use TheCodingMachine\GraphQLite\Annotations\ParameterAnnotations;
use TheCodingMachine\GraphQLite\Annotations\UseInputType;
use TheCodingMachine\GraphQLite\InvalidDocBlockRuntimeException;
use TheCodingMachine\GraphQLite\Mappers\CannotMapTypeException;
use TheCodingMachine\GraphQLite\Mappers\CannotMapTypeExceptionInterface;
use TheCodingMachine\GraphQLite\Mappers\Root\RootTypeMapperInterface;
use TheCodingMachine\GraphQLite\Parameters\DefaultValueParameter;
use TheCodingMachine\GraphQLite\Parameters\InputTypeParameter;
use TheCodingMachine\GraphQLite\Parameters\ParameterInterface;
use TheCodingMachine\GraphQLite\TypeMappingRuntimeException;
use TheCodingMachine\GraphQLite\Types\ArgumentResolver;
use TheCodingMachine\GraphQLite\Types\TypeResolver;
use Webmozart\Assert\Assert;
Expand Down Expand Up @@ -83,8 +83,6 @@ public function mapReturnType(ReflectionMethod $refMethod, DocBlock $docBlockObj
try {
$type = $this->mapType($phpdocType, $docBlockReturnType, $returnType ? $returnType->allowsNull() : false, false, $refMethod, $docBlockObj);
assert($type instanceof GraphQLType && $type instanceof OutputType);
} catch (TypeMappingRuntimeException $e) {
throw TypeMappingRuntimeException::wrapWithReturnInfo($e, $refMethod);
} catch (CannotMapTypeExceptionInterface $e) {
$e->addReturnInfo($refMethod);
throw $e;
Expand Down Expand Up @@ -146,8 +144,6 @@ public function mapParameter(ReflectionParameter $parameter, DocBlock $docBlock,
$declaringFunction = $parameter->getDeclaringFunction();
Assert::isInstanceOf($declaringFunction, ReflectionMethod::class, 'Parameter of a function passed. Only parameters of methods are supported.');
$type = $this->mapType($phpdocType, $paramTagType, $allowsNull || $parameter->isDefaultValueAvailable(), true, $declaringFunction, $docBlock, $parameter->getName());
} catch (TypeMappingRuntimeException $e) {
throw TypeMappingRuntimeException::wrapWithParamInfo($e, $parameter);
} catch (CannotMapTypeExceptionInterface $e) {
$e->addParamInfo($parameter);
throw $e;
Expand Down Expand Up @@ -179,7 +175,7 @@ private function mapType(Type $type, ?Type $docBlockType, bool $isNullable, bool
if ($innerType instanceof Array_ || $innerType instanceof Iterable_ || $innerType instanceof Mixed_) {
// We need to use the docBlockType
if ($docBlockType === null) {
throw TypeMappingRuntimeException::createFromType($type);
throw CannotMapTypeException::createForMissingPhpDoc($innerType, $refMethod, $argumentName);
}
if ($mapToInputType === true) {
Assert::notNull($argumentName);
Expand Down
4 changes: 2 additions & 2 deletions src/Mappers/Root/BaseTypeMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
use phpDocumentor\Reflection\Types\String_;
use Psr\Http\Message\UploadedFileInterface;
use ReflectionMethod;
use TheCodingMachine\GraphQLite\Mappers\CannotMapTypeException;
use TheCodingMachine\GraphQLite\Mappers\CannotMapTypeExceptionInterface;
use TheCodingMachine\GraphQLite\Mappers\RecursiveTypeMapperInterface;
use TheCodingMachine\GraphQLite\TypeMappingRuntimeException;
use TheCodingMachine\GraphQLite\Types\DateTimeType;
use TheCodingMachine\GraphQLite\Types\ID;
use function ltrim;
Expand Down Expand Up @@ -144,7 +144,7 @@ private function mapBaseType(Type $type)
case '\\' . UploadedFileInterface::class:
return self::getUploadType();
case '\\DateTime':
throw TypeMappingRuntimeException::createFromType($type);
throw CannotMapTypeException::createForDateTime();
case '\\' . ID::class:
return GraphQLType::id();
default:
Expand Down
10 changes: 4 additions & 6 deletions src/Mappers/Root/CompoundTypeMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
use phpDocumentor\Reflection\Types\Compound;
use phpDocumentor\Reflection\Types\Iterable_;
use ReflectionMethod;
use RuntimeException;
use TheCodingMachine\GraphQLite\Mappers\CannotMapTypeException;
use TheCodingMachine\GraphQLite\Mappers\RecursiveTypeMapperInterface;
use TheCodingMachine\GraphQLite\TypeMappingRuntimeException;
use TheCodingMachine\GraphQLite\TypeRegistry;
use TheCodingMachine\GraphQLite\Types\UnionType;
use Webmozart\Assert\Assert;
Expand Down Expand Up @@ -63,9 +63,7 @@ public function toGraphQLOutputType(Type $type, ?OutputType $subType, Reflection
}

$filteredDocBlockTypes = iterator_to_array($type);
if (empty($filteredDocBlockTypes)) {
throw TypeMappingRuntimeException::createFromType($type);
}
Assert::notEmpty($filteredDocBlockTypes);

$unionTypes = [];
$lastException = null;
Expand All @@ -79,7 +77,7 @@ public function toGraphQLOutputType(Type $type, ?OutputType $subType, Reflection
}

if ($mustBeIterable && empty($unionTypes)) {
throw TypeMappingRuntimeException::createFromType(new Iterable_());
throw new RuntimeException('Iterable compound type cannot be alone in the compound.');
}

$return = $this->getTypeFromUnion($unionTypes);
Expand Down Expand Up @@ -107,7 +105,7 @@ public function toGraphQLInputType(Type $type, ?InputType $subType, string $argu
// At this point, the |null has been removed and the |iterable has been removed too.
// So there should only be compound input types, which is forbidden by the GraphQL spec.
// Let's kill this right away
throw TypeMappingRuntimeException::createFromType($type);
throw CannotMapTypeException::createForInputUnionType($type);
}

private function isWrappedListOfType(GraphQLType $type): bool
Expand Down
6 changes: 3 additions & 3 deletions src/Mappers/Root/FinalRootTypeMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
use phpDocumentor\Reflection\DocBlock;
use phpDocumentor\Reflection\Type;
use ReflectionMethod;
use TheCodingMachine\GraphQLite\Mappers\CannotMapTypeException;
use TheCodingMachine\GraphQLite\Mappers\RecursiveTypeMapperInterface;
use TheCodingMachine\GraphQLite\TypeMappingRuntimeException;

/**
* The final root type mapper of the RootTypeMapperInterface chain.
Expand All @@ -37,7 +37,7 @@ public function __construct(RecursiveTypeMapperInterface $recursiveTypeMapper)
*/
public function toGraphQLOutputType(Type $type, ?OutputType $subType, ReflectionMethod $refMethod, DocBlock $docBlockObj): OutputType
{
throw TypeMappingRuntimeException::createFromType($type);
throw CannotMapTypeException::createForPhpDocType($type);
}

/**
Expand All @@ -47,7 +47,7 @@ public function toGraphQLOutputType(Type $type, ?OutputType $subType, Reflection
*/
public function toGraphQLInputType(Type $type, ?InputType $subType, string $argumentName, ReflectionMethod $refMethod, DocBlock $docBlockObj): InputType
{
throw TypeMappingRuntimeException::createFromType($type);
throw CannotMapTypeException::createForPhpDocType($type);
}

/**
Expand Down
5 changes: 2 additions & 3 deletions src/Mappers/Root/IteratorTypeMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
use ReflectionMethod;
use TheCodingMachine\GraphQLite\Mappers\CannotMapTypeException;
use TheCodingMachine\GraphQLite\Mappers\CannotMapTypeExceptionInterface;
use TheCodingMachine\GraphQLite\TypeMappingRuntimeException;
use Webmozart\Assert\Assert;
use function assert;
use function count;
Expand Down Expand Up @@ -165,7 +164,7 @@ private function toGraphQLType(Compound $type, Closure $topToGraphQLType, bool $
}

$unionTypes[] = $topToGraphQLType($iteratorType, $subGraphQlType);
} catch (TypeMappingRuntimeException | CannotMapTypeExceptionInterface $e) {
} catch (CannotMapTypeExceptionInterface $e) {
// We have several types. It is ok not to be able to match one.
$lastException = $e;

Expand All @@ -183,7 +182,7 @@ private function toGraphQLType(Compound $type, Closure $topToGraphQLType, bool $
// We have an issue, let's try without the subType
try {
$result = $topToGraphQLType($iteratorType, null);
} catch (TypeMappingRuntimeException | CannotMapTypeExceptionInterface $otherException) {
} catch (CannotMapTypeExceptionInterface $otherException) {
// Still an issue? Let's rethrow the previous exception.
throw $lastException;
}
Expand Down
6 changes: 3 additions & 3 deletions src/Mappers/Root/NullableTypeMapperAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use phpDocumentor\Reflection\Types\Null_;
use phpDocumentor\Reflection\Types\Nullable;
use ReflectionMethod;
use TheCodingMachine\GraphQLite\TypeMappingRuntimeException;
use TheCodingMachine\GraphQLite\Mappers\CannotMapTypeException;
use TheCodingMachine\GraphQLite\Types\ResolvableMutableInputObjectType;
use function array_filter;
use function array_map;
Expand Down Expand Up @@ -50,7 +50,7 @@ public function toGraphQLOutputType(Type $type, ?OutputType $subType, Reflection
if ($isNullable) {
$nonNullableType = $this->getNonNullable($type);
if ($nonNullableType === null) {
throw TypeMappingRuntimeException::createFromType($type);
throw CannotMapTypeException::createForNull();
}
$type = $nonNullableType;
}
Expand All @@ -77,7 +77,7 @@ public function toGraphQLInputType(Type $type, ?InputType $subType, string $argu
if ($isNullable) {
$nonNullableType = $this->getNonNullable($type);
if ($nonNullableType === null) {
throw TypeMappingRuntimeException::createFromType($type);
throw CannotMapTypeException::createForNull();
}
$type = $nonNullableType;
}
Expand Down
125 changes: 0 additions & 125 deletions src/TypeMappingRuntimeException.php

This file was deleted.

0 comments on commit dd08619

Please sign in to comment.