diff --git a/src/Transformer/ArgumentsTransformer.php b/src/Transformer/ArgumentsTransformer.php index e8f91aeca..4afa2c82b 100644 --- a/src/Transformer/ArgumentsTransformer.php +++ b/src/Transformer/ArgumentsTransformer.php @@ -147,9 +147,13 @@ public function getInstanceAndValidate(string $argType, $data, ResolveInfo $info $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); + $gqlType = $this->getType($type, $info); + $result = $this->populateObject($gqlType, $data, $isMultiple, $info); - if (null !== $this->validator) { + // We only want to validate input object types and not the scalar or object types that are already validated by the GraphQL library. + $shouldValidate = $gqlType instanceof InputObjectType; + + if (null !== $this->validator && $shouldValidate) { $errors = new ConstraintViolationList(); if (is_object($result)) { $errors = $this->validator->validate($result); diff --git a/tests/Transformer/ArgumentsTransformerTest.php b/tests/Transformer/ArgumentsTransformerTest.php index 843d35735..14db0919b 100644 --- a/tests/Transformer/ArgumentsTransformerTest.php +++ b/tests/Transformer/ArgumentsTransformerTest.php @@ -9,6 +9,7 @@ use GraphQL\Type\Definition\InputObjectType; use GraphQL\Type\Definition\ListOfType; use GraphQL\Type\Definition\NonNull; +use GraphQL\Type\Definition\ObjectType; use GraphQL\Type\Definition\ResolveInfo; use GraphQL\Type\Definition\Type; use GraphQL\Type\Schema; @@ -101,10 +102,17 @@ public static function getTypes(): array ], ]); - $types = [$t1, $t2, $t3, $t4, $t5]; + $t6 = new ObjectType([ + 'name' => 'Type1', + 'fields' => [ + 'field1' => Type::string(), + ], + ]); + + $types = [$t1, $t2, $t3, $t4, $t5, $t6]; if (PHP_VERSION_ID >= 80100) { - $t5 = new PhpEnumType([ + $types[] = new PhpEnumType([ 'name' => 'EnumPhp', 'enumClass' => EnumPhp::class, 'values' => [ @@ -113,7 +121,6 @@ public static function getTypes(): array 'VALUE3' => 'VALUE3', ], ]); - $types[] = $t5; } return $types; @@ -431,4 +438,21 @@ public function testInputObjectWithWrappingTypeList(Type $type): void $this->assertEquals($inputValue->field2, $data['field2']); $this->assertEquals($inputValue->field3, $data['field3']); } + + public function testIgnoreNonInputObjectValidation(): void + { + $validator = $this->createMock(RecursiveValidator::class); + $validator->expects($this->never())->method('validate'); + + $transformer = new ArgumentsTransformer($validator, [ + 'Type1' => ['type' => 'object', 'class' => Type1::class], + ]); + + $info = $this->getResolveInfo(self::getTypes()); + $data = new Type1(); + + $typeValue = $transformer->getInstanceAndValidate('Type1', $data, $info, 'type1'); + + $this->assertEquals($data, $typeValue); + } } diff --git a/tests/Transformer/Type1.php b/tests/Transformer/Type1.php new file mode 100644 index 000000000..8c3ac9c08 --- /dev/null +++ b/tests/Transformer/Type1.php @@ -0,0 +1,13 @@ +