Skip to content

Commit

Permalink
Apply @convertEmptyStringsToNull to input fields when used upon fields
Browse files Browse the repository at this point in the history
  • Loading branch information
dennis-koster authored Sep 6, 2024
1 parent 45e1cbb commit 82d0272
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 10 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ You can find and compare releases at the [GitHub release page](https://github.co

## Unreleased

## v6.44.2

### Fixed

- Apply `@convertEmptyStringsToNull` to input fields when used upon fields https://github.com/nuwave/lighthouse/issues/2610

## v6.44.1

### Fixed
Expand Down
19 changes: 9 additions & 10 deletions src/Schema/Directives/ConvertEmptyStringsToNullDirective.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,13 @@ protected function transformArgumentSet(ArgumentSet $argumentSet): ArgumentSet
{
foreach ($argumentSet->arguments as $argument) {
$namedType = $argument->namedType();
if (
$namedType !== null
$argumentValue = $argument->value;

$isNullableStringType = $namedType !== null
&& $namedType->name === ScalarType::STRING
&& ! $namedType->nonNull
) {
$argument->value = $this->sanitize($argument->value);
&& ! $namedType->nonNull;
if ($isNullableStringType || $argumentValue instanceof ArgumentSet) {
$argument->value = $this->sanitize($argumentValue);
}
}

Expand All @@ -63,10 +64,8 @@ protected function transformArgumentSet(ArgumentSet $argumentSet): ArgumentSet
*/
protected function transformLeaf(mixed $value): mixed
{
if ($value === '') {
return null;
}

return $value;
return $value === ''
? null
: $value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -221,4 +221,93 @@ public function testConvertsEmptyStringToNullWithGlobalFieldMiddleware(): void
],
]);
}

public function testConvertsEmptyStringToNullWithFieldDirectiveAndInputType(): void
{
$this->schema = /** @lang GraphQL */ '
type Query {
foo(input: FooInput): FooInputResponse
@convertEmptyStringsToNull
@field(resolver: "Tests\\\Utils\\\Mutations\\\ReturnReceivedInput")
}
input FooInput {
bar: String
}
type FooInputResponse {
input: FooResponse
}
type FooResponse {
bar: String
}
';

$this->graphQL(/** @lang GraphQL */ '
{
foo(input: {
bar: ""
}) {
input {
bar
}
}
}
')->assertExactJson([
'data' => [
'foo' => [
'input' => [
'bar' => null,
],
],
],
]);
}

public function testConvertsEmptyStringToNullWithGlobalFieldMiddlewareAndInputType(): void
{
config(['lighthouse.field_middleware' => [
ConvertEmptyStringsToNullDirective::class,
]]);

$this->schema = /** @lang GraphQL */ '
type Query {
foo(input: FooInput): FooInputResponse
@field(resolver: "Tests\\\Utils\\\Mutations\\\ReturnReceivedInput")
}
input FooInput {
bar: String
}
type FooInputResponse {
input: FooResponse
}
type FooResponse {
bar: String
}
';

$this->graphQL(/** @lang GraphQL */ '
{
foo(input: {
bar: ""
}) {
input {
bar
}
}
}
')->assertExactJson([
'data' => [
'foo' => [
'input' => [
'bar' => null,
],
],
],
]);
}
}

0 comments on commit 82d0272

Please sign in to comment.