Skip to content
Draft
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
28 changes: 24 additions & 4 deletions src/Execution/Arguments/NestedOneToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,31 @@ public static function createUpdateUpsert(ArgumentSet $args, Relation $relation)
}

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

foreach ($args->arguments['update']->value as $childArgs) {
// @phpstan-ignore-next-line Relation&Builder mixin not recognized
$updateModel($relation->make(), $childArgs);
// @phpstan-ignore-next-line Relation&Builder mixin not recognized
$model = $relation->make();

$ids = [];

$updateArgs = $args->arguments['update']->value;

foreach ($updateArgs as $childArgs) {
$ids[UpdateModel::getId($childArgs, $model)] = true;
}

$models = $model->newModelQuery()
->whereIn($model->getKeyName(), array_keys($ids))
->get();

foreach ($models as $instance) {
$id = $instance->getKey();
$ids[$id] = $instance;
}

foreach ($updateArgs as $childArgs) {
$instance = $ids[UpdateModel::pullId($childArgs, $model)];
$updateModel($instance, $childArgs);
}
}

Expand Down
37 changes: 34 additions & 3 deletions src/Execution/Arguments/UpdateModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Nuwave\Lighthouse\Execution\Arguments;

use GraphQL\Error\Error;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Arr;
use Nuwave\Lighthouse\Support\Contracts\ArgResolver;

Expand All @@ -24,10 +25,23 @@ public function __construct(callable $previous)
}

/**
* @param \Illuminate\Database\Eloquent\Model $model
* @param Model $model
* @param \Nuwave\Lighthouse\Execution\Arguments\ArgumentSet $args
*/
public function __invoke($model, $args)
{
$id = self::pullId($args, $model);
$instance = $model->newQuery()->findOrFail($id);

return ($this->previous)($instance, $args);
}

/**
* Extract and remove the model ID from the given args.
*
* @return mixed any non-null ID value
*/
public static function pullId(ArgumentSet $args, Model $model)
{
/** @var \Nuwave\Lighthouse\Execution\Arguments\Argument|null $id */
$id = Arr::pull($args->arguments, 'id')
Expand All @@ -38,8 +52,25 @@ public function __invoke($model, $args)
throw new Error(self::MISSING_PRIMARY_KEY_FOR_UPDATE);
}

$model = $model->newQuery()->findOrFail($id->value);
return $id->value;
}

/**
* Extract and remove the model ID from the given args.
*
* @return mixed any non-null ID value
*/
public static function getId(ArgumentSet $args, Model $model)
{
/** @var \Nuwave\Lighthouse\Execution\Arguments\Argument|null $id */
$id = Arr::get($args->arguments, 'id')
?? Arr::get($args->arguments, $model->getKeyName())
?? null;

if (null === $id) {
throw new Error(self::MISSING_PRIMARY_KEY_FOR_UPDATE);
}

return ($this->previous)($model, $args);
return $id->value;
}
}
40 changes: 30 additions & 10 deletions tests/Integration/Execution/MutationExecutor/HasManyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -373,8 +373,8 @@ public function testUpdateHasMany(string $action): void
$user = factory(User::class)->create();

$user->tasks()
->save(
factory(Task::class)->create()
->saveMany(
factory(Task::class, 2)->create()
);

$this->graphQL(/** @lang GraphQL */ <<<GRAPHQL
Expand All @@ -383,10 +383,16 @@ public function testUpdateHasMany(string $action): void
id: 1
name: "foo"
tasks: {
update: [{
id: 1
name: "bar"
}]
update: [
{
id: 1
name: "foo"
}
{
id: 2
name: "bar"
}
]
}
}) {
id
Expand All @@ -406,6 +412,10 @@ public function testUpdateHasMany(string $action): void
'tasks' => [
[
'id' => '1',
'name' => 'foo',
],
[
'id' => '2',
'name' => 'bar',
],
],
Expand Down Expand Up @@ -433,10 +443,16 @@ public function testUpsertHasMany(string $action): void
id: 1
name: "foo"
tasks: {
upsert: [{
id: 1
name: "bar"
}]
upsert: [
{
id: 1
name: "foo"
},
{
id: 2
name: "bar"
},
]
}
}) {
id
Expand All @@ -456,6 +472,10 @@ public function testUpsertHasMany(string $action): void
'tasks' => [
[
'id' => '1',
'name' => 'foo',
],
[
'id' => '2',
'name' => 'bar',
],
],
Expand Down