Skip to content

Commit

Permalink
Handle null being passed to a nullable argument that is a list of t…
Browse files Browse the repository at this point in the history
…ype (#1016)
  • Loading branch information
spawnia authored Oct 16, 2019
1 parent 1d68cba commit 9dba7fd
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 7 deletions.
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
17 changes: 11 additions & 6 deletions src/Execution/Arguments/TypedArgs.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,15 +145,20 @@ 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;
}

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

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);
}
}

0 comments on commit 9dba7fd

Please sign in to comment.