diff --git a/CHANGELOG.md b/CHANGELOG.md index 058eab2b4..4001e0868 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/Schema/DirectiveLocator.php b/src/Schema/DirectiveLocator.php index 3242c81a3..5b66df01a 100644 --- a/src/Schema/DirectiveLocator.php +++ b/src/Schema/DirectiveLocator.php @@ -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', @@ -77,8 +79,6 @@ public function namespaces(): array */ public function classes(): array { - $directives = []; - foreach ($this->namespaces() as $directiveNamespace) { /** @var array $classesInNamespace */ $classesInNamespace = ClassFinder::getClassesInNamespace($directiveNamespace); @@ -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; } } - return $directives; + return $this->resolvedClassnames; } /** diff --git a/tests/Unit/Schema/DirectiveLocatorTest.php b/tests/Unit/Schema/DirectiveLocatorTest.php index e62bb3a06..e1b3f83ac 100644 --- a/tests/Unit/Schema/DirectiveLocatorTest.php +++ b/tests/Unit/Schema/DirectiveLocatorTest.php @@ -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 @@ -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);