Skip to content

Commit c747b38

Browse files
authored
Merge pull request #511 from crissi/fix_non_null_list_of_non_null_input_object_rules
fix non null list of non null of input object
2 parents 62d47d2 + 5c19853 commit c747b38

File tree

4 files changed

+29
-11
lines changed

4 files changed

+29
-11
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ CHANGELOG
33

44
[Next release](https://github.com/rebing/graphql-laravel/compare/3.1.0...master)
55
--------------
6+
### Fixed
7+
- Fix validation rules for non-null list of non-null objects [\#511 / crissi](https://github.com/rebing/graphql-laravel/pull/511/files)
68

79
2019-10-23, 3.1.0
810
-----------------

src/Support/Field.php

+14-9
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@
55
namespace Rebing\GraphQL\Support;
66

77
use Closure;
8-
use Validator;
9-
use Illuminate\Support\Arr;
10-
use GraphQL\Type\Definition\NonNull;
8+
use GraphQL\Type\Definition\InputObjectType;
119
use GraphQL\Type\Definition\ListOfType;
10+
use GraphQL\Type\Definition\NonNull;
1211
use GraphQL\Type\Definition\ResolveInfo;
12+
use GraphQL\Type\Definition\Type as GraphqlType;
1313
use GraphQL\Type\Definition\WrappingType;
14-
use Rebing\GraphQL\Error\ValidationError;
15-
use GraphQL\Type\Definition\InputObjectType;
14+
use Illuminate\Support\Arr;
1615
use Rebing\GraphQL\Error\AuthorizationError;
17-
use GraphQL\Type\Definition\Type as GraphqlType;
16+
use Rebing\GraphQL\Error\ValidationError;
17+
use Validator;
1818

1919
abstract class Field
2020
{
@@ -118,7 +118,7 @@ public function inferRulesFromType(GraphqlType $type, string $prefix, array $res
118118

119119
// make sure we are dealing with the actual type
120120
if ($type instanceof WrappingType) {
121-
$type = $type->getWrappedType();
121+
$type = $type->getWrappedType(true);
122122
}
123123

124124
// if it is an input object type - the only type we care about here...
@@ -144,12 +144,17 @@ public function getInputTypeRules(InputObjectType $input, string $prefix, array
144144
$rules[$key] = $this->resolveRules($field->rules, $resolutionArguments);
145145
}
146146

147+
$type = $field->type;
148+
if ($field->type instanceof WrappingType) {
149+
$type = $field->type->getWrappedType(true);
150+
}
151+
147152
// then recursively call the parent method to see if this is an
148153
// input object, passing in the new prefix
149-
if ($field->type instanceof InputObjectType) {
154+
if ($type instanceof InputObjectType) {
150155
// in case the field is a self reference we must not do
151156
// a recursive call as it will never stop
152-
if ($field->type->toString() == $input->toString()) {
157+
if ($type->toString() == $input->toString()) {
153158
continue;
154159
}
155160
}

tests/Support/Objects/UpdateExampleMutationWithInputType.php

+7-1
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
namespace Rebing\GraphQL\Tests\Support\Objects;
66

77
use GraphQL\Type\Definition\Type;
8-
use Rebing\GraphQL\Support\Mutation;
98
use Rebing\GraphQL\Support\Facades\GraphQL;
9+
use Rebing\GraphQL\Support\Mutation;
1010

1111
class UpdateExampleMutationWithInputType extends Mutation
1212
{
@@ -59,6 +59,12 @@ public function args(): array
5959
'type' => Type::nonNull(GraphQL::type('ExampleValidationInputObject')),
6060
'rules' => ['required'],
6161
],
62+
63+
'test_with_rules_non_nullable_list_of_non_nullable_input_object' => [
64+
'name' => 'test',
65+
'type' => Type::nonNull(Type::listOf(Type::nonNull(GraphQL::type('ExampleValidationInputObject')))),
66+
],
67+
6268
];
6369
}
6470

tests/Unit/MutationTest.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
use Illuminate\Support\Arr;
88
use Illuminate\Validation\Validator;
99
use Rebing\GraphQL\Error\ValidationError;
10+
use Rebing\GraphQL\Tests\Support\Objects\ExampleNestedValidationInputObject;
1011
use Rebing\GraphQL\Tests\Support\Objects\ExampleType;
1112
use Rebing\GraphQL\Tests\Support\Objects\ExampleValidationInputObject;
12-
use Rebing\GraphQL\Tests\Support\Objects\ExampleNestedValidationInputObject;
1313
use Rebing\GraphQL\Tests\Support\Objects\UpdateExampleMutationWithInputType;
1414

1515
class MutationTest extends FieldTest
@@ -58,6 +58,11 @@ public function testGetRules(): void
5858
$this->assertEquals(Arr::get($rules, 'test_with_rules_non_nullable_input_object.nest.email'), ['email']);
5959
$this->assertEquals(Arr::get($rules, 'test_with_rules_non_nullable_input_object.list'), ['required']);
6060
$this->assertEquals(Arr::get($rules, 'test_with_rules_non_nullable_input_object.list.*.email'), ['email']);
61+
$this->assertEquals(Arr::get($rules, 'test_with_rules_non_nullable_list_of_non_nullable_input_object.*.val'), ['required']);
62+
$this->assertEquals(Arr::get($rules, 'test_with_rules_non_nullable_list_of_non_nullable_input_object.*.nest'), ['required']);
63+
$this->assertEquals(Arr::get($rules, 'test_with_rules_non_nullable_list_of_non_nullable_input_object.*.nest.email'), ['email']);
64+
$this->assertEquals(Arr::get($rules, 'test_with_rules_non_nullable_list_of_non_nullable_input_object.*.list'), ['required']);
65+
$this->assertEquals(Arr::get($rules, 'test_with_rules_non_nullable_list_of_non_nullable_input_object.*.list.*.email'), ['email']);
6166
}
6267

6368
/**

0 commit comments

Comments
 (0)