Repository navigation
feat: Allow Custom Directives #815
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
Merged
oojacoboo
merged 12 commits into
thecodingmachine:master
from
michael-georgiadis:feat/custom-directives-v2
Nov 30, 2026
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
800c894
Implement discovery, registry, wiring
michael-georgiadis bfcffb9
Add unit-tests
michael-georgiadis 501c70b
Add integration tests
michael-georgiadis 295d363
Split validation to separate units
michael-georgiadis e163c38
Improve handling of edge case and remove a now unused method
michael-georgiadis 237f4e3
Enrich E2E test
michael-georgiadis a1c40c8
Trim comments
michael-georgiadis 836165d
Bump version for native webonyx/graphql directive support
michael-georgiadis 0028530
Merge remote-tracking branch 'upstream/master' into feat/custom-direc…
michael-georgiadis 758bfc2
Merge remote-tracking branch 'upstream/master' into feat/custom-direc…
michael-georgiadis b66bb64
Add docs
michael-georgiadis 3e2cdb8
Add `psalm.xml`
michael-georgiadis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,32 @@ | ||
| <?xml version="1.0"?> | ||
| <!-- Psalm only runs as the security scan (taint analysis). PHPStan handles type checks. --> | ||
| <psalm | ||
| errorLevel="8" | ||
| findUnusedCode="false" | ||
| findUnusedBaselineEntry="false" | ||
| ensureOverrideAttribute="false" | ||
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xmlns="https://getpsalm.org/schema/config" | ||
| xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd" | ||
| > | ||
| <projectFiles> | ||
| <directory name="src"/> | ||
| <ignoreFiles> | ||
| <directory name="vendor"/> | ||
| </ignoreFiles> | ||
| </projectFiles> | ||
| <issueHandlers> | ||
| <!-- Only exists in phpdocumentor/type-resolver 1. Both versions are supported. --> | ||
| <UndefinedClass> | ||
| <errorLevel type="suppress"> | ||
| <referencedClass name="phpDocumentor\Reflection\Types\Collection"/> | ||
| </errorLevel> | ||
| </UndefinedClass> | ||
| <!-- Docblock says array, native type is iterable. --> | ||
| <NoValue> | ||
| <errorLevel type="suppress"> | ||
| <file name="src/AggregateQueryProvider.php"/> | ||
| </errorLevel> | ||
| </NoValue> | ||
| </issueHandlers> | ||
| </psalm> |
This file contains hidden or 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 hidden or 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 hidden or 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,71 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace TheCodingMachine\GraphQLite\Directives\Discovery; | ||
|
|
||
| use ReflectionClass; | ||
| use TheCodingMachine\GraphQLite\Directives\TypeSystemDirective; | ||
| use TheCodingMachine\GraphQLite\Discovery\Cache\ClassFinderComputedCache; | ||
| use TheCodingMachine\GraphQLite\Discovery\ClassFinder; | ||
|
|
||
| use function array_reduce; | ||
|
|
||
| /** | ||
| * Finds classes in the configured namespaces that implement {@see TypeSystemDirective}, cached the | ||
| * same way as {@see \TheCodingMachine\GraphQLite\Mappers\ClassFinderTypeMapper}. | ||
| * | ||
| * @internal | ||
| */ | ||
| final class DirectiveClassFinder | ||
| { | ||
| /** @var list<class-string<TypeSystemDirective>>|null */ | ||
| private array|null $cache = null; | ||
|
github-advanced-security[bot] marked this conversation as resolved.
Fixed
|
||
|
|
||
| public function __construct( | ||
|
github-advanced-security[bot] marked this conversation as resolved.
Fixed
|
||
| private readonly ClassFinder $classFinder, | ||
|
github-advanced-security[bot] marked this conversation as resolved.
Fixed
|
||
| private readonly ClassFinderComputedCache $classFinderComputedCache, | ||
|
github-advanced-security[bot] marked this conversation as resolved.
Fixed
|
||
| ) { | ||
| } | ||
|
|
||
| /** @return list<class-string<TypeSystemDirective>> */ | ||
| public function findDirectives(): array | ||
|
github-advanced-security[bot] marked this conversation as resolved.
Fixed
|
||
| { | ||
| if ($this->cache !== null) { | ||
| return $this->cache; | ||
| } | ||
|
|
||
| /** @var list<class-string<TypeSystemDirective>> $result */ | ||
| $result = $this->classFinderComputedCache->compute( | ||
| $this->classFinder, | ||
| 'customDirectives', | ||
| static function (ReflectionClass $refClass): GlobDirectivesCache|null { | ||
| if ($refClass->isAbstract() || $refClass->isInterface() || $refClass->isEnum()) { | ||
| return null; | ||
| } | ||
|
|
||
| if (! $refClass->implementsInterface(TypeSystemDirective::class)) { | ||
| return null; | ||
| } | ||
|
|
||
| /** @var class-string<TypeSystemDirective> $directiveClass */ | ||
|
github-advanced-security[bot] marked this conversation as resolved.
Fixed
|
||
| $directiveClass = $refClass->getName(); | ||
| return new GlobDirectivesCache($directiveClass); | ||
| }, | ||
| static fn (array $entries): array => array_reduce( | ||
| $entries, | ||
| static function (array $carry, GlobDirectivesCache|null $entry): array { | ||
| if ($entry === null) { | ||
| return $carry; | ||
| } | ||
| $carry[] = $entry->directiveClass; | ||
| return $carry; | ||
| }, | ||
| [], | ||
| ), | ||
| ); | ||
|
|
||
| $this->cache = $result; | ||
| return $result; | ||
| } | ||
| } | ||
This file contains hidden or 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,20 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace TheCodingMachine\GraphQLite\Directives\Discovery; | ||
|
|
||
| use TheCodingMachine\GraphQLite\Directives\TypeSystemDirective; | ||
|
|
||
| /** | ||
| * Cache entry for a directive class {@see DirectiveClassFinder} found. | ||
| * | ||
| * @internal | ||
| */ | ||
| final class GlobDirectivesCache | ||
|
github-advanced-security[bot] marked this conversation as resolved.
Fixed
|
||
| { | ||
| /** @param class-string<TypeSystemDirective> $directiveClass */ | ||
| public function __construct(public readonly string $directiveClass) | ||
| { | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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,51 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace TheCodingMachine\GraphQLite\Directives\Validation; | ||
|
|
||
| use Attribute; | ||
| use TheCodingMachine\GraphQLite\Directives\DirectiveDefinition; | ||
| use TheCodingMachine\GraphQLite\Directives\DirectiveLocation; | ||
| use TheCodingMachine\GraphQLite\Directives\Exceptions\InvalidDirectiveException; | ||
|
|
||
| /** | ||
| * Checks a directive's `#[Attribute(...)]` targets cover every GraphQL location it declares. | ||
| * | ||
| * @internal | ||
| */ | ||
| final class AttributeTargetValidator | ||
|
github-advanced-security[bot] marked this conversation as resolved.
Fixed
|
||
| { | ||
| public static function validate(string $directiveClass, DirectiveDefinition $definition, int $phpFlags): void | ||
| { | ||
| foreach ($definition->locations as $location) { | ||
| foreach (self::requiredPhpTargetsFor($location) as $requiredTarget => $label) { | ||
| if (($phpFlags & $requiredTarget) === $requiredTarget) { | ||
| continue; | ||
| } | ||
|
|
||
| throw InvalidDirectiveException::phpTargetMissingForLocation($directiveClass, $location, $label); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** @return array<int, string> required TARGET_* flag => label */ | ||
| private static function requiredPhpTargetsFor(DirectiveLocation $location): array | ||
| { | ||
| return match ($location) { | ||
| DirectiveLocation::FIELD_DEFINITION => [ | ||
| Attribute::TARGET_METHOD => 'TARGET_METHOD', | ||
| Attribute::TARGET_PROPERTY => 'TARGET_PROPERTY', | ||
| ], | ||
| DirectiveLocation::INPUT_FIELD_DEFINITION => [ | ||
| Attribute::TARGET_METHOD => 'TARGET_METHOD', | ||
| Attribute::TARGET_PROPERTY => 'TARGET_PROPERTY', | ||
| Attribute::TARGET_PARAMETER => 'TARGET_PARAMETER', | ||
| ], | ||
| DirectiveLocation::OBJECT => [Attribute::TARGET_CLASS => 'TARGET_CLASS'], | ||
| DirectiveLocation::INPUT_OBJECT => [Attribute::TARGET_CLASS => 'TARGET_CLASS'], | ||
| // No apply hooks for the other locations yet. | ||
| default => [], | ||
| }; | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.