Skip to content

Commit e143f5d

Browse files
authored
Merge pull request #806 from dmitry-hordinsky/hotfix/annotation-for-non-nullable-array-of-non-nullable-elements
Fix annotation for non-nullable array of non-nullable values as query argument
2 parents 674c0d2 + 8c5a472 commit e143f5d

File tree

2 files changed

+80
-1
lines changed

2 files changed

+80
-1
lines changed

src/Transformer/ArgumentsTransformer.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,12 @@ public function getInstanceAndValidate(string $argType, $data, ResolveInfo $info
132132
{
133133
$isRequired = '!' === $argType[strlen($argType) - 1];
134134
$isMultiple = '[' === $argType[0];
135-
$endIndex = ($isRequired ? 1 : 0) + ($isMultiple ? 1 : 0);
135+
$isStrictMultiple = false;
136+
if ($isMultiple) {
137+
$isStrictMultiple = '!' === $argType[strpos($argType, ']') - 1];
138+
}
139+
140+
$endIndex = ($isRequired ? 1 : 0) + ($isMultiple ? 1 : 0) + ($isStrictMultiple ? 1 : 0);
136141
$type = substr($argType, $isMultiple ? 1 : 0, $endIndex > 0 ? -$endIndex : strlen($argType));
137142

138143
$result = $this->populateObject($this->getType($type, $info), $data, $isMultiple, $info);

tests/Transformer/ArgumentsTransformerTest.php

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@
55
namespace Overblog\GraphQLBundle\Tests\Transformer;
66

77
use Exception;
8+
use Generator;
89
use GraphQL\Type\Definition\EnumType;
910
use GraphQL\Type\Definition\InputObjectType;
11+
use GraphQL\Type\Definition\ListOfType;
12+
use GraphQL\Type\Definition\NonNull;
1013
use GraphQL\Type\Definition\ResolveInfo;
1114
use GraphQL\Type\Definition\Type;
1215
use GraphQL\Type\Schema;
@@ -300,4 +303,75 @@ public function testRaisedErrorsForMultipleInputs(): void
300303
$this->assertEquals($e->toState(), $expected);
301304
}
302305
}
306+
307+
public function getWrappedInputObject(): Generator
308+
{
309+
$inputObject = new InputObjectType([
310+
'name' => 'InputType1',
311+
'fields' => [
312+
'field1' => Type::string(),
313+
'field2' => Type::int(),
314+
'field3' => Type::boolean(),
315+
],
316+
]);
317+
yield [$inputObject, false];
318+
yield [new NonNull($inputObject), false];
319+
}
320+
321+
/** @dataProvider getWrappedInputObject */
322+
public function testInputObjectWithWrappingType(Type $type): void
323+
{
324+
$transformer = $this->getTransformer([
325+
'InputType1' => ['type' => 'input', 'class' => InputType1::class],
326+
], new ConstraintViolationList()
327+
);
328+
$info = $this->getResolveInfo(self::getTypes());
329+
330+
$data = ['field1' => 'hello', 'field2' => 12, 'field3' => true];
331+
332+
$inputValue = $transformer->getInstanceAndValidate($type->toString(), $data, $info, 'input1');
333+
334+
/** @var InputType1 $inputValue */
335+
$this->assertInstanceOf(InputType1::class, $inputValue);
336+
$this->assertEquals($inputValue->field1, $data['field1']);
337+
$this->assertEquals($inputValue->field2, $data['field2']);
338+
$this->assertEquals($inputValue->field3, $data['field3']);
339+
}
340+
341+
public function getWrappedInputObjectList(): Generator
342+
{
343+
$inputObject = new InputObjectType([
344+
'name' => 'InputType1',
345+
'fields' => [
346+
'field1' => Type::string(),
347+
'field2' => Type::int(),
348+
'field3' => Type::boolean(),
349+
],
350+
]);
351+
yield [new ListOfType($inputObject)];
352+
yield [new ListOfType(new NonNull($inputObject))];
353+
yield [new NonNull(new ListOfType($inputObject))];
354+
yield [new NonNull(new ListOfType(new NonNull($inputObject)))];
355+
}
356+
357+
/** @dataProvider getWrappedInputObjectList */
358+
public function testInputObjectWithWrappingTypeList(Type $type): void
359+
{
360+
$transformer = $this->getTransformer(
361+
['InputType1' => ['type' => 'input', 'class' => InputType1::class]],
362+
new ConstraintViolationList()
363+
);
364+
$info = $this->getResolveInfo(self::getTypes());
365+
366+
$data = ['field1' => 'hello', 'field2' => 12, 'field3' => true];
367+
368+
$inputValue = $transformer->getInstanceAndValidate($type->toString(), [$data], $info, 'input1');
369+
$inputValue = reset($inputValue);
370+
371+
/** @var InputType1 $inputValue */
372+
$this->assertInstanceOf(InputType1::class, $inputValue);
373+
$this->assertEquals($inputValue->field1, $data['field1']);
374+
$this->assertEquals($inputValue->field2, $data['field2']);
375+
$this->assertEquals($inputValue->field3, $data['field3']);
376+
}
303377
}

0 commit comments

Comments
 (0)