Skip to content
Open
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
2 changes: 1 addition & 1 deletion src/Execution/Arguments/NestedBelongsTo.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public static function disconnectOrDelete(BelongsTo $relation, ArgumentSet $args
&& $args->arguments['delete']->value
) {
$relation->dissociate();
$relation->delete();
$relation->first()?->delete();
}
}
}
2 changes: 1 addition & 1 deletion src/Schema/Directives/DeleteDirective.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public function __invoke($model, $idOrIds): void
$relation->getParent()->save();
}

$relation->delete();
$relation->first()?->delete();
}
} else {
$related = $relation->make(); // @phpstan-ignore method.notFound (Relation delegates to Builder)
Expand Down
52 changes: 48 additions & 4 deletions tests/Integration/Execution/MutationExecutor/BelongsToTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -794,9 +794,53 @@ public function testUpdateAndDeleteBelongsTo(string $action): void
);
}

/** @dataProvider existingModelMutations */
#[DataProvider('existingModelMutations')]
public function testDeleteBelongsToFiresModelEvents(string $action): void
{
$user = factory(User::class)->create();
$this->assertInstanceOf(User::class, $user);

$task = factory(Task::class)->make();
$this->assertInstanceOf(Task::class, $task);
$task->user()->associate($user);
$task->save();

$deletingCalled = false;
User::deleting(static function () use (&$deletingCalled): void {
$deletingCalled = true;
});

$this->graphQL(/** @lang GraphQL */ <<<GRAPHQL
mutation {
{$action}Task(input: {
id: {$task->id}
user: {
delete: true
}
}) {
id
}
}
GRAPHQL
)->assertJson([
'data' => [
"{$action}Task" => [
'id' => "{$task->id}",
],
],
]);

$this->assertTrue(
$deletingCalled,
'Deleting the related model must trigger model events.',
);
}

public function testCreateUsingUpsertAndDeleteBelongsTo(): void
{
factory(User::class)->create();
$user = factory(User::class)->create();
$this->assertInstanceOf(User::class, $user);

$this->graphQL(/** @lang GraphQL */ '
mutation {
Expand Down Expand Up @@ -842,10 +886,10 @@ public function testDoesNotDeleteOrDisconnectOnFalsyValues(string $action): void
$user = factory(User::class)->create();
$this->assertInstanceOf(User::class, $user);

$task = $user->tasks()->save(
factory(Task::class)->make(),
);
$task = factory(Task::class)->make();
$this->assertInstanceOf(Task::class, $task);
$task->user()->associate($user);
$task->save();

$this->graphQL(/** @lang GraphQL */ <<<GRAPHQL
mutation {
Expand Down
112 changes: 111 additions & 1 deletion tests/Integration/Schema/Directives/DeleteDirectiveTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,8 @@ public function testDeleteHasOneThroughNestedArgResolver(): void

$post = factory(Post::class)->make();
$this->assertInstanceOf(Post::class, $post);
$task->post()->save($post);
$post->task()->associate($task);
$post->save();

$this->schema .= /** @lang GraphQL */ '
type Mutation {
Expand Down Expand Up @@ -430,6 +431,60 @@ public function testDeleteHasOneThroughNestedArgResolver(): void
$this->assertNull(Post::find($post->id));
}

public function testDeleteHasOneThroughNestedArgResolverFiresModelEvents(): void
{
$task = factory(Task::class)->create();
$this->assertInstanceOf(Task::class, $task);

$post = factory(Post::class)->make();
$this->assertInstanceOf(Post::class, $post);
$task->post()->save($post);

$deletingCalled = false;
Post::deleting(static function () use (&$deletingCalled): void {
$deletingCalled = true;
});

$this->schema .= /** @lang GraphQL */ '
type Mutation {
updateTask(
id: ID!
deletePost: Boolean @delete(relation: "post")
): Task! @update
}

type Task {
id: ID!
post: Post
}

type Post {
id: ID!
}
';

$this->graphQL(/** @lang GraphQL */ '
mutation ($id: ID!) {
updateTask(id: $id, deletePost: true) {
id
}
}
', [
'id' => $task->id,
])->assertJson([
'data' => [
'updateTask' => [
'id' => "{$task->id}",
],
],
]);

$this->assertTrue(
$deletingCalled,
'Deleting the related model must trigger model events.',
);
}

public function testDeleteBelongsToThroughNestedArgResolver(): void
{
$user = factory(User::class)->create();
Expand Down Expand Up @@ -482,6 +537,61 @@ public function testDeleteBelongsToThroughNestedArgResolver(): void
$this->assertNull(User::find($user->id));
}

public function testDeleteBelongsToThroughNestedArgResolverFiresModelEvents(): void
{
$user = factory(User::class)->create();
$this->assertInstanceOf(User::class, $user);

$task = factory(Task::class)->make();
$this->assertInstanceOf(Task::class, $task);
$task->user()->associate($user);
$task->save();

$deletingCalled = false;
User::deleting(static function () use (&$deletingCalled): void {
$deletingCalled = true;
});

$this->schema .= /** @lang GraphQL */ '
type Mutation {
updateTask(
id: ID!
deleteUser: Boolean @delete(relation: "user")
): Task! @update
}

type Task {
id: ID!
user: User
}

type User {
id: ID!
}
';

$this->graphQL(/** @lang GraphQL */ '
mutation ($id: ID!) {
updateTask(id: $id, deleteUser: true) {
id
}
}
', [
'id' => $task->id,
])->assertJson([
'data' => [
'updateTask' => [
'id' => "{$task->id}",
],
],
]);

$this->assertTrue(
$deletingCalled,
'Deleting the related model must trigger model events.',
);
}

public function testDeletingReturnsFalseTriggersException(): void
{
User::deleting(static fn (): bool => false);
Expand Down