GraphQL / Advanced: bounded and distributed execution / Lesson 21
Schema directives and enforcement boundaries
Document a schema directive’s meaning and test the server implementation that enforces it.
On this page
Lesson goals
- Distinguish directive declaration from server behavior.
- Describe a field policy with a schema directive.
- Test enforcement, nested access and schema evolution.
Define the directive contract
A schema directive can attach metadata to a schema element, but its declaration alone does not implement application behavior. Specify its locations, arguments, semantics and owner. The GraphQL specification defines directive syntax and validation; a custom authorization directive requires server-specific code to interpret it. Never assume that a visually annotated field is protected until the running server’s enforcement path is tested.
Annotate an example field
The schema fragment declares a requiresRole directive and places it on a synthetic report field. It is documentation and schema metadata, not an implementation of role checking. The application must define what the role means, how the principal is established and how denial is represented. A broad role may still be insufficient for tenant or object-level access, so combine it with an explicit resource policy.
directive @requiresRole(role: String!) on FIELD_DEFINITION
type Query {
report(id: ID!): Report @requiresRole(role: "reader")
}
type Report { id: ID! title: String! }
The directive and field are schema examples only. They provide no enforcement without server-specific implementation.
Enforce in the chosen server
The pseudocode fragment illustrates the intended decision rather than a portable GraphQL execution hook. A real implementation must integrate with its chosen server, inspect the authenticated principal and apply the directive consistently to every applicable field. Define behavior when the directive is absent or misconfigured, and fail safely according to the application’s contract. Keep internal policy details and credentials out of public errors.
if principal is absent or lacks required role:
deny field safely
if principal cannot access requested resource:
deny field safely
return authorized field result
This pseudocode is not a GraphQL server API. The real implementation must integrate with the chosen execution framework and resource policy.
Consider nested and evolving access
A protected object may be reachable through more than one root field, fragment or nested relationship. Test each path and any batching or cache layer with different synthetic identities. Schema composition, code generation or a new resolver can accidentally bypass an implementation that relies on a specific wiring path. Review the directive and policy when the schema evolves, and ensure cache keys include relevant identity dimensions.
Prove actual enforcement
In an isolated server, execute the same field as an allowed principal, a denied principal and an unauthenticated caller. Inspect data, errors and null propagation, then verify no unauthorized durable effect for mutations. Test a nested path and a different tenant as well. Schema validation can prove directive syntax and placement, but only execution tests of the deployed wiring can establish its authorization behavior.
Common errors and fixes
A directive annotation is assumed to enforce access
- Why it happens
- The server never registered implementation logic.
- How to fix it
- Wire and test enforcement in the chosen server.
- Prevent it next time
- Run allowed and denied operations against the actual schema.
A role-protected object leaks through another path
- Why it happens
- Only one resolver or root field applied the policy.
- How to fix it
- Apply object-level authorization consistently at all access paths.
- Prevent it next time
- Test nested fields and alternate entry points.
Cached data crosses users
- Why it happens
- The cache ignored principal or tenant scope.
- How to fix it
- Scope the cache or key it by all relevant dimensions.
- Prevent it next time
- Test the same object under two identities.
2 practice exercises
1. Does declaring @requiresRole make a field secure?
Show worked solution
No. It defines schema metadata. The selected server must implement and invoke the policy, establish the principal and enforce object scope. Verify allowed, denied and unauthenticated executions.
2. Why test a nested path to the same object?
Show worked solution
The object may be reached through another resolver that bypasses a policy applied only at one root. Test alternate paths and ensure authorization and cache scope are consistent.
Key takeaways
- A custom directive is metadata until server code enforces its semantics.
- Authorization must cover every path and cache boundary for a resource.