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 renaming input fields that are nested within lists using @rename #1166

Merged
merged 3 commits into from
Jan 23, 2020
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Fix eager-loading relations where the parent type is an `interface` or `union` and
may correspond to multiple different models https://github.com/nuwave/lighthouse/pull/1035
- Fix renaming input fields that are nested within lists using @rename https://github.com/nuwave/lighthouse/pull/1166

## [4.8.1](https://github.com/nuwave/lighthouse/compare/v4.8.1...4.8.0)

Expand Down
18 changes: 14 additions & 4 deletions src/Execution/Arguments/ArgumentSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Nuwave\Lighthouse\Schema\Directives\SpreadDirective;
use Nuwave\Lighthouse\Support\Contracts\ArgBuilderDirective;
use Nuwave\Lighthouse\Support\Contracts\Directive;
use Nuwave\Lighthouse\Support\Utils;

class ArgumentSet
{
Expand Down Expand Up @@ -87,10 +88,19 @@ public function rename(): self
$argumentSet->directives = $this->directives;

foreach ($this->arguments as $name => $argument) {
// Recursively apply the renaming to nested inputs
if ($argument->value instanceof self) {
$argument->value = $argument->value->rename();
}
// Recursively apply the renaming to nested inputs.
// We look for further ArgumentSet instances, they
// might be contained within an array.
$argument->value = Utils::applyEach(
function ($value) {
if ($value instanceof self) {
return $value->rename();
}

return $value;
},
$argument->value
);

/** @var \Nuwave\Lighthouse\Schema\Directives\RenameDirective|null $renameDirective */
$renameDirective = $argument->directives->first(function ($directive) {
Expand Down
39 changes: 39 additions & 0 deletions tests/Unit/Schema/Directives/RenameDirectiveTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,43 @@ public function testRenameArgument(): void
],
]);
}

public function testRenameListOfInputs(): void
{
$this->mockResolver(function ($root, array $args) {
return $args === [
'input' => [
['bar' => 'something'],
],
];
});

$this->schema = /** @lang GraphQL */ '
type Query {
foo(
input: [FooInput]
): Boolean @mock
}

input FooInput {
baz: String @rename(attribute: "bar")
}
';

$this->graphQL(/** @lang GraphQL */ '
{
foo(
input: [
{
baz: "something"
}
]
)
}
')->assertJson([
'data' => [
'foo' => true,
],
]);
}
}