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

Refresh newly created models before returning them #509

Merged
merged 2 commits into from Dec 14, 2018
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 22 additions & 8 deletions src/Execution/MutationExecutor.php
Expand Up @@ -34,6 +34,10 @@ public static function executeCreate(Model $model, Collection $args, HasMany $pa
});
});

// Make sure that values that are set through the database insert
// are immediately available for return, e.g. default values
$model->refresh();

return $model;
}

Expand Down Expand Up @@ -73,7 +77,11 @@ protected static function saveModelWithBelongsTo(Model $model, Collection $remai
protected static function handleHasManyCreate(Collection $multiValues, HasMany $relation)
{
$multiValues->each(function ($singleValues) use ($relation) {
self::executeCreate($relation->getModel()->newInstance(), collect($singleValues), $relation);
self::executeCreate(
$relation->getModel()->newInstance(),
collect($singleValues),
$relation
);
});
}

Expand All @@ -99,18 +107,22 @@ public static function executeUpdate(Model $model, Collection $args, HasMany $pa

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

$hasMany->each(function ($nestedOperations, $relationName) use ($model) {
$hasMany->each(function ($nestedOperations, string $relationName) use ($model) {
/** @var HasMany $relation */
$relation = $model->{$relationName}();

collect($nestedOperations)->each(function ($values, $operationKey) use ($relation) {
collect($nestedOperations)->each(function ($values, string $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);
self::executeUpdate(
$relation->getModel()->newInstance(),
collect($singleValues),
$relation
);
});
}

Expand Down Expand Up @@ -144,8 +156,9 @@ public static function executeUpdate(Model $model, Collection $args, HasMany $pa
*/
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);
return $args->partition(function ($value, string $key) use ($model) {
return method_exists($model, $key)
&& ($model->{$key}() instanceof BelongsTo);
});
}

Expand Down Expand Up @@ -179,8 +192,9 @@ protected static function extractBelongsToArgs(Model $model, Collection $args):
*/
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);
return $args->partition(function ($value, string $key) use ($model) {
return method_exists($model, $key)
&& ($model->{$key}() instanceof HasMany);
});
}
}
93 changes: 93 additions & 0 deletions tests/Integration/Schema/Directives/Fields/CreateDirectiveTest.php
Expand Up @@ -119,4 +119,97 @@ public function itCanCreateWithBelongsTo()
$this->assertSame('foo', Arr::get($result, 'data.createTask.name'));
$this->assertSame('1', Arr::get($result, 'data.createTask.user.id'));
}

/**
* @test
*/
public function itCanCreateWithHasMany()
{
$schema = '
type Task {
id: ID!
name: String!
}

type User {
id: ID!
name: String
tasks: [Task!]! @hasMany
}

type Mutation {
createUser(input: CreateUserInput!): User @create(flatten: true)
}

input CreateUserInput {
name: String
tasks: CreateTaskRelation
}

input CreateTaskRelation {
create: [CreateTaskInput!]
}

input CreateTaskInput {
name: String
user: ID
}
' . $this->placeholderQuery();
$query = '
mutation {
createUser(input: {
name: "foo"
tasks: {
create: [{
name: "bar"
}]
}
}) {
id
name
tasks {
id
name
}
}
}
';
$result = $this->execute($schema, $query);

$this->assertSame('1', Arr::get($result, 'data.createUser.id'));
$this->assertSame('foo', Arr::get($result, 'data.createUser.name'));
$this->assertSame('1', Arr::get($result, 'data.createUser.tasks.0.id'));
$this->assertSame('bar', Arr::get($result, 'data.createUser.tasks.0.name'));
}

/**
* @test
*/
public function itCreatesAnEntryWithDatabaseDefaultsAndReturnsItImmediately()
{
$schema = '
type Mutation {
createTag(name: String): Tag @create
}

type Tag {
name: String!
default_string: String!
}
' . $this->placeholderQuery();
$query = '
mutation {
createTag(name: "foobar"){
name
default_string
}
}
';
$result = $this->execute($schema, $query);

$this->assertSame([
'name' => 'foobar',
'default_string' => \CreateTestbenchTagsTable::DEFAULT_STRING,
], Arr::get($result, 'data.createTag'));
}
}
Expand Up @@ -13,11 +13,11 @@ public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('company_id');
$table->unsignedInteger('company_id')->nullable();
$table->unsignedInteger('team_id')->nullable();
$table->string('name');
$table->string('email');
$table->string('password');
$table->string('name')->nullable();
$table->string('email')->nullable();
$table->string('password')->nullable();
$table->timestamps();
});
}
Expand Down
Expand Up @@ -6,6 +6,8 @@

class CreateTestbenchTagsTable extends Migration
{
const DEFAULT_STRING = 'this is the default string';

/**
* Run the migrations.
*/
Expand All @@ -14,6 +16,7 @@ public function up()
Schema::create('tags', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('default_string')->default(self::DEFAULT_STRING);
$table->timestamps();
});
}
Expand Down