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

## Unreleased

### Changed

- Classes passed to `DirectiveLocator::setResolved()` take precedence in `DirectiveLocator::classes()` and `DirectiveLocator::definitions()` https://github.com/nuwave/lighthouse/pull/2787

## v6.69.2

### Fixed
Expand Down
8 changes: 4 additions & 4 deletions src/Schema/DirectiveLocator.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ class DirectiveLocator
/**
* A map from short directive names to full class names.
*
* Takes precedence over the namespaces.
*
* E.g.
* [
* 'create' => 'Nuwave\Lighthouse\Schema\Directives\CreateDirective',
Expand Down Expand Up @@ -77,8 +79,6 @@ public function namespaces(): array
*/
public function classes(): array
{
$directives = [];

foreach ($this->namespaces() as $directiveNamespace) {
/** @var array<class-string> $classesInNamespace */
$classesInNamespace = ClassFinder::getClassesInNamespace($directiveNamespace);
Expand All @@ -94,11 +94,11 @@ public function classes(): array
}

// Only add the first directive that was found
$directives[self::directiveName($class)] ??= $class;
$this->resolvedClassnames[self::directiveName($class)] ??= $class;
Comment thread
spawnia marked this conversation as resolved.
}
}

return $directives;
return $this->resolvedClassnames;
}

/**
Expand Down
8 changes: 8 additions & 0 deletions tests/Unit/Schema/DirectiveLocatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Nuwave\Lighthouse\Support\Contracts\FieldMiddleware;
use Nuwave\Lighthouse\Support\Contracts\FieldResolver;
use Nuwave\Lighthouse\Support\Utils;
use Tests\Integration\Events\FieldDirective as AlternateFieldDirective;
use Tests\TestCase;

final class DirectiveLocatorTest extends TestCase
Expand Down Expand Up @@ -77,6 +78,13 @@ public function handleField(FieldValue $fieldValue): void {}
$this->assertNotInstanceOf(BaseDirective::class, $directive);
}

public function testResolvesExplicitlySetClassInsteadOfScannedNamespaces(): void
{
$this->directiveLocator->setResolved('field', AlternateFieldDirective::class);

$this->assertSame(AlternateFieldDirective::class, $this->directiveLocator->classes()['field']);
}

public function testThrowsIfDirectiveNameCanNotBeResolved(): void
{
$this->expectException(DirectiveException::class);
Expand Down
Loading