Skip to content

Commit

Permalink
Fix error for non-nullable array of non-nullable elements annotation
Browse files Browse the repository at this point in the history
  • Loading branch information
gordinskiy committed Jan 12, 2021
1 parent 674c0d2 commit 8c5a472
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/Transformer/ArgumentsTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,12 @@ public function getInstanceAndValidate(string $argType, $data, ResolveInfo $info
{
$isRequired = '!' === $argType[strlen($argType) - 1];
$isMultiple = '[' === $argType[0];
$endIndex = ($isRequired ? 1 : 0) + ($isMultiple ? 1 : 0);
$isStrictMultiple = false;
if ($isMultiple) {
$isStrictMultiple = '!' === $argType[strpos($argType, ']') - 1];
}

$endIndex = ($isRequired ? 1 : 0) + ($isMultiple ? 1 : 0) + ($isStrictMultiple ? 1 : 0);
$type = substr($argType, $isMultiple ? 1 : 0, $endIndex > 0 ? -$endIndex : strlen($argType));

$result = $this->populateObject($this->getType($type, $info), $data, $isMultiple, $info);
Expand Down
74 changes: 74 additions & 0 deletions tests/Transformer/ArgumentsTransformerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
namespace Overblog\GraphQLBundle\Tests\Transformer;

use Exception;
use Generator;
use GraphQL\Type\Definition\EnumType;
use GraphQL\Type\Definition\InputObjectType;
use GraphQL\Type\Definition\ListOfType;
use GraphQL\Type\Definition\NonNull;
use GraphQL\Type\Definition\ResolveInfo;
use GraphQL\Type\Definition\Type;
use GraphQL\Type\Schema;
Expand Down Expand Up @@ -300,4 +303,75 @@ public function testRaisedErrorsForMultipleInputs(): void
$this->assertEquals($e->toState(), $expected);
}
}

public function getWrappedInputObject(): Generator
{
$inputObject = new InputObjectType([
'name' => 'InputType1',
'fields' => [
'field1' => Type::string(),
'field2' => Type::int(),
'field3' => Type::boolean(),
],
]);
yield [$inputObject, false];
yield [new NonNull($inputObject), false];
}

/** @dataProvider getWrappedInputObject */
public function testInputObjectWithWrappingType(Type $type): void
{
$transformer = $this->getTransformer([
'InputType1' => ['type' => 'input', 'class' => InputType1::class],
], new ConstraintViolationList()
);
$info = $this->getResolveInfo(self::getTypes());

$data = ['field1' => 'hello', 'field2' => 12, 'field3' => true];

$inputValue = $transformer->getInstanceAndValidate($type->toString(), $data, $info, 'input1');

/** @var InputType1 $inputValue */
$this->assertInstanceOf(InputType1::class, $inputValue);
$this->assertEquals($inputValue->field1, $data['field1']);
$this->assertEquals($inputValue->field2, $data['field2']);
$this->assertEquals($inputValue->field3, $data['field3']);
}

public function getWrappedInputObjectList(): Generator
{
$inputObject = new InputObjectType([
'name' => 'InputType1',
'fields' => [
'field1' => Type::string(),
'field2' => Type::int(),
'field3' => Type::boolean(),
],
]);
yield [new ListOfType($inputObject)];
yield [new ListOfType(new NonNull($inputObject))];
yield [new NonNull(new ListOfType($inputObject))];
yield [new NonNull(new ListOfType(new NonNull($inputObject)))];
}

/** @dataProvider getWrappedInputObjectList */
public function testInputObjectWithWrappingTypeList(Type $type): void
{
$transformer = $this->getTransformer(
['InputType1' => ['type' => 'input', 'class' => InputType1::class]],
new ConstraintViolationList()
);
$info = $this->getResolveInfo(self::getTypes());

$data = ['field1' => 'hello', 'field2' => 12, 'field3' => true];

$inputValue = $transformer->getInstanceAndValidate($type->toString(), [$data], $info, 'input1');
$inputValue = reset($inputValue);

/** @var InputType1 $inputValue */
$this->assertInstanceOf(InputType1::class, $inputValue);
$this->assertEquals($inputValue->field1, $data['field1']);
$this->assertEquals($inputValue->field2, $data['field2']);
$this->assertEquals($inputValue->field3, $data['field3']);
}
}

0 comments on commit 8c5a472

Please sign in to comment.