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

Handle null being passed to a nullable argument that is a list of type #1016

Merged
merged 5 commits into from
Oct 16, 2019
Merged
Show file tree
Hide file tree
Changes from 4 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
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased](https://github.com/nuwave/lighthouse/compare/v4.5.0...master)
## [Unreleased](https://github.com/nuwave/lighthouse/compare/v4.5.1...master)

## [4.5.1](https://github.com/nuwave/lighthouse/compare/v4.5.0...v4.5.1)

### Fixed

- Handle `null` being passed to a nullable argument that is a list of type https://github.com/nuwave/lighthouse/pull/1016

## [4.5.0](https://github.com/nuwave/lighthouse/compare/v4.4.2...v4.5.0)

Expand Down
15 changes: 10 additions & 5 deletions src/Execution/Arguments/TypedArgs.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,17 @@ protected function wrapWithType($valueOrValues, $type)
if ($type instanceof ListType) {
$typeInList = $type->type;

$values = [];
foreach ($valueOrValues as $singleValue) {
$values [] = $this->wrapWithNamedType($singleValue, $typeInList);
if (is_array($valueOrValues)) {
$values = [];
foreach ($valueOrValues as $singleValue) {
$values [] = $this->wrapWithNamedType($singleValue, $typeInList);
}

return $values;
} else {
// This case happens if `null` is passed
return $this->wrapWithNamedType($valueOrValues, $typeInList);
}

return $values;
} else {
return $this->wrapWithNamedType($valueOrValues, $type);
}
Expand Down
31 changes: 31 additions & 0 deletions tests/Unit/Execution/Arguments/TypedArgsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,35 @@ public function testSimpleField(): void
$this->assertInstanceOf(Argument::class, $bar);
$this->assertSame(123, $bar->value);
}

public function testNullableList(): void
{
$this->schema = '
type Query {
foo(bar: [Int!]): Int
}
';

/** @var \Nuwave\Lighthouse\Schema\AST\ASTBuilder $astBuilder */
$astBuilder = $this->app->make(ASTBuilder::class);
$documentAST = $astBuilder->documentAST();
/** @var \Nuwave\Lighthouse\Execution\Arguments\TypedArgs $typedArgs */
$typedArgs = $this->app->make(TypedArgs::class);

/** @var \GraphQL\Language\AST\ObjectTypeDefinitionNode $queryType */
$queryType = $documentAST->types['Query'];

$argumentSet = $typedArgs->fromField(
[
'bar' => null,
],
ASTHelper::firstByName($queryType->fields, 'foo')
);

$this->assertCount(1, $argumentSet->arguments);

$bar = $argumentSet->arguments['bar'];
$this->assertInstanceOf(Argument::class, $bar);
$this->assertSame(null, $bar->value);
}
}