From 0311fff41518a6b2b1fadb83960d6e378cffbcf8 Mon Sep 17 00:00:00 2001 From: bpteam Date: Sun, 31 Dec 2023 17:48:08 +0200 Subject: [PATCH] fix auto gernerator of directive with arguments with list of value --- .../Wrapper/FieldResolverDirectiveWrapped.php | 10 +++- .../FieldResolverDirectiveWrappedTest.php | 52 +++++++++++++++++++ .../resources/SumVariantsDirective.php | 18 +++++++ .../resources/SumVariantsDirectiveArgs.php | 9 ++++ 4 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 tests/Generator/TypeRegistry/resources/SumVariantsDirective.php create mode 100644 tests/Generator/TypeRegistry/resources/SumVariantsDirectiveArgs.php diff --git a/src/Generator/TypeRegistry/Foundation/Resolver/Wrapper/FieldResolverDirectiveWrapped.php b/src/Generator/TypeRegistry/Foundation/Resolver/Wrapper/FieldResolverDirectiveWrapped.php index a59adbc..30578fe 100644 --- a/src/Generator/TypeRegistry/Foundation/Resolver/Wrapper/FieldResolverDirectiveWrapped.php +++ b/src/Generator/TypeRegistry/Foundation/Resolver/Wrapper/FieldResolverDirectiveWrapped.php @@ -7,6 +7,8 @@ use Axtiva\FlexibleGraphql\Generator\Config\ArgsDirectiveResolverGeneratorConfigInterface; use GraphQL\Language\AST\ArgumentNode; use GraphQL\Language\AST\DirectiveNode; +use GraphQL\Language\AST\ListValueNode; +use GraphQL\Language\AST\NullValueNode; use GraphQL\Type\Definition\Directive; use GraphQL\Type\Definition\FieldDefinition; use GraphQL\Type\Definition\Type; @@ -62,9 +64,15 @@ public function generate(Type $type, FieldDefinition $field): string if ($this->directiveResolverGenerator->hasResolver($directive)) { $directiveResolver = $this->directiveResolverGenerator->generate($directive); $directiveArguments = []; + $recursive = function ($value) use (&$recursive) { + if ($value instanceof ListValueNode) { + return array_map($recursive, iterator_to_array($value->values->getIterator())); + } + return $value instanceof NullValueNode ? null : $value->value; + }; /** @var ArgumentNode $argument */ foreach ($directive->arguments as $argument) { - $directiveArguments[$argument->name->value] = $argument->value->value; + $directiveArguments[$argument->name->value] = $recursive($argument->value); } $directiveArgs = $this->serializer->serialize($directiveArguments); diff --git a/tests/Generator/TypeRegistry/Resolver/Wrapper/FieldResolverDirectiveWrappedTest.php b/tests/Generator/TypeRegistry/Resolver/Wrapper/FieldResolverDirectiveWrappedTest.php index 6931cfe..af3e445 100644 --- a/tests/Generator/TypeRegistry/Resolver/Wrapper/FieldResolverDirectiveWrappedTest.php +++ b/tests/Generator/TypeRegistry/Resolver/Wrapper/FieldResolverDirectiveWrappedTest.php @@ -259,6 +259,58 @@ function($rootValue, $args, $context, $info) { $rootValue, $args, $context, $info ); } +PHP, + ]; + require_once __DIR__ . '/../../resources/SumVariantsDirective.php'; + require_once __DIR__ . '/../../resources/SumVariantsDirectiveArgs.php'; + yield [ + 'NamedCurrency', + 'name', + CodeGeneratorConfig::V7_4, + BuildSchema::build(Parser::parse(<<container->get('Axtiva\FlexibleGraphql\Example\GraphQL\Directive\SumVariantsDirective')( + function($rootValue, $args, $context, $info) { + return $this->container->get('Axtiva\FlexibleGraphql\Example\GraphQL\Directive\UppercaseDirective')( + (function ($rootValue, $args, $context, $info) { + $args = new \Axtiva\FlexibleGraphql\Example\GraphQL\ResolverArgs\NamedCurrency\NameResolverArgs($args); + return $this->container->get('Axtiva\FlexibleGraphql\Example\GraphQL\Resolver\NamedCurrency\NameResolver')($rootValue, $args, $context, $info); +}), + array ( +), + $rootValue, $args, $context, $info + ); + }, + new \Axtiva\FlexibleGraphql\Example\GraphQL\DirectiveArgs\SumVariantsDirectiveArgs(array ( + 'x' => '2', + 'variants' => + array ( + 0 => '1', + 1 => '2', + 2 => NULL, + 3 => '3', + ), +)), + $rootValue, $args, $context, $info + ); + } PHP, ]; } diff --git a/tests/Generator/TypeRegistry/resources/SumVariantsDirective.php b/tests/Generator/TypeRegistry/resources/SumVariantsDirective.php new file mode 100644 index 0000000..cf4b26b --- /dev/null +++ b/tests/Generator/TypeRegistry/resources/SumVariantsDirective.php @@ -0,0 +1,18 @@ +