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

Populating nested mutations with other relationships for @update #549

Merged
merged 25 commits into from
Mar 19, 2019
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
991d21b
refactoring tests
liepaja Jan 5, 2019
0581a84
Merge branch 'master' into nested-mutations-for-update
spawnia Jan 21, 2019
3df73fe
Fix BelongsToTest
spawnia Jan 21, 2019
8a6250f
Merge remote-tracking branch 'upstream/master' into nested-mutations-…
liepaja Feb 8, 2019
723183b
Porting functionality from create to update
liepaja Feb 9, 2019
5a98b9f
HasOne
liepaja Feb 9, 2019
2886b00
Merge remote-tracking branch 'upstream/master' into nested-mutations-…
liepaja Feb 28, 2019
ebbc716
Merge branch 'master' into liepaja-nested-mutations-for-update
spawnia Mar 7, 2019
63386a9
Merge remote-tracking branch 'liepaja/nested-mutations-for-update' in…
spawnia Mar 7, 2019
f8f9287
Fix type hints
spawnia Mar 7, 2019
a6adb35
Detach before destroying a many-to-many relationship
spawnia Mar 7, 2019
83c1a07
Add connect, sync and disconnect for many-to-many relationships
spawnia Mar 7, 2019
018d1c8
Add some docs for updating nested mutations
spawnia Mar 13, 2019
4dce8a4
Add test for BelongsToMany and fix it
spawnia Mar 14, 2019
2c91398
zYN7rK
spawnia Mar 14, 2019
f836de1
Merge branch 'master' into liepaja-nested-mutations-for-update
spawnia Mar 14, 2019
d2eb553
Add docs for updating a belongsTo relation
spawnia Mar 15, 2019
a3f267c
Merge branch 'master' into liepaja-nested-mutations-for-update
spawnia Mar 18, 2019
575f2c1
Clarify semantics for deleting/disconnecting BelongsTo, adding tests …
spawnia Mar 18, 2019
dd3e72b
Use new Collection instead of collect helper
spawnia Mar 19, 2019
035d98b
Make test table columns nullable for ease of use in testing
spawnia Mar 19, 2019
8383c7c
Fix column name in HourFactory
spawnia Mar 19, 2019
9f0c53a
Fix treatment of nested relation args for HasOne and MorphOne relations
spawnia Mar 19, 2019
1032029
Combine nested relation execution tests by relation type, simplifying…
spawnia Mar 19, 2019
be3c39e
zOGM7B
spawnia Mar 19, 2019
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
121 changes: 121 additions & 0 deletions src/Execution/MutationExecutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,10 @@ protected static function saveModelWithBelongsTo(Model $model, Collection $args,
$belongsTo = $model->{$relationName}();
$belongsTo->associate($values);
}

if ($operationKey === 'delete') {
$relation->getModel()::destroy($values);
}
});
});

Expand Down Expand Up @@ -212,6 +216,16 @@ public static function executeUpdate(Model $model, Collection $args, ?HasMany $p

[$hasMany, $remaining] = self::partitionArgsByRelationType($reflection, $args, HasMany::class);

[$morphMany, $remaining] = self::partitionArgsByRelationType($reflection, $remaining, MorphMany::class);

[$hasOne, $remaining] = self::partitionArgsByRelationType($reflection, $remaining, HasOne::class);

[$belongsToMany, $remaining] = self::partitionArgsByRelationType($reflection, $remaining, BelongsToMany::class);

[$morphOne, $remaining] = self::partitionArgsByRelationType($reflection, $remaining, MorphOne::class);

[$morphToMany, $remaining] = self::partitionArgsByRelationType($reflection, $remaining, MorphToMany::class);

$model = self::saveModelWithBelongsTo($model, $remaining, $parentRelation);

$hasMany->each(function (array $nestedOperations, string $relationName) use ($model): void {
Expand All @@ -235,6 +249,113 @@ public static function executeUpdate(Model $model, Collection $args, ?HasMany $p
});
});

$hasOne->each(function (array $nestedOperations, string $relationName) use ($model): void {
/** @var \Illuminate\Database\Eloquent\Relations\HasOne $relation */
$relation = $model->{$relationName}();

collect($nestedOperations)->each(function ($values, string $operationKey) use ($relation): void {
if ($operationKey === 'create') {
self::handleSingleRelationCreate(collect($values), $relation);
}

if ($operationKey === 'update') {
collect($values)->each(function ($singleValues) use ($relation): void {
self::executeUpdate($relation->getModel()->newInstance(), collect($singleValues), $relation);
});
}

if ($operationKey === 'delete') {
$relation->getModel()::destroy($values);
}
});
});

$morphMany->each(function (array $nestedOperations, string $relationName) use ($model): void {
/** @var \Illuminate\Database\Eloquent\Relations\MorphMany $relation */
$relation = $model->{$relationName}();

collect($nestedOperations)->each(function ($values, string $operationKey) use ($relation): void {
if ($operationKey === 'create') {
self::handleMultiRelationCreate(collect($values), $relation);
}
if ($operationKey === 'update') {
collect($values)->each(function ($singleValues) use ($relation): void {
self::executeUpdate($relation->getModel()->newInstance(), collect($singleValues), $relation);
});
}

if ($operationKey === 'delete') {
$relation->getModel()::destroy($values);
}
});
});

$morphOne->each(function (array $nestedOperations, string $relationName) use ($model): void {
/** @var \Illuminate\Database\Eloquent\Relations\MorphOne $relation */
$relation = $model->{$relationName}();

collect($nestedOperations)->each(function ($values, string $operationKey) use ($relation): void {
if ($operationKey === 'create') {
self::handleSingleRelationCreate(collect($values), $relation);
}

if ($operationKey === 'update') {
collect($values)->each(function ($singleValues) use ($relation): void {
self::executeUpdate($relation->getModel()->newInstance(), collect($singleValues), $relation);
});
}

if ($operationKey === 'delete') {
$relation->getModel()::destroy($values);
}
});
});

$belongsToMany->each(function (array $nestedOperations, string $relationName) use ($model): void {
/** @var \Illuminate\Database\Eloquent\Relations\BelongsToMany $relation */
$relation = $model->{$relationName}();

collect($nestedOperations)->each(function ($values, string $operationKey) use ($relation): void {
if ($operationKey === 'create') {
self::handleMultiRelationCreate(collect($values), $relation);
}

if ($operationKey === 'update') {
collect($values)->each(function ($singleValues) use ($relation): void {
self::executeUpdate($relation->getModel()->newInstance(), collect($singleValues), $relation);
});
}

if ($operationKey === 'delete') {
$relation->getModel()::destroy($values);
}

spawnia marked this conversation as resolved.
Show resolved Hide resolved
if ($operationKey === 'connect') {
$relation->attach($values);
}
});
});

$morphToMany->each(function (array $nestedOperations, string $relationName) use ($model): void {
/** @var \Illuminate\Database\Eloquent\Relations\HasMany $relation */
$relation = $model->{$relationName}();

collect($nestedOperations)->each(function ($values, string $operationKey) use ($relation): void {
if ($operationKey === 'create') {
self::handleMultiRelationCreate(collect($values), $relation);
}
if ($operationKey === 'update') {
collect($values)->each(function ($singleValues) use ($relation): void {
self::executeUpdate($relation->getModel()->newInstance(), collect($singleValues), $relation);
});
}

if ($operationKey === 'delete') {
$relation->getModel()::destroy($values);
}
});
});

return $model;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<?php

namespace Tests\Integration\Schema\Directives\Fields;
namespace Tests\Integration\Schema\Directives\Fields\UpdateDirectiveTests;

use Tests\DBTestCase;
use Tests\Utils\Models\Task;
use Tests\Utils\Models\User;
use Tests\Utils\Models\Company;
use Tests\Utils\Models\Category;

class UpdateDirectiveTest extends DBTestCase
class CoreTest extends DBTestCase
{
/**
* @test
Expand Down Expand Up @@ -98,70 +98,6 @@ public function itCanUpdateFromInputObject(): void
$this->assertSame('bar', Company::first()->name);
}

/**
* @test
*/
public function itCanUpdateWithBelongsTo(): void
{
factory(User::class, 2)->create();
factory(Task::class)->create([
'name' => 'bar',
'user_id' => 1,
]);

$this->schema = '
type Task {
id: ID!
name: String!
user: User @belongsTo
}

type User {
id: ID
}

type Mutation {
updateTask(input: UpdateTaskInput!): Task @update(flatten: true)
}

input UpdateTaskInput {
id: ID!
name: String
user_id: ID
}
'.$this->placeholderQuery();

$this->query('
mutation {
updateTask(input: {
id: 1
name: "foo"
user_id: 2
}) {
id
name
user {
id
}
}
}
')->assertJson([
'data' => [
'updateTask' => [
'id' => '1',
'name' => 'foo',
'user' => [
'id' => '2',
],
],
],
]);

$task = Task::first();
$this->assertSame('2', $task->user_id);
$this->assertSame('foo', $task->name);
}

/**
* @test
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Tests\Integration\Schema\Directives\Fields\UpdateDirectiveTests\RelationshipTests;

use Tests\DBTestCase;
use Illuminate\Support\Arr;
use Tests\Utils\Models\User;

class BelongsToManyTest extends DBTestCase
{

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

namespace Tests\Integration\Schema\Directives\Fields\UpdateDirectiveTests\RelationshipTests;

use Tests\DBTestCase;
use Tests\Utils\Models\Task;
use Tests\Utils\Models\User;

class BelongsToTest extends DBTestCase
{
/**
* @test
*/
public function itCanUpdateWithBelongsTo()
{
factory(User::class, 2)->create();
factory(Task::class)->create([
'name' => 'bar',
'user_id' => 1,
]);

$this->schema = '
type Task {
id: ID!
name: String!
user: User @belongsTo
}

type User {
id: ID
}

type Mutation {
updateTask(input: UpdateTaskInput!): Task @update(flatten: true)
}

input UpdateTaskInput {
id: ID!
name: String
user_id: ID
}
'.$this->placeholderQuery();

$this->query('
mutation {
updateTask(input: {
id: 1
name: "foo"
user_id: 2
}) {
id
name
user {
id
}
}
}
')->assertJson([
'data' => [
'updateTask' => [
'id' => '1',
'name' => 'foo',
'user' => [
'id' => '2'
]
]
]
]);

$task = Task::first();
$this->assertSame('2', $task->user_id);
$this->assertSame('foo', $task->name);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Tests\Integration\Schema\Directives\Fields\UpdateDirectiveTests\RelationshipTests;

use Tests\DBTestCase;
use Illuminate\Support\Arr;
use Tests\Utils\Models\Task;

class HasManyTest extends DBTestCase
{

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Tests\Integration\Schema\Directives\Fields\UpdateDirectiveTests\RelationshipTests;

use Tests\DBTestCase;
use Illuminate\Support\Arr;
use Tests\Utils\Models\Post;
use Tests\Utils\Models\User;

class HasOneTest extends DBTestCase
{

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Tests\Integration\Schema\Directives\Fields\UpdateDirectiveTests\RelationshipTests;

use Tests\DBTestCase;
use Illuminate\Support\Arr;
use Tests\Utils\Models\User;

class MorphManyTest extends DBTestCase
{

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Tests\Integration\Schema\Directives\Fields\UpdateDirectiveTests\RelationshipTests;

use Tests\DBTestCase;
use Illuminate\Support\Arr;
use Tests\Utils\Models\User;

class MorphOneTest extends DBTestCase
{

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Tests\Integration\Schema\Directives\Fields\UpdateDirectiveTests\RelationshipTests;

use Tests\DBTestCase;
use Illuminate\Support\Arr;
use Tests\Utils\Models\Task;

class MorphToTest extends DBTestCase
{

}