-
Notifications
You must be signed in to change notification settings - Fork 101
Allow callable() as field deferring mechanism #739
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
884a5fe
Allow callable() as field deferring mechanism
oprypkhantc c242094
Improve coverage
oprypkhantc 30a4f95
Bump dependencies for proper callable() type support
oprypkhantc 7751070
Revert deferred test, add a separate one for callable
oprypkhantc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,61 @@ | ||
<?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\Callable_; | ||
use ReflectionMethod; | ||
use ReflectionProperty; | ||
use TheCodingMachine\GraphQLite\Mappers\CannotMapTypeException; | ||
|
||
/** | ||
* This mapper maps callable types into their return types, so that fields can defer their execution. | ||
*/ | ||
class CallableTypeMapper implements RootTypeMapperInterface | ||
{ | ||
public function __construct( | ||
private readonly RootTypeMapperInterface $next, | ||
private readonly RootTypeMapperInterface $topRootTypeMapper, | ||
) { | ||
} | ||
|
||
public function toGraphQLOutputType(Type $type, OutputType|null $subType, ReflectionMethod|ReflectionProperty $reflector, DocBlock $docBlockObj): OutputType&GraphQLType | ||
{ | ||
if (! $type instanceof Callable_) { | ||
return $this->next->toGraphQLOutputType($type, $subType, $reflector, $docBlockObj); | ||
} | ||
|
||
if ($type->getParameters()) { | ||
throw CannotMapTypeException::createForUnexpectedCallableParameters(); | ||
} | ||
|
||
$returnType = $type->getReturnType(); | ||
|
||
if (! $returnType) { | ||
throw CannotMapTypeException::createForMissingCallableReturnType(); | ||
} | ||
|
||
return $this->topRootTypeMapper->toGraphQLOutputType($returnType, null, $reflector, $docBlockObj); | ||
} | ||
|
||
public function toGraphQLInputType(Type $type, InputType|null $subType, string $argumentName, ReflectionMethod|ReflectionProperty $reflector, DocBlock $docBlockObj): InputType&GraphQLType | ||
{ | ||
if (! $type instanceof Callable_) { | ||
return $this->next->toGraphQLInputType($type, $subType, $argumentName, $reflector, $docBlockObj); | ||
} | ||
|
||
throw CannotMapTypeException::createForCallableAsInput(); | ||
} | ||
|
||
public function mapNameToType(string $typeName): NamedType&GraphQLType | ||
{ | ||
return $this->next->mapNameToType($typeName); | ||
} | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,131 @@ | ||
<?php | ||
|
||
namespace TheCodingMachine\GraphQLite\Mappers\Root; | ||
|
||
use Generator; | ||
use GraphQL\Type\Definition\InputType; | ||
use GraphQL\Type\Definition\NamedType; | ||
use GraphQL\Type\Definition\NonNull; | ||
use GraphQL\Type\Definition\OutputType; | ||
use GraphQL\Type\Definition\StringType; | ||
use GraphQL\Type\Definition\Type as GraphQLType; | ||
use phpDocumentor\Reflection\DocBlock; | ||
use phpDocumentor\Reflection\Type; | ||
use phpDocumentor\Reflection\Types\Array_; | ||
use phpDocumentor\Reflection\Types\Callable_; | ||
use phpDocumentor\Reflection\Types\CallableParameter; | ||
use phpDocumentor\Reflection\Types\Nullable; | ||
use phpDocumentor\Reflection\Types\Object_; | ||
use phpDocumentor\Reflection\Types\String_; | ||
use PHPUnit\Framework\Attributes\CoversClass; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use ReflectionMethod; | ||
use TheCodingMachine\GraphQLite\AbstractQueryProvider; | ||
use TheCodingMachine\GraphQLite\Fixtures\TestObject; | ||
use TheCodingMachine\GraphQLite\Fixtures\TestObject2; | ||
use TheCodingMachine\GraphQLite\Mappers\CannotMapTypeException; | ||
|
||
#[CoversClass(CallableTypeMapper::class)] | ||
#[CoversClass(CannotMapTypeException::class)] | ||
class CallableTypeMapperTest extends AbstractQueryProvider | ||
{ | ||
public function testMapsCallableReturnTypeUsingTopRootMapper(): void | ||
{ | ||
$reflection = new ReflectionMethod(__CLASS__, 'testSkipsNonCallables'); | ||
$docBlock = new DocBlock(); | ||
|
||
$returnType = new String_(); | ||
|
||
$topRootMapper = $this->createMock(RootTypeMapperInterface::class); | ||
$topRootMapper->expects($this->once()) | ||
->method('toGraphQLOutputType') | ||
->with($returnType, null, $reflection, $docBlock) | ||
->willReturn(GraphQLType::string()); | ||
|
||
$mapper = new CallableTypeMapper( | ||
$this->createMock(RootTypeMapperInterface::class), | ||
$topRootMapper, | ||
); | ||
|
||
$result = $mapper->toGraphQLOutputType(new Callable_(returnType: $returnType), null, $reflection, $docBlock); | ||
|
||
$this->assertSame(GraphQLType::string(), $result); | ||
} | ||
|
||
public function testThrowsWhenUsingCallableWithParameters(): void | ||
{ | ||
$this->expectExceptionObject(CannotMapTypeException::createForUnexpectedCallableParameters()); | ||
|
||
$mapper = new CallableTypeMapper( | ||
$this->createMock(RootTypeMapperInterface::class), | ||
$this->createMock(RootTypeMapperInterface::class) | ||
); | ||
|
||
$type = new Callable_( | ||
parameters: [ | ||
new CallableParameter(new String_()) | ||
] | ||
); | ||
|
||
$mapper->toGraphQLOutputType($type, null, new ReflectionMethod(__CLASS__, 'testSkipsNonCallables'), new DocBlock()); | ||
} | ||
|
||
public function testThrowsWhenUsingCallableWithoutReturnType(): void | ||
{ | ||
$this->expectExceptionObject(CannotMapTypeException::createForMissingCallableReturnType()); | ||
|
||
$mapper = new CallableTypeMapper( | ||
$this->createMock(RootTypeMapperInterface::class), | ||
$this->createMock(RootTypeMapperInterface::class) | ||
); | ||
|
||
$mapper->toGraphQLOutputType(new Callable_(), null, new ReflectionMethod(__CLASS__, 'testSkipsNonCallables'), new DocBlock()); | ||
} | ||
|
||
public function testThrowsWhenUsingCallableAsInputType(): void | ||
{ | ||
$this->expectExceptionObject(CannotMapTypeException::createForCallableAsInput()); | ||
|
||
$mapper = new CallableTypeMapper( | ||
$this->createMock(RootTypeMapperInterface::class), | ||
$this->createMock(RootTypeMapperInterface::class) | ||
); | ||
|
||
$mapper->toGraphQLInputType(new Callable_(), null, 'arg1', new ReflectionMethod(__CLASS__, 'testSkipsNonCallables'), new DocBlock()); | ||
} | ||
|
||
#[DataProvider('skipsNonCallablesProvider')] | ||
public function testSkipsNonCallables(callable $createType): void | ||
{ | ||
$type = $createType(); | ||
$reflection = new ReflectionMethod(__CLASS__, 'testSkipsNonCallables'); | ||
$docBlock = new DocBlock(); | ||
|
||
$next = $this->createMock(RootTypeMapperInterface::class); | ||
$next->expects($this->once()) | ||
->method('toGraphQLOutputType') | ||
->with($type, null, $reflection, $docBlock) | ||
->willReturn(GraphQLType::string()); | ||
$next->expects($this->once()) | ||
->method('toGraphQLInputType') | ||
->with($type, null, 'arg1', $reflection, $docBlock) | ||
->willReturn(GraphQLType::int()); | ||
$next->expects($this->once()) | ||
->method('mapNameToType') | ||
->with('Name') | ||
->willReturn(GraphQLType::float()); | ||
|
||
$mapper = new CallableTypeMapper($next, $this->createMock(RootTypeMapperInterface::class)); | ||
|
||
$this->assertSame(GraphQLType::string(), $mapper->toGraphQLOutputType($type, null, $reflection, $docBlock)); | ||
$this->assertSame(GraphQLType::int(), $mapper->toGraphQLInputType($type, null, 'arg1', $reflection, $docBlock)); | ||
$this->assertSame(GraphQLType::float(), $mapper->mapNameToType('Name')); | ||
} | ||
|
||
public static function skipsNonCallablesProvider(): iterable | ||
{ | ||
yield [fn () => new Object_()]; | ||
yield [fn () => new Array_()]; | ||
yield [fn () => new String_()]; | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.