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 nullable input objects in TypedArgs conversion #1021

Merged
merged 7 commits into from
Oct 21, 2019
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
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.2...master)
## [Unreleased](https://github.com/nuwave/lighthouse/compare/v4.5.3...master)

## [4.5.3](https://github.com/nuwave/lighthouse/compare/v4.5.2...v4.5.3)

### Fixed

- Handle `null` being passed to a nullable argument that is an input object type https://github.com/nuwave/lighthouse/pull/1021

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

Expand Down
11 changes: 8 additions & 3 deletions src/Execution/Arguments/TypedArgs.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,11 @@ protected function wrapWithType($valueOrValues, $type)
*/
protected function wrapWithNamedType($value, NamedType $namedType)
{
// As GraphQL does not allow empty input objects, we return null as is
if ($value === null) {
return;
}

// This might be null if the type is
// - created outside of the schema string
// - one of the built in types
Expand All @@ -193,9 +198,9 @@ protected function wrapWithNamedType($value, NamedType $namedType)
$subArgumentSet->arguments = $this->wrapArgs($value, $typeDef->fields);

return $subArgumentSet;
} else {
// Otherwise, we just return the value as is and are down with that subtree
return $value;
}

// Otherwise, we just return the value as is and are down with that subtree
return $value;
}
}
67 changes: 42 additions & 25 deletions tests/Unit/Execution/Arguments/TypedArgsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Nuwave\Lighthouse\Schema\AST\ASTBuilder;
use Nuwave\Lighthouse\Execution\Arguments\Argument;
use Nuwave\Lighthouse\Execution\Arguments\TypedArgs;
use Nuwave\Lighthouse\Execution\Arguments\ArgumentSet;

class TypedArgsTest extends TestCase
{
Expand All @@ -18,21 +19,9 @@ public function testSimpleField(): void
}
';

/** @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' => 123,
],
ASTHelper::firstByName($queryType->fields, 'foo')
);
$argumentSet = $this->rootQueryArgumentSet([
'bar' => 123,
]);

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

Expand All @@ -49,6 +38,42 @@ public function testNullableList(): void
}
';

$argumentSet = $this->rootQueryArgumentSet([
'bar' => null,
]);

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

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

public function testNullableInputObject(): void
{
$this->schema = '
type Query {
foo(bar: Bar): Int
}

input Bar {
baz: ID
}
';

$argumentSet = $this->rootQueryArgumentSet([
'bar' => null,
]);

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

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

protected function rootQueryArgumentSet(array $args): ArgumentSet
{
/** @var \Nuwave\Lighthouse\Schema\AST\ASTBuilder $astBuilder */
$astBuilder = $this->app->make(ASTBuilder::class);
$documentAST = $astBuilder->documentAST();
Expand All @@ -58,17 +83,9 @@ public function testNullableList(): void
/** @var \GraphQL\Language\AST\ObjectTypeDefinitionNode $queryType */
$queryType = $documentAST->types['Query'];

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

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

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