Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix non null list of non null of input object #511

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ CHANGELOG

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

2019-10-23, 3.1.0
-----------------
Expand Down
23 changes: 14 additions & 9 deletions src/Support/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@
namespace Rebing\GraphQL\Support;

use Closure;
use Validator;
use Illuminate\Support\Arr;
use GraphQL\Type\Definition\NonNull;
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 as GraphqlType;
use GraphQL\Type\Definition\WrappingType;
use Rebing\GraphQL\Error\ValidationError;
use GraphQL\Type\Definition\InputObjectType;
use Illuminate\Support\Arr;
use Rebing\GraphQL\Error\AuthorizationError;
use GraphQL\Type\Definition\Type as GraphqlType;
use Rebing\GraphQL\Error\ValidationError;
use Validator;

abstract class Field
{
Expand Down Expand Up @@ -118,7 +118,7 @@ public function inferRulesFromType(GraphqlType $type, string $prefix, array $res

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

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

$type = $field->type;
if ($field->type instanceof WrappingType) {
$type = $field->type->getWrappedType(true);
}

// then recursively call the parent method to see if this is an
// input object, passing in the new prefix
if ($field->type instanceof InputObjectType) {
if ($type instanceof InputObjectType) {
// in case the field is a self reference we must not do
// a recursive call as it will never stop
if ($field->type->toString() == $input->toString()) {
if ($type->toString() == $input->toString()) {
continue;
}
}
Expand Down
8 changes: 7 additions & 1 deletion tests/Support/Objects/UpdateExampleMutationWithInputType.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
namespace Rebing\GraphQL\Tests\Support\Objects;

use GraphQL\Type\Definition\Type;
use Rebing\GraphQL\Support\Mutation;
use Rebing\GraphQL\Support\Facades\GraphQL;
use Rebing\GraphQL\Support\Mutation;

class UpdateExampleMutationWithInputType extends Mutation
{
Expand Down Expand Up @@ -59,6 +59,12 @@ public function args(): array
'type' => Type::nonNull(GraphQL::type('ExampleValidationInputObject')),
'rules' => ['required'],
],

'test_with_rules_non_nullable_list_of_non_nullable_input_object' => [
'name' => 'test',
'type' => Type::nonNull(Type::listOf(Type::nonNull(GraphQL::type('ExampleValidationInputObject')))),
],

];
}

Expand Down
7 changes: 6 additions & 1 deletion tests/Unit/MutationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
use Illuminate\Support\Arr;
use Illuminate\Validation\Validator;
use Rebing\GraphQL\Error\ValidationError;
use Rebing\GraphQL\Tests\Support\Objects\ExampleNestedValidationInputObject;
use Rebing\GraphQL\Tests\Support\Objects\ExampleType;
use Rebing\GraphQL\Tests\Support\Objects\ExampleValidationInputObject;
use Rebing\GraphQL\Tests\Support\Objects\ExampleNestedValidationInputObject;
use Rebing\GraphQL\Tests\Support\Objects\UpdateExampleMutationWithInputType;

class MutationTest extends FieldTest
Expand Down Expand Up @@ -58,6 +58,11 @@ public function testGetRules(): void
$this->assertEquals(Arr::get($rules, 'test_with_rules_non_nullable_input_object.nest.email'), ['email']);
$this->assertEquals(Arr::get($rules, 'test_with_rules_non_nullable_input_object.list'), ['required']);
$this->assertEquals(Arr::get($rules, 'test_with_rules_non_nullable_input_object.list.*.email'), ['email']);
$this->assertEquals(Arr::get($rules, 'test_with_rules_non_nullable_list_of_non_nullable_input_object.*.val'), ['required']);
$this->assertEquals(Arr::get($rules, 'test_with_rules_non_nullable_list_of_non_nullable_input_object.*.nest'), ['required']);
$this->assertEquals(Arr::get($rules, 'test_with_rules_non_nullable_list_of_non_nullable_input_object.*.nest.email'), ['email']);
$this->assertEquals(Arr::get($rules, 'test_with_rules_non_nullable_list_of_non_nullable_input_object.*.list'), ['required']);
$this->assertEquals(Arr::get($rules, 'test_with_rules_non_nullable_list_of_non_nullable_input_object.*.list.*.email'), ['email']);
}

/**
Expand Down