diff --git a/CHANGELOG.md b/CHANGELOG.md index 822d530d8..94f68a9a3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/Execution/Arguments/NestedOneToMany.php b/src/Execution/Arguments/NestedOneToMany.php index ecf8885b8..8b2c216aa 100644 --- a/src/Execution/Arguments/NestedOneToMany.php +++ b/src/Execution/Arguments/NestedOneToMany.php @@ -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 @@ -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 @@ -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 diff --git a/src/Execution/Arguments/NestedOneToOne.php b/src/Execution/Arguments/NestedOneToOne.php index cdd3155c6..d2de3e170 100644 --- a/src/Execution/Arguments/NestedOneToOne.php +++ b/src/Execution/Arguments/NestedOneToOne.php @@ -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); } diff --git a/tests/Integration/Execution/MutationExecutor/HasManyTest.php b/tests/Integration/Execution/MutationExecutor/HasManyTest.php index e6c8d5e4f..8d5593aa4 100644 --- a/tests/Integration/Execution/MutationExecutor/HasManyTest.php +++ b/tests/Integration/Execution/MutationExecutor/HasManyTest.php @@ -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', + ], + ], + ], + ], + ]); + } } diff --git a/tests/Integration/Execution/MutationExecutor/MorphOneTest.php b/tests/Integration/Execution/MutationExecutor/MorphOneTest.php index 8a86ccd99..1667204f6 100644 --- a/tests/Integration/Execution/MutationExecutor/MorphOneTest.php +++ b/tests/Integration/Execution/MutationExecutor/MorphOneTest.php @@ -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', + ], + ], + ], + ]); + } }