Skip to content

Commit

Permalink
Test upsert with id null
Browse files Browse the repository at this point in the history
  • Loading branch information
spawnia committed Aug 2, 2024
1 parent 30fedff commit 98cff28
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 7 deletions.
8 changes: 4 additions & 4 deletions src/Execution/Arguments/UpsertModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ public function __invoke($model, $args): mixed
?? $args->arguments[$model->getKeyName()]
?? null;

if ($id !== null) {
$existingModel = $model
->newQuery()
->find($id->value);
$idValue = $id?->value;
if ($idValue) {
$existingModel = $model->newQuery()
->find($idValue);

if ($existingModel !== null) {
$model = $existingModel;
Expand Down
34 changes: 34 additions & 0 deletions tests/Integration/Execution/MutationExecutor/BelongsToTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,40 @@ public function testUpsertBelongsToWithoutID(): void
]);
}

public function testUpsertBelongsToWithIDNull(): void
{
$this->graphQL(/** @lang GraphQL */ <<<'GRAPHQL'
mutation {
upsertTask(input: {
name: "foo"
user: {
upsert: {
id: null
name: "New User"
}
}
}) {
id
name
user {
id
}
}
}
GRAPHQL
)->assertJson([
'data' => [
'upsertTask' => [
'id' => '1',
'name' => 'foo',
'user' => [
'id' => '1',
],
],
],
]);
}

public function testUpsertUsingCreateWithNewBelongsTo(): void
{
$this->graphQL(/** @lang GraphQL */ '
Expand Down
41 changes: 38 additions & 3 deletions tests/Integration/Execution/MutationExecutor/HasOneTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public function testUpsertWithNewHasOne(): void
name: "foo"
post: {
upsert: {
id: 1
id: 2
title: "bar"
}
}
Expand All @@ -139,7 +139,7 @@ public function testUpsertWithNewHasOne(): void
'id' => '1',
'name' => 'foo',
'post' => [
'id' => '1',
'id' => '2',
'title' => 'bar',
],
],
Expand Down Expand Up @@ -183,14 +183,49 @@ public function testCreateUsingUpsertWithNewHasOne(): void
]);
}

public function testUpsertHasOneWithoutId(): void
public function testUpsertHasOneWithoutID(): void
{
$this->graphQL(/** @lang GraphQL */ '
mutation {
upsertTask(input: {
name: "foo"
post: {
upsert: {
title: "bar"
}
}
}) {
id
name
post {
id
title
}
}
}
')->assertJson([
'data' => [
'upsertTask' => [
'id' => '1',
'name' => 'foo',
'post' => [
'id' => '1',
'title' => 'bar',
],
],
],
]);
}

public function testUpsertHasOneWithIDNull(): void
{
$this->graphQL(/** @lang GraphQL */ '
mutation {
upsertTask(input: {
name: "foo"
post: {
upsert: {
id: null
title: "bar"
}
}
Expand Down

0 comments on commit 98cff28

Please sign in to comment.