Skip to content

fix: refuse directives that can never be applied, and correct directive docblocks - #854

Merged
oojacoboo merged 2 commits into
thecodingmachine:masterfrom
oojacoboo:fix/directive-locations-and-docblocks
Dec 1, 2026
Merged

oojacoboo merged 2 commits into
thecodingmachine:masterfrom
oojacoboo:fix/directive-locations-and-docblocks

Conversation

@oojacoboo

Copy link
Copy Markdown
Collaborator

Follow-up to #815 (custom directives). It closes two ways a directive could register and then silently do nothing, and corrects docblocks and docs that describe behavior the code does not have.

Validation

Only FIELD_DEFINITION, INPUT_FIELD_DEFINITION, OBJECT and INPUT_OBJECT have an apply path.

  • A custom directive that declares any other location (type-system or executable), or no location at all, was registered and published in the schema but never applied. It now throws InvalidDirectiveException naming the class, the offending locations and the supported ones. directives.md already said executable directives cannot be defined; nothing enforced it.
  • A builtIn: true directive skipped every location and name check, so any class could set it with a made-up name and register with no definition and no effect. It must now name a directive webonyx defines, declare only locations in webonyx's definition of it, and include at least one location GraphQLite applies. That also rules out builtIn: true overrides of @skip, @include and @specifiedBy, so their reserved-name message no longer suggests it.

The bundled #[Deprecated] and #[OneOf], and a user override of either, keep working. No existing test changed.

Docblocks and docs

  • Ordering. BehavioralFieldDirective said its hook is chained ahead of the field pipe. SchemaFactory pipes the directive middleware last, after the user, security, authorization and cost middlewares. So the hook runs after them at build time, and a resolver installed with withResolver() is the outermost wrapper at request time: code before the wrapped call runs before the #[Logged], #[Right] and #[Security] checks. The same holds for input fields. directives.md now describes both phases, and notes that the #[FailWith] fallback applies to fields only.
  • Visibility. DirectiveAstBuilder and the four directive middlewares said applied directives show up in printed SDL. They live on astNode->directives only; introspection and webonyx's SchemaPrinter expose a custom directive's definition, not its applications (checked at webonyx 15.38.3 and 15.43.0). @deprecated and @oneOf show through the type's own properties.
  • DirectiveDefinition pointed to a DirectiveValidator::isRepeatable() that does not exist; it is DirectiveResolver::isRepeatable().

Checks

phpunit, composer phpstan and composer cs-check pass on PHP 8.2, 8.3, 8.4 and 8.5, with composer update and composer update --prefer-lowest (781 tests).

Only FIELD_DEFINITION, INPUT_FIELD_DEFINITION, OBJECT and INPUT_OBJECT have
an apply path. A custom directive declaring any other location (executable
or type-system), or no location, was registered and published in the
schema but never applied. InterfaceLocationValidator now refuses it with
InvalidDirectiveException::unsupportedLocations() or ::noLocations().
builtIn: true directives are exempt, since webonyx owns their definition.

Also correct docblocks and docs that overstated what directives do:
- a behavioral field or input field directive runs last at build time, so
  its withResolver() wrapper is outermost at request time and runs before
  the #[Logged], #[Right] and #[Security] checks;
- applied directives live on astNode->directives only; introspection and
  SchemaPrinter expose the definitions, not the applications;
- DirectiveDefinition pointed to a nonexistent DirectiveValidator method.
A builtIn: true directive skipped the location rule on trust: any name, with
any or no locations, registered without publishing a definition and was
never applied. InterfaceLocationValidator now requires its name to be a
webonyx built-in (InvalidDirectiveException::unknownBuiltIn()), its
locations to be among webonyx's for that name (::locationsOutsideBuiltIn()),
and at least one location GraphQLite applies (::noLocations() or
::unsupportedLocations()). That last rule also rules out @Skip, @include and
@specifiedBy, so their reserved-name message no longer suggests builtIn: true.

Docs: applied @deprecated and @OneOf do show in introspection and printed
SDL; the FailWith fallback applies to fields, not input fields; the location
rule is part of the custom directives changelog entry.
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.62%. Comparing base (53f9d49) to head (73e8ad7).
⚠️ Report is 192 commits behind head on master.

Additional details and impacted files
@@             Coverage Diff              @@
##             master     #854      +/-   ##
============================================
- Coverage     95.72%   94.62%   -1.11%     
- Complexity     1773     2217     +444     
============================================
  Files           154      208      +54     
  Lines          4586     5913    +1327     
============================================
+ Hits           4390     5595    +1205     
- Misses          196      318     +122     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@oojacoboo
oojacoboo merged commit b1ecc0c into thecodingmachine:master Dec 1, 2026
12 checks passed
@oojacoboo
oojacoboo deleted the fix/directive-locations-and-docblocks branch December 1, 2026 03:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants