-
-
Notifications
You must be signed in to change notification settings - Fork 439
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
Specify identifying columns on nested mutation upserts #2426
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -9,10 +9,14 @@ class UpsertModel implements ArgResolver | |||||||||
/** @var callable|\Nuwave\Lighthouse\Support\Contracts\ArgResolver */ | ||||||||||
protected $previous; | ||||||||||
|
||||||||||
/** @var array<string> */ | ||||||||||
protected array $identifyingColumns; | ||||||||||
Comment on lines
+12
to
+13
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
|
||||||||||
/** @param callable|\Nuwave\Lighthouse\Support\Contracts\ArgResolver $previous */ | ||||||||||
public function __construct(callable $previous) | ||||||||||
public function __construct(callable $previous, ?array $identifyingColumns) | ||||||||||
{ | ||||||||||
$this->previous = $previous; | ||||||||||
$this->identifyingColumns = $identifyingColumns ?? []; | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
} | ||||||||||
|
||||||||||
/** | ||||||||||
|
@@ -22,20 +26,39 @@ public function __construct(callable $previous) | |||||||||
public function __invoke($model, $args): mixed | ||||||||||
{ | ||||||||||
// TODO consider Laravel native ->upsert(), available from 8.10 | ||||||||||
spawnia marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||
$id = $args->arguments['id'] | ||||||||||
?? $args->arguments[$model->getKeyName()] | ||||||||||
?? null; | ||||||||||
$existingModel = null; | ||||||||||
|
||||||||||
if ($id !== null) { | ||||||||||
if (! empty($this->identifyingColumns)) { | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
The directive should implement |
||||||||||
$existingModel = $model | ||||||||||
->newQuery() | ||||||||||
->find($id->value); | ||||||||||
->firstWhere( | ||||||||||
array_intersect_key( | ||||||||||
$args->toArray(), | ||||||||||
array_flip($this->identifyingColumns), | ||||||||||
), | ||||||||||
); | ||||||||||
|
||||||||||
if ($existingModel !== null) { | ||||||||||
$model = $existingModel; | ||||||||||
} | ||||||||||
} | ||||||||||
|
||||||||||
if ($existingModel === null) { | ||||||||||
$id = $args->arguments['id'] | ||||||||||
?? $args->arguments[$model->getKeyName()] | ||||||||||
?? null; | ||||||||||
|
||||||||||
if ($id !== null) { | ||||||||||
$existingModel = $model | ||||||||||
->newQuery() | ||||||||||
->find($id->value); | ||||||||||
|
||||||||||
if ($existingModel !== null) { | ||||||||||
$model = $existingModel; | ||||||||||
} | ||||||||||
} | ||||||||||
} | ||||||||||
|
||||||||||
return ($this->previous)($model, $args); | ||||||||||
} | ||||||||||
} |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Update the directive definition in |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -21,6 +21,12 @@ public static function definition(): string | |||||
""" | ||||||
model: String | ||||||
|
||||||
""" | ||||||
Specify the columns by which to upsert the model. | ||||||
This is optional, defaults to the ID or model Key. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
It is not like this argument itself defaults to those values, the mechanics behind it behave that way. |
||||||
""" | ||||||
identifyingColumns: [String!] = [] | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
""" | ||||||
Specify the name of the relation on the parent model. | ||||||
This is only needed when using this directive as a nested arg | ||||||
|
@@ -33,6 +39,9 @@ public static function definition(): string | |||||
|
||||||
protected function makeExecutionFunction(Relation $parentRelation = null): callable | ||||||
{ | ||||||
return new UpsertModel(new SaveModel($parentRelation)); | ||||||
return new UpsertModel( | ||||||
new SaveModel($parentRelation), | ||||||
$this->directiveArgValue('identifyingColumns'), | ||||||
); | ||||||
} | ||||||
} |
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -6,6 +6,7 @@ | |||||||||
use Illuminate\Container\Container; | ||||||||||
use Nuwave\Lighthouse\Schema\TypeRegistry; | ||||||||||
use Tests\DBTestCase; | ||||||||||
use Tests\Utils\Models\Company; | ||||||||||
use Tests\Utils\Models\Task; | ||||||||||
use Tests\Utils\Models\User; | ||||||||||
|
||||||||||
|
@@ -198,6 +199,150 @@ interface IUser | |||||||||
]); | ||||||||||
} | ||||||||||
|
||||||||||
public function testDirectUpsertByIdentifyingColumn(): void | ||||||||||
{ | ||||||||||
$this->schema .= /** @lang GraphQL */ ' | ||||||||||
type User { | ||||||||||
id: ID! | ||||||||||
email: String! | ||||||||||
name: String! | ||||||||||
} | ||||||||||
|
||||||||||
type Mutation { | ||||||||||
upsertUser(name: String!, email: String!): User @upsert(identifyingColumns: ["email"]) | ||||||||||
} | ||||||||||
'; | ||||||||||
|
||||||||||
$this->graphQL(/** @lang GraphQL */ ' | ||||||||||
mutation { | ||||||||||
upsertUser( | ||||||||||
email: "[email protected]" | ||||||||||
name: "bar" | ||||||||||
) { | ||||||||||
name | ||||||||||
} | ||||||||||
} | ||||||||||
')->assertJson([ | ||||||||||
'data' => [ | ||||||||||
'upsertUser' => [ | ||||||||||
'email' => '[email protected]', | ||||||||||
'name' => 'bar', | ||||||||||
], | ||||||||||
], | ||||||||||
]); | ||||||||||
|
||||||||||
$user = User::firstOrFail(); | ||||||||||
|
||||||||||
$this->assertSame('bar', $user->name); | ||||||||||
$this->assertSame('[email protected]', $user->email); | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use variables to show the connection between the passed values. |
||||||||||
|
||||||||||
$this->graphQL(/** @lang GraphQL */ ' | ||||||||||
mutation { | ||||||||||
upsertUser( | ||||||||||
email: "[email protected]" | ||||||||||
name: "foo" | ||||||||||
) { | ||||||||||
name | ||||||||||
} | ||||||||||
} | ||||||||||
')->assertJson([ | ||||||||||
'data' => [ | ||||||||||
'upsertUser' => [ | ||||||||||
'email' => '[email protected]', | ||||||||||
'name' => 'foo', | ||||||||||
], | ||||||||||
], | ||||||||||
]); | ||||||||||
|
||||||||||
$user->refresh(); | ||||||||||
|
||||||||||
$this->assertSame('foo', $user->name); | ||||||||||
$this->assertSame('[email protected]', $user->email); | ||||||||||
} | ||||||||||
|
||||||||||
public function testDirectUpsertByIdentifyingColumns(): void | ||||||||||
{ | ||||||||||
$company = factory(Company::class)->create(['id' => 1]); | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||||||
|
||||||||||
$this->schema | ||||||||||
/** @lang GraphQL */ | ||||||||||
.= ' | ||||||||||
Comment on lines
+269
to
+271
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
type User { | ||||||||||
id: ID! | ||||||||||
email: String! | ||||||||||
name: String! | ||||||||||
company_id: ID! | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No raw foreign keys in GraphQL, not even as a test case. Often, those test cases serve as an example, so I don't want them to implement anti-patterns. |
||||||||||
} | ||||||||||
|
||||||||||
type Mutation { | ||||||||||
upsertUser(name: String!, email: String!, company_id:ID!): User @upsert(identifyingColumns: ["name", "company_id"]) | ||||||||||
} | ||||||||||
'; | ||||||||||
|
||||||||||
$this->graphQL( | ||||||||||
/** @lang GraphQL */ | ||||||||||
' | ||||||||||
Comment on lines
+284
to
+286
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
mutation { | ||||||||||
upsertUser( | ||||||||||
email: "[email protected]" | ||||||||||
name: "bar" | ||||||||||
company_id: 1 | ||||||||||
) { | ||||||||||
name | ||||||||||
company_id | ||||||||||
} | ||||||||||
} | ||||||||||
', | ||||||||||
)->assertJson([ | ||||||||||
'data' => [ | ||||||||||
'upsertUser' => [ | ||||||||||
'email' => '[email protected]', | ||||||||||
'name' => 'bar', | ||||||||||
'company_id' => 1, | ||||||||||
], | ||||||||||
], | ||||||||||
]); | ||||||||||
|
||||||||||
$user = User::firstOrFail(); | ||||||||||
|
||||||||||
$this->assertSame('bar', $user->name); | ||||||||||
$this->assertSame('[email protected]', $user->email); | ||||||||||
$this->assertSame(1, $user->company_id); | ||||||||||
|
||||||||||
$this->graphQL( | ||||||||||
/** @lang GraphQL */ | ||||||||||
' | ||||||||||
Comment on lines
+315
to
+317
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
mutation { | ||||||||||
upsertUser( | ||||||||||
email: "[email protected]" | ||||||||||
name: "bar" | ||||||||||
company_id: 1 | ||||||||||
) { | ||||||||||
name | ||||||||||
company_id | ||||||||||
} | ||||||||||
} | ||||||||||
', | ||||||||||
)->assertJson([ | ||||||||||
'data' => [ | ||||||||||
'upsertUser' => [ | ||||||||||
'email' => '[email protected]', | ||||||||||
'name' => 'bar', | ||||||||||
'company_id' => $company->id, | ||||||||||
], | ||||||||||
], | ||||||||||
]); | ||||||||||
|
||||||||||
$user->refresh(); | ||||||||||
|
||||||||||
$this->assertSame('bar', $user->name); | ||||||||||
$this->assertSame('[email protected]', $user->email); | ||||||||||
} | ||||||||||
|
||||||||||
public static function resolveType(): Type | ||||||||||
{ | ||||||||||
$typeRegistry = Container::getInstance()->make(TypeRegistry::class); | ||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.