Skip to content

Commit

Permalink
fix for default value of arguments of fields and fields in input objects
Browse files Browse the repository at this point in the history
  • Loading branch information
bpteam committed Nov 19, 2023
1 parent ea777d4 commit 8f4227d
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 49 deletions.
13 changes: 10 additions & 3 deletions src/Generator/TypeRegistry/Foundation/FieldArgumentGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,18 @@ public function __construct(

public function generate(Argument $argument): string
{
$description = '';
if (isset($argument->description)) {
$description = "\n 'description' => {$this->serializer->serialize($argument->description)},";
}

$defaultValue = '';
if (isset($argument->defaultValue)) {
$defaultValue = "\n 'defaultValue' => {$this->serializer->serialize($argument->defaultValue)},";
}
return "[
'name' => {$this->serializer->serialize($argument->name)},
'type' => function() { return {$this->typeDefinitionResolver->getDefinition($argument->getType())}; },
'defaultValue' => {$this->serializer->serialize($argument->defaultValue)},
'description' => {$this->serializer->serialize($argument->description)},
'type' => function() { return {$this->typeDefinitionResolver->getDefinition($argument->getType())}; },{$description}{$defaultValue}
]";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,18 @@ public function __construct(

public function generate(Type $type, InputObjectField $field): string
{
$description = '';
if (isset($field->description)) {
$description = "\n 'description' => {$this->serializer->serialize($field->description)},";
}

$defaultValue = '';
if (isset($field->defaultValue)) {
$defaultValue = "\n 'defaultValue' => {$this->serializer->serialize($field->defaultValue)},";
}

return "[
'name' => {$this->serializer->serialize($field->name)},
'description' => {$this->serializer->serialize($field->description)},
'defaultValue' => {$this->serializer->serialize($field->defaultValue)},
'name' => {$this->serializer->serialize($field->name)},{$description}{$defaultValue}
'type' => {$this->typeDefinitionResolver->getDefinition($field->getType())},
]";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,28 +149,18 @@ function($rootValue, $args, $context, $info) {
'args' => ['x' => [
'name' => 'x',
'type' => function() { return Type::int(); },
'defaultValue' => NULL,
'description' => NULL,
],'testInput' => [
'name' => 'testInput',
'type' => function() { return Type::nonNull(function() { return $this->getType('DemoInput'); }); },
'defaultValue' => NULL,
'description' => NULL,
],'demo' => [
'name' => 'demo',
'type' => function() { return $this->getType('DemoEnum'); },
'defaultValue' => NULL,
'description' => NULL,
],'date' => [
'name' => 'date',
'type' => function() { return $this->getType('DateTime'); },
'defaultValue' => NULL,
'description' => NULL,
],'hello' => [
'name' => 'hello',
'type' => function() { return $this->getType('HelloScalar'); },
'defaultValue' => NULL,
'description' => NULL,
]],
])],
]);
Expand Down Expand Up @@ -207,8 +197,6 @@ public function DemoInput()
'description' => NULL,
'fields' => fn() => ['field' => [
'name' => 'field',
'description' => NULL,
'defaultValue' => NULL,
'type' => Type::int(),
]],
]);
Expand Down Expand Up @@ -263,8 +251,6 @@ public function directive_sum()
[
'name' => 'x',
'type' => function() { return Type::int(); },
'defaultValue' => NULL,
'description' => NULL,
]
],
]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,28 +150,18 @@ function($rootValue, $args, $context, $info) {
'args' => ['x' => [
'name' => 'x',
'type' => function() { return Type::int(); },
'defaultValue' => NULL,
'description' => NULL,
],'testInput' => [
'name' => 'testInput',
'type' => function() { return Type::nonNull(function() { return $this->getType('DemoInput'); }); },
'defaultValue' => NULL,
'description' => NULL,
],'demo' => [
'name' => 'demo',
'type' => function() { return $this->getType('DemoEnum'); },
'defaultValue' => NULL,
'description' => NULL,
],'date' => [
'name' => 'date',
'type' => function() { return $this->getType('DateTime'); },
'defaultValue' => NULL,
'description' => NULL,
],'hello' => [
'name' => 'hello',
'type' => function() { return $this->getType('HelloScalar'); },
'defaultValue' => NULL,
'description' => NULL,
]],
])],
]);
Expand Down Expand Up @@ -208,8 +198,6 @@ public function DemoInput()
'description' => NULL,
'fields' => fn() => ['field' => [
'name' => 'field',
'description' => NULL,
'defaultValue' => NULL,
'type' => Type::int(),
]],
]);
Expand Down Expand Up @@ -264,8 +252,6 @@ public function directive_sum()
[
'name' => 'x',
'type' => function() { return Type::int(); },
'defaultValue' => NULL,
'description' => NULL,
]
],
]);
Expand Down
20 changes: 5 additions & 15 deletions tests/Builder/Psr/Container/TypeRegistryGeneratorBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ public function dataProviderGeneratePhpCode(): iterable
directive @uppercase on FIELD | FIELD_DEFINITION
type NamedCurrency {
id: ID!
name(x: Int, testInput: DemoInput!, demo: DemoEnum, date: DateTime, hello: HelloScalar): String @uppercase @sum(x: 2)
name(
"Description for argument"
x: Int = 5, testInput: DemoInput!, demo: DemoEnum, date: DateTime, hello: HelloScalar): String @uppercase @sum(x: 2)
}
enum DemoEnum {
A
Expand Down Expand Up @@ -145,28 +147,20 @@ function($rootValue, $args, $context, $info) {
'args' => ['x' => [
'name' => 'x',
'type' => function() { return Type::int(); },
'defaultValue' => NULL,
'description' => NULL,
'description' => 'Description for argument',
'defaultValue' => 5,
],'testInput' => [
'name' => 'testInput',
'type' => function() { return Type::nonNull(function() { return $this->getType('DemoInput'); }); },
'defaultValue' => NULL,
'description' => NULL,
],'demo' => [
'name' => 'demo',
'type' => function() { return $this->getType('DemoEnum'); },
'defaultValue' => NULL,
'description' => NULL,
],'date' => [
'name' => 'date',
'type' => function() { return $this->getType('DateTime'); },
'defaultValue' => NULL,
'description' => NULL,
],'hello' => [
'name' => 'hello',
'type' => function() { return $this->getType('HelloScalar'); },
'defaultValue' => NULL,
'description' => NULL,
]],
])],
]);
Expand Down Expand Up @@ -203,8 +197,6 @@ public function DemoInput()
'description' => NULL,
'fields' => fn() => ['field' => [
'name' => 'field',
'description' => NULL,
'defaultValue' => NULL,
'type' => Type::int(),
]],
]);
Expand Down Expand Up @@ -259,8 +251,6 @@ public function directive_sum()
[
'name' => 'x',
'type' => function() { return Type::int(); },
'defaultValue' => NULL,
'description' => NULL,
]
],
]);
Expand Down

0 comments on commit 8f4227d

Please sign in to comment.