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

Restore correct function of nested HasMany and MorphOne mutations #2591

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
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.42.2

### Fixed

- Restore correct function of nested `HasMany` and `MorphOne` mutations https://github.com/nuwave/lighthouse/pull/2591

## v6.42.1

### Changed
Expand Down
6 changes: 3 additions & 3 deletions src/Execution/Arguments/NestedOneToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function __invoke($model, $args): void
assert($relation instanceof HasMany || $relation instanceof MorphMany);

if ($args->has('create')) {
$saveModel = new ResolveNested(new SaveModel());
$saveModel = new ResolveNested(new SaveModel($relation));

foreach ($args->arguments['create']->value as $childArgs) {
// @phpstan-ignore-next-line Relation&Builder mixin not recognized
Expand All @@ -31,7 +31,7 @@ public function __invoke($model, $args): void
}

if ($args->has('update')) {
$updateModel = new ResolveNested(new UpdateModel(new SaveModel()));
$updateModel = new ResolveNested(new UpdateModel(new SaveModel($relation)));

foreach ($args->arguments['update']->value as $childArgs) {
// @phpstan-ignore-next-line Relation&Builder mixin not recognized
Expand All @@ -40,7 +40,7 @@ public function __invoke($model, $args): void
}

if ($args->has('upsert')) {
$upsertModel = new ResolveNested(new UpsertModel(new SaveModel()));
$upsertModel = new ResolveNested(new UpsertModel(new SaveModel($relation)));

foreach ($args->arguments['upsert']->value as $childArgs) {
// @phpstan-ignore-next-line Relation&Builder mixin not recognized
Expand Down
6 changes: 3 additions & 3 deletions src/Execution/Arguments/NestedOneToOne.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,19 @@ public function __invoke($model, $args): void
assert($relation instanceof HasOne || $relation instanceof MorphOne);

if ($args->has('create')) {
$saveModel = new ResolveNested(new SaveModel());
$saveModel = new ResolveNested(new SaveModel($relation));

$saveModel($relation->make(), $args->arguments['create']->value);
}

if ($args->has('update')) {
$updateModel = new ResolveNested(new UpdateModel(new SaveModel()));
$updateModel = new ResolveNested(new UpdateModel(new SaveModel($relation)));

$updateModel($relation->make(), $args->arguments['update']->value);
}

if ($args->has('upsert')) {
$upsertModel = new ResolveNested(new UpsertModel(new SaveModel()));
$upsertModel = new ResolveNested(new UpsertModel(new SaveModel($relation)));

$upsertModel($relation->make(), $args->arguments['upsert']->value);
}
Expand Down
48 changes: 48 additions & 0 deletions tests/Integration/Execution/MutationExecutor/HasManyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -834,4 +834,52 @@ public function testConnectModelWithCustomKey(): void
],
]);
}

public function testUpdateNestedHasMany(): void
{
$user = factory(User::class)->create();
assert($user instanceof User);

$task = factory(Task::class)->create();
assert($task instanceof Task);

$this->graphQL(/** @lang GraphQL */ '
mutation ($input: UpdateUserInput!) {
updateUser(input: $input) {
id
name
tasks {
id
name
}
}
}
', [
'input' => [
'id' => $user->id,
'name' => 'foo',
'tasks' => [
'update' => [
[
'id' => $task->id,
'name' => 'bar',
],
],
],
],
])->assertJson([
'data' => [
'updateUser' => [
'id' => $user->id,
'name' => 'foo',
'tasks' => [
[
'id' => $task->id,
'name' => 'bar',
],
],
],
],
]);
}
}
42 changes: 42 additions & 0 deletions tests/Integration/Execution/MutationExecutor/MorphOneTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -369,4 +369,46 @@ public function testUpdateAndDeleteMorphOne(string $action): void
],
]);
}

public function testNestedConnectMorphOne(): void
{
$task = factory(Task::class)->create();
assert($task instanceof Task);

$image = factory(Image::class)->create();
assert($image instanceof Image);

$this->graphQL(/** @lang GraphQL */ '
mutation ($input: UpdateTaskInput!) {
updateTask(input: $input) {
id
name
image {
url
}
}
}
', [
'input' => [
'id' => $task->id,
'name' => 'foo',
'image' => [
'upsert' => [
'id' => $image->id,
'url' => 'foo',
],
],
],
])->assertJson([
'data' => [
'updateTask' => [
'id' => '1',
'name' => 'foo',
'image' => [
'url' => 'foo',
],
],
],
]);
}
}
Loading