Skip to content

Commit

Permalink
Merge pull request #13 from andrey-mokhov/fix-for-query-mutation-addi…
Browse files Browse the repository at this point in the history
…tional

fix: couldn't use several attributes for one method with QueryField, MutationField or several AdditionalField
  • Loading branch information
andrey-mokhov committed Feb 14, 2024
2 parents 4b95970 + bba7d42 commit 8dd5df9
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 16 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"type": "library",
"require": {
"php": "^8.1",
"andi-lab/graphql-php": "^1.0",
"andi-lab/graphql-php": "^1.0.1",
"spiral/framework": "^3.8.4"
},
"license": "MIT",
Expand Down
11 changes: 5 additions & 6 deletions src/Listener/AbstractAdditionalFieldListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@
namespace Andi\GraphQL\Spiral\Listener;

use Andi\GraphQL\Attribute\AbstractField;
use Andi\GraphQL\Common\ReflectionMethodWithAttribute;
use Andi\GraphQL\TypeRegistryInterface;
use ReflectionClass;
use ReflectionMethod;
use Spiral\Attributes\ReaderInterface;
use Spiral\Tokenizer\TokenizationListenerInterface;

abstract class AbstractAdditionalFieldListener implements TokenizationListenerInterface
{
/**
* @var array<ReflectionMethod>
* @var array<ReflectionMethodWithAttribute>
*/
protected array $methods = [];

Expand All @@ -27,7 +26,7 @@ public function __construct(
) {
}

public function listen(ReflectionClass $class): void
public function listen(\ReflectionClass $class): void
{
if ($class->isAbstract() || $class->isEnum() || $class->isTrait()) {
return;
Expand All @@ -38,8 +37,8 @@ public function listen(ReflectionClass $class): void
continue;
}

if (null !== $this->reader->firstFunctionMetadata($method, $this->attribute)) {
$this->methods[] = $method;
if ($attribute = $this->reader->firstFunctionMetadata($method, $this->attribute)) {
$this->methods[] = new ReflectionMethodWithAttribute($method, $attribute);
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/Listener/AdditionalFieldListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Andi\GraphQL\Spiral\Listener;

use Andi\GraphQL\Attribute\AdditionalField;
use Andi\GraphQL\Common\ReflectionMethodWithAttribute;
use Andi\GraphQL\Type\DynamicObjectTypeInterface;
use Spiral\Tokenizer\Attribute\TargetAttribute;

Expand All @@ -16,11 +17,11 @@ final class AdditionalFieldListener extends AbstractAdditionalFieldListener
public function finalize(): void
{
foreach ($this->methods as $method) {
foreach ($this->reader->getFunctionMetadata($method, $this->attribute) as $attribute) {
foreach ($this->reader->getFunctionMetadata($method->method, $this->attribute) as $attribute) {
$type = $this->typeRegistry->get($attribute->targetType);

if ($type instanceof DynamicObjectTypeInterface) {
$type->addAdditionalField($method);
$type->addAdditionalField(new ReflectionMethodWithAttribute($method->method, $attribute));
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions tests/Feature/GraphQLTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,14 @@ public static function getData(): iterable
yield 'query echo' => [
'expected' => [
'data' => [
'echo' => 'echo: hello',
'queryEcho' => 'echo: hello',
],
],
'data' => [
'operationName' => null,
'variables' => new \stdClass(),
'query' => <<<'QUERY'
{echo(message:"hello")}
{queryEcho(message:"hello")}
QUERY,
],
];
Expand All @@ -135,7 +135,7 @@ public static function getData(): iterable
'operationName' => null,
'variables' => ['mess' => 'foo'],
'query' => <<<'QUERY'
mutation tst($mess: String!) {tmp: echo(message:$mess)}
mutation tst($mess: String!) {tmp: mutationEcho(message:$mess)}
QUERY,
],
];
Expand Down
5 changes: 3 additions & 2 deletions tests/Unit/Listener/AttributedTypeLoaderListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use Andi\GraphQL\Spiral\Listener\AttributedTypeLoaderListener;
use Andi\GraphQL\TypeRegistry;
use Andi\GraphQL\TypeRegistryInterface;
use Andi\GraphQL\TypeResolver\CantResolveGraphQLTypeResolver;
use Andi\GraphQL\TypeResolver\Middleware\AttributedGraphQLTypeMiddleware;
use Andi\GraphQL\TypeResolver\Middleware\EnumTypeMiddleware;
use Andi\GraphQL\TypeResolver\TypeResolver;
Expand Down Expand Up @@ -40,7 +39,9 @@ protected function setUp(): void
$container = new Container();
$reader = new NativeAttributeReader();

$this->typeRegistry = new TypeRegistry();
$typeRegistry = $this->typeRegistry = new TypeRegistry();

$container->bindSingleton(TypeRegistryInterface::class, static fn () => $typeRegistry);

$typeResolver = new TypeResolver();
$typeResolver->pipe(new EnumTypeMiddleware($reader));
Expand Down
4 changes: 2 additions & 2 deletions tests/dirs/app/src/SimpleService.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
*/
final class SimpleService
{
#[QueryField(name: 'echo')]
#[MutationField(name: 'echo')]
#[QueryField(name: 'queryEcho')]
#[MutationField(name: 'mutationEcho')]
public function echoMessage(#[Argument] string $message): string
{
return 'echo: ' . $message;
Expand Down

0 comments on commit 8dd5df9

Please sign in to comment.