Apollo Router Plugin Creator
Create native Rust plugins for Apollo Router.
Request Lifecycle
Service Hooks
Service Overview
Signatures:
Individual Hooks (Tower Layers)
Use ServiceBuilder to compose these hooks within any service:
Choosing a Service Hook
By data needed:
- HTTP headers only →
router_service - GraphQL query/variables →
supergraph_service - Query plan →
execution_service - Per-subgraph control →
subgraph_service
By timing:
- Before GraphQL parsing →
router_servicerequest - After parsing, before planning →
supergraph_servicerequest - After planning, before execution →
execution_servicerequest - Before/after each subgraph call →
subgraph_service - Final response to client →
router_serviceresponse
See references/service-hooks.md [blocked] for implementation patterns.
Quick Start
Step 1: Create Plugin File
Create a new file src/plugins/my_plugin.rs with required imports:
Step 2: Define Configuration Struct
Every plugin needs a configuration struct with Deserialize and JsonSchema derives. The JsonSchema enables configuration validation in editors:
Step 3: Define Plugin Struct
Step 4: Implement Plugin Trait
Implement the Plugin trait with the required Config type and new constructor:
Step 5: Add Service Hooks
Choose which service(s) to hook based on your requirements, see Service Overview for details.
Example service hook:
Step 6: Register Plugin
At the bottom of your plugin file, register it with the router:
Step 7: Add Module to mod.rs
In src/plugins/mod.rs, add your module:
Step 8: Configure in YAML
Enable your plugin in the router configuration:
Common Patterns
For implementation patterns and code examples, see references/service-hooks.md [blocked]:
- Enable/disable pattern
- Request/response transformation (
map_request,map_response) - Checkpoint (early return/short-circuit)
- Context passing between hooks
- Async operations (
checkpoint_async,buffered) - Error response builders
Examples
Apollo Router Examples
Located in the Apollo Router plugins directory:
For full code examples and testing patterns, see references/examples.md [blocked].
Prerequisites
It is advised to have the rust-best-practices skill installed for writing idiomatic Rust code when developing router plugins. If installed, follow those best practices when generating or modifying plugin code.
Resources
- references/service-hooks.md [blocked] - Detailed service hook implementations
- references/existing-plugins.md [blocked] - Index of existing plugins
- references/examples.md [blocked] - Full code examples and testing patterns
- Apollo Router plugins: https://github.com/apollographql/router/tree/dev/apollo-router/src/plugins

