Skip to content

Commit

Permalink
fix auto gernerator of directive with arguments with list of value
Browse files Browse the repository at this point in the history
  • Loading branch information
bpteam committed Dec 31, 2023
1 parent b3c678b commit 0311fff
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(<<<GQL
directive @sumVariants(x: Int, variants: [Int]) on FIELD | FIELD_DEFINITION
directive @uppercase on FIELD | FIELD_DEFINITION
type NamedCurrency {
id: ID!
name(x: Int, testInput: DemoInput!, demo: DemoEnum, date: DateTime, hello: HelloScalar): String @uppercase @sumVariants(x: 2, variants: [1,2,null,3])
}
enum DemoEnum {
A
B
}
input DemoInput {
field: Int
}
scalar DateTime
scalar HelloScalar
GQL)),
<<<'PHP'
function($rootValue, $args, $context, $info) {
return $this->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,
];
}
Expand Down
18 changes: 18 additions & 0 deletions tests/Generator/TypeRegistry/resources/SumVariantsDirective.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare (strict_types=1);
namespace Axtiva\FlexibleGraphql\Example\GraphQL\Directive;

use GraphQL\Type\Definition\ResolveInfo;
use Axtiva\FlexibleGraphql\Resolver\DirectiveResolverInterface;

/**
* This code is @generated by axtiva/flexible-graphql-php
* Resolver for executable directive @sum
* CAPITALIZE ALL LETTERS IN STRING
*/
final class SumVariantsDirective implements DirectiveResolverInterface
{
function __invoke(callable $next, $directiveArgs, $rootValue, $args, $context, ResolveInfo $info)
{}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare (strict_types=1);
namespace Axtiva\FlexibleGraphql\Example\GraphQL\DirectiveArgs;


final class SumVariantsDirectiveArgs
{
}

0 comments on commit 0311fff

Please sign in to comment.