-
-
Notifications
You must be signed in to change notification settings - Fork 438
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #207 from mll-lab/nested-mutations
[Feat] Nested mutations
- Loading branch information
Showing
11 changed files
with
443 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
<?php | ||
|
||
namespace Nuwave\Lighthouse\Execution; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Database\Eloquent\Relations\BelongsTo; | ||
use Illuminate\Database\Eloquent\Relations\HasMany; | ||
use Illuminate\Support\Collection; | ||
|
||
class MutationExecutor | ||
{ | ||
public static function executeCreate(Model $model, Collection $args, HasMany $parentRelation = null): Model | ||
{ | ||
list($belongsTo, $remaining) = self::extractBelongsToArgs($model, $args); | ||
list($hasMany, $remaining) = self::extractHasManyArgs($model, $remaining); | ||
|
||
$model->fill($remaining->all()); | ||
|
||
$belongsTo->each(function ($value, $key) use ($model) { | ||
$model->{$key}()->associate($value); | ||
}); | ||
|
||
$parentRelation | ||
? $parentRelation->save($model) | ||
: $model->save(); | ||
|
||
$hasMany->each(function ($nestedOperations, $key) use ($model) { | ||
/** @var HasMany $relation */ | ||
$relation = $model->{$key}(); | ||
|
||
collect($nestedOperations)->each(function ($values, $operationKey) use ($relation) { | ||
if ($operationKey === 'create') { | ||
self::handleHasManyCreate(collect($values), $relation); | ||
} | ||
}); | ||
}); | ||
|
||
return $model; | ||
} | ||
|
||
protected static function handleHasManyCreate(Collection $multiValues, HasMany $relation) | ||
{ | ||
$multiValues->each(function ($singleValues) use ($relation) { | ||
self::executeCreate($relation->getModel()->newInstance(), collect($singleValues), $relation); | ||
}); | ||
} | ||
|
||
public static function executeUpdate(Model $model, Collection $args, HasMany $parentRelation = null): Model | ||
{ | ||
list($belongsTo, $remaining) = self::extractBelongsToArgs($model, $args); | ||
list($hasMany, $remaining) = self::extractHasManyArgs($model, $remaining); | ||
|
||
$model = $model->newQuery()->findOrFail($args->pull('id')); | ||
$model->fill($remaining->all()); | ||
|
||
$belongsTo->each(function ($value, $key) use ($model) { | ||
$model->{$key}()->associate($value); | ||
}); | ||
|
||
$parentRelation | ||
? $parentRelation->save($model) | ||
: $model->save(); | ||
|
||
$hasMany->each(function ($nestedOperations, $key) use ($model) { | ||
/** @var HasMany $relation */ | ||
$relation = $model->{$key}(); | ||
|
||
collect($nestedOperations)->each(function ($values, $operationKey) use ($relation) { | ||
if ($operationKey === 'create') { | ||
self::handleHasManyCreate(collect($values), $relation); | ||
} | ||
|
||
if ($operationKey === 'update') { | ||
collect($values)->each(function ($singleValues) use ($relation) { | ||
self::executeUpdate($relation->getModel()->newInstance(), collect($singleValues), $relation); | ||
}); | ||
} | ||
|
||
if ($operationKey === 'delete') { | ||
$relation->getModel()::destroy($values); | ||
} | ||
}); | ||
}); | ||
|
||
return $model; | ||
} | ||
|
||
protected static function extractBelongsToArgs(Model $model, Collection $args): Collection | ||
{ | ||
return $args->partition(function ($value, $key) use ($model) { | ||
return method_exists($model, $key) && ($model->{$key}() instanceof BelongsTo); | ||
}); | ||
} | ||
|
||
protected static function extractHasManyArgs(Model $model, Collection $args): Collection | ||
{ | ||
return $args->partition(function ($value, $key) use ($model) { | ||
return method_exists($model, $key) && ($model->{$key}() instanceof HasMany); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
tests/Integration/Schema/Directives/Fields/CreateDirectiveTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
<?php | ||
|
||
namespace Tests\Integration\Schema\Directives\Fields; | ||
|
||
use Tests\DBTestCase; | ||
use Tests\Utils\Models\User; | ||
use Illuminate\Foundation\Testing\RefreshDatabase; | ||
|
||
class CreateDirectiveTest extends DBTestCase | ||
{ | ||
use RefreshDatabase; | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function itCanCreateFromFieldArguments() | ||
{ | ||
$schema = ' | ||
type Company { | ||
id: ID! | ||
name: String! | ||
} | ||
type Mutation { | ||
createCompany(name: String): Company @create | ||
} | ||
'; | ||
$query = ' | ||
mutation { | ||
createCompany(name: "foo") { | ||
id | ||
name | ||
} | ||
} | ||
'; | ||
$result = $this->execute($schema, $query); | ||
|
||
$this->assertSame('1', array_get($result, 'data.createCompany.id')); | ||
$this->assertSame('foo', array_get($result, 'data.createCompany.name')); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function itCanCreateFromInputObject() | ||
{ | ||
$schema = ' | ||
type Company { | ||
id: ID! | ||
name: String! | ||
} | ||
type Mutation { | ||
createCompany(input: CreateCompanyInput!): Company @create(flatten: true) | ||
} | ||
input CreateCompanyInput { | ||
name: String | ||
} | ||
'; | ||
$query = ' | ||
mutation { | ||
createCompany(input: { | ||
name: "foo" | ||
}) { | ||
id | ||
name | ||
} | ||
} | ||
'; | ||
$result = $this->execute($schema, $query); | ||
|
||
$this->assertSame('1', array_get($result, 'data.createCompany.id')); | ||
$this->assertSame('foo', array_get($result, 'data.createCompany.name')); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function itCanCreateWithBelongsTo() | ||
{ | ||
factory(User::class)->create(); | ||
|
||
$schema = ' | ||
type Task { | ||
id: ID! | ||
name: String! | ||
user: User @belongsTo | ||
} | ||
type User { | ||
id: ID | ||
} | ||
type Mutation { | ||
createTask(input: CreateTaskInput!): Task @create(flatten: true) | ||
} | ||
input CreateTaskInput { | ||
name: String | ||
user: ID | ||
} | ||
'; | ||
$query = ' | ||
mutation { | ||
createTask(input: { | ||
name: "foo" | ||
user: 1 | ||
}) { | ||
id | ||
name | ||
user { | ||
id | ||
} | ||
} | ||
} | ||
'; | ||
$result = $this->execute($schema, $query); | ||
|
||
$this->assertSame('1', array_get($result, 'data.createTask.id')); | ||
$this->assertSame('foo', array_get($result, 'data.createTask.name')); | ||
$this->assertSame('1', array_get($result, 'data.createTask.user.id')); | ||
} | ||
} |
Oops, something went wrong.