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

Add @bind directive as a GraphQL analogue for Laravel's Route Model Binding #2645

Merged
merged 34 commits into from
Jan 9, 2025
Merged
Changes from 1 commit
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
a7a0952
Setup validation for `@bind` directive schema definitions
remipelhate Dec 21, 2024
b41f64b
Implement simple model binding
remipelhate Dec 22, 2024
145a247
Restructure to separate Nuwave\Lighthouse\Bind namespace
remipelhate Dec 22, 2024
d407329
Test optional model binding
remipelhate Dec 22, 2024
3018a72
Handle optional model bindings
remipelhate Dec 23, 2024
518df4c
Add missing tests for binding models by column
remipelhate Dec 24, 2024
ce51095
Add missing tests for binding models with eager loading
remipelhate Dec 24, 2024
8f981f7
Add missing tests for too many results when resolving a binding
remipelhate Dec 24, 2024
2f46f5e
Handle missing required bindings a validation errors
remipelhate Dec 25, 2024
371749f
Rename `optional` argument to `required` on BindDirective
remipelhate Dec 25, 2024
a03666e
Cleanup exception assetions in BindDirectiveTest
remipelhate Dec 25, 2024
6c6e4e4
Fail schema validation when `@bind` directive is defined on unsupport…
remipelhate Dec 26, 2024
a291ef2
Ensure `@bind` directive can be used multiple times in the same request
remipelhate Dec 27, 2024
aa54257
Add missing tests for binding instances using callable classes
remipelhate Dec 27, 2024
8080404
Add docs
remipelhate Dec 27, 2024
f0ddbeb
Fix PHPStan errors
remipelhate Dec 27, 2024
23dd711
Fix backwards compatibility with Laravel 9
remipelhate Dec 27, 2024
6030bd4
Update changelog
remipelhate Dec 29, 2024
17597f9
review | Remove Laravel version in docs reference
remipelhate Jan 3, 2025
dd87182
review | Update examples in docs
remipelhate Jan 3, 2025
e64fff9
review | Remove function imports
remipelhate Jan 3, 2025
baa7b21
review | Adhere to coding standards
remipelhate Jan 3, 2025
b685580
review | Use placeholder query in BindDirectiveTest
remipelhate Jan 3, 2025
67d3d6d
review | Remove SpyResolver in favour of inspecting resolver args by …
remipelhate Jan 3, 2025
2ef000c
Fix BindDefinition constructor docblock
remipelhate Jan 3, 2025
c543ee9
Fix BindDirective schema validation tests
remipelhate Jan 3, 2025
b52dc07
review | Reuse existing underlying type name resolution
remipelhate Jan 3, 2025
87c0a72
review | Don’t validate value types on which `@bind` is defined
remipelhate Jan 3, 2025
10a7580
review | Add comment for context on handling “too few” vs “too many” …
remipelhate Jan 3, 2025
28cdfb3
review | Update examples in directives docs
remipelhate Jan 3, 2025
541e796
review | Remove redundant docblock from BindDefinition
remipelhate Jan 3, 2025
c6df8c8
review | Cleanup internals in accordance to coding standard
remipelhate Jan 3, 2025
fd530f6
review | Use protected methods and properties instead of private
remipelhate Jan 3, 2025
2ba7be4
Merge branch 'master' into feature/bind-directive
spawnia Jan 9, 2025
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
Prev Previous commit
Next Next commit
review | Add comment for context on handling “too few” vs “too many” …
…results in ModelBinding
  • Loading branch information
remipelhate committed Jan 3, 2025
commit 10a7580faa9d75f6745b747d27f2d0b0382223b2
9 changes: 7 additions & 2 deletions src/Bind/ModelBinding.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ public function __invoke(int|string|array $value, BindDefinition $definition): M
*/
private function modelInstance(EloquentCollection $results): ?Model
{
// While "too few records" errors are handled as (client-safe) validation errors by applying
// the `BindingExists` rule on the BindDirective depending on whether the binding is required,
// "too many records" should be considered as (non-client-safe) configuration errors as it
// means the binding was not resolved using a unique identifier.
if ($results->count() > 1) {
throw new MultipleRecordsFoundException($results->count());
}
Expand All @@ -43,7 +47,7 @@ private function modelInstance(EloquentCollection $results): ?Model

/**
* Binding collections should be returned with the original values
* as keys to allow us to validate the binding when non-optional.
* as keys to allow validating the binding when required.
* @see \Nuwave\Lighthouse\Bind\BindDirective::rules()
*
* @param \Illuminate\Database\Eloquent\Collection<int, \Illuminate\Database\Eloquent\Model> $results
Expand All @@ -56,10 +60,11 @@ private function modelCollection(
IlluminateCollection $values,
BindDefinition $definition,
): EloquentCollection {
/* @see self::modelInstance() */
if ($results->count() > $values->unique()->count()) {
throw new MultipleRecordsFoundException($results->count());
}

return $results->keyby($definition->column);
return $results->keyBy($definition->column);
}
}