Skip to content
Merged
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ You can find and compare releases at the [GitHub release page](https://github.co

## Unreleased

## v6.10.0

### Added

- Allow default model class resolution for `union` types

## v6.9.2

### Fixed
Expand Down
5 changes: 4 additions & 1 deletion src/Schema/AST/ASTHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,10 @@ public static function modelName(Node $definitionNode): ?string
$definitionNode = static::underlyingType($definitionNode);
}

if ($definitionNode instanceof ObjectTypeDefinitionNode || $definitionNode instanceof InterfaceTypeDefinitionNode) {
if ($definitionNode instanceof ObjectTypeDefinitionNode
|| $definitionNode instanceof InterfaceTypeDefinitionNode
|| $definitionNode instanceof UnionTypeDefinitionNode
) {
return ModelDirective::modelClass($definitionNode)
?? $definitionNode->name->value;
}
Expand Down
7 changes: 2 additions & 5 deletions src/Schema/Directives/BaseDirective.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,8 @@ protected function directiveArgValue(string $name, mixed $default = null): mixed
*/
protected function getModelClass(string $argumentName = 'model'): string
{
$model = $this->directiveArgValue($argumentName, ASTHelper::modelName($this->definitionNode));

if (! $model) {
throw new DefinitionException("Could not determine a model name for the '@{$this->name()}' directive on '{$this->nodeName()}.");
}
$model = $this->directiveArgValue($argumentName, ASTHelper::modelName($this->definitionNode))
?? throw new DefinitionException("Could not determine a model name for the '@{$this->name()}' directive on '{$this->nodeName()}'.");

return $this->namespaceModelClass($model);
}
Expand Down
52 changes: 51 additions & 1 deletion tests/Unit/Schema/Directives/BaseDirectiveTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function testGetsModelClassFromDirective(): void
);
}

public function testDefaultsToFieldTypeForTheModelClass(): void
public function testDefaultsToFieldTypeForTheModelClassIfObject(): void
{
$this->schema .= /** @lang GraphQL */ '
type User {
Expand All @@ -55,6 +55,56 @@ public function testDefaultsToFieldTypeForTheModelClass(): void
);
}

public function testDefaultsToFieldTypeForTheModelClassIfInterface(): void
{
$this->schema .= /** @lang GraphQL */ '
interface User {
id: ID
}
';

$directive = $this->constructFieldDirective('foo: User @dummy');

$this->assertSame(
User::class,
$directive->getModelClass(),
);
}

public function testDefaultsToFieldTypeForTheModelClassIfUnion(): void
{
$this->schema .= /** @lang GraphQL */ '
union User = Admin | Member

type Admin {
id: ID
}

type Member {
id: ID
}
';

$directive = $this->constructFieldDirective('foo: User @dummy');

$this->assertSame(
User::class,
$directive->getModelClass(),
);
}

public function testDoesntDefaultToFieldTypeForTheModelClassIfScalar(): void
{
$this->schema .= /** @lang GraphQL */ '
scalar User
';

$directive = $this->constructFieldDirective('foo: User @dummy');

$this->expectException(DefinitionException::class);
$directive->getModelClass();
}

public function testThrowsIfTheClassIsNotInTheSchema(): void
{
$directive = $this->constructFieldDirective('foo: UnknownType @dummy');
Expand Down