-
-
Notifications
You must be signed in to change notification settings - Fork 437
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into enable-reporting-client-safe-exceptions
- Loading branch information
Showing
18 changed files
with
2,007 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Nuwave\Lighthouse\Bind; | ||
|
||
use GraphQL\Language\AST\FieldDefinitionNode; | ||
use GraphQL\Language\AST\InputObjectTypeDefinitionNode; | ||
use GraphQL\Language\AST\InputValueDefinitionNode; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Nuwave\Lighthouse\Exceptions\DefinitionException; | ||
|
||
/** @template-covariant TClass of object */ | ||
class BindDefinition | ||
{ | ||
public function __construct( | ||
/** @var class-string<TClass> */ | ||
public string $class, | ||
public string $column, | ||
/** @var array<string> */ | ||
public array $with, | ||
public bool $required, | ||
) {} | ||
|
||
public function validate( | ||
InputValueDefinitionNode $definitionNode, | ||
FieldDefinitionNode|InputObjectTypeDefinitionNode $parentNode, | ||
): void { | ||
$nodeName = $definitionNode->name->value; | ||
$parentNodeName = $parentNode->name->value; | ||
|
||
if (! class_exists($this->class)) { | ||
throw new DefinitionException( | ||
"@bind argument `class` defined on `{$parentNodeName}.{$nodeName}` must be an existing class, received `{$this->class}`.", | ||
); | ||
} | ||
|
||
if ($this->isModelBinding()) { | ||
return; | ||
} | ||
|
||
if (method_exists($this->class, '__invoke')) { | ||
return; | ||
} | ||
|
||
$modelClass = Model::class; | ||
throw new DefinitionException( | ||
"@bind argument `class` defined on `{$parentNodeName}.{$nodeName}` must extend {$modelClass} or define the method `__invoke`, but `{$this->class}` does neither.", | ||
); | ||
} | ||
|
||
public function isModelBinding(): bool | ||
{ | ||
return is_subclass_of($this->class, Model::class); | ||
} | ||
} |
Oops, something went wrong.