Writing native Rust plugins for the Apollo Router
Extend the Apollo Router with custom Rust code
Your federated graph might have specific requirements that aren't supported by the Apollo Router's built-in configuration options. For example, you might need to further customize the behavior of:
- Authentication/authorization
- Logging
- Operation tracing
In these cases, you can create custom plugins for the Apollo Router.
Note: The Apollo Router is made available under the Elastic License v2.0 (ELv2). This applies to its source code and all distributions, including any embedded usage. Read our licensing page for more details.
Planning a plugin
When designing a new plugin, you first want to determine which of the Apollo Router's services the plugin should hook into to achieve its use case.
For descriptions of these services, see How router plugins work.
Building a plugin
To demonstrate building a plugin, we'll walk through the hello world example plugin in the Router repo.
1. Add modules
Most plugins should start by including the following set of use
declarations:
use apollo_router_core::plugin::Plugin;use apollo_router_core::{register_plugin, ExecutionRequest, ExecutionResponse, QueryPlannerRequest,QueryPlannerResponse, RouterRequest, RouterResponse, SubgraphRequest, SubgraphResponse,};use schemars::JsonSchema;use serde::Deserialize;use tower::util::BoxService;use tower::{BoxError, ServiceBuilder, ServiceExt};
When your plugin is complete, the compiler will provide helpful warnings if any of these modules aren't necessary. Your plugin can also use
modules from other crates as needed.
2. Define your configuration
All plugins require an associated configuration. At a minimum, this configuration contains a boolean that indicates whether the plugin is enabled, but it can include anything that can be deserialized by serde
.
Create your configuration struct like so:
#[derive(Debug, Default, Deserialize, JsonSchema)]struct Conf {// Put your plugin confguration here. It's deserialzed from YAML automatically.}
Note: You need to derive
JsonSchema
so that your configuration can participate in JSON schema generation.
Then define the plugin itself and specify the configuration as an associated type:
#[async_trait::async_trait]impl Plugin for HelloWorld {type Config = Conf;}
3. Implement the Plugin
trait
All router plugins must implement the Plugin
trait. This trait defines lifecycle hooks that enable hooking into Apollo Router services.
The trait also provides a default implementations for each hook, which returns the associated service unmodified.
// This is a bare-bones plugin that you can duplicate when creating your own.#[async_trait::async_trait]impl Plugin for HelloWorld {type Config = Conf;// This is invoked once after the router starts and compiled-in// plugins are registeredfn new(configuration: Self::Config) -> Result<Self, BoxError> {Ok(HelloWorld { configuration })}/// This is invoked after all plugins have been created and we're ready to go live./// This method MUST not panic.fn activate(&mut self);// Only define the hooks you need to modify. Each default hook// implementation returns its associated service with no changes.fn router_service(&mut self,service: BoxService<RouterRequest, RouterResponse, BoxError>,) -> BoxService<RouterRequest, RouterResponse, BoxError> {service}fn query_planning_service(&mut self,service: BoxService<QueryPlannerRequest, QueryPlannerResponse, BoxError>,) -> BoxService<QueryPlannerRequest, QueryPlannerResponse, BoxError> {service}fn execution_service(&mut self,service: BoxService<ExecutionRequest, ExecutionResponse, BoxError>,) -> BoxService<ExecutionRequest, ExecutionResponse, BoxError> {service}// Unlike other hooks, this hook also passes the name of the subgraph// being invoked. That's because this service might invoke *multiple*// subgraphs for a single request, and this is called once for each.fn subgraph_service(&mut self,name: &str,service: BoxService<SubgraphRequest, SubgraphResponse, BoxError>,) -> BoxService<SubgraphRequest, SubgraphResponse, BoxError> {service}}
4. Define individual hooks
To define custom logic for a service hook, you can use ServiceBuilder
.
ServiceBuilder
provides common building blocks that remove much of the complexity of writing a plugin. These building blocks are called layers.
// Replaces the default definition in the example aboveuse tower::ServiceBuilderExt;use apollo_router_core::ServiceBuilderExt as ApolloServiceBuilderExt;fn router_service(&mut self,service: BoxService<RouterRequest, RouterResponse, BoxError>,) -> BoxService<RouterRequest, RouterResponse, BoxError> {// Always use service builder to compose your plugins.// It provides off-the-shelf building blocks for your plugin.ServiceBuilder::new()// Some example service builder methods:// .map_request()// .map_response()// .rate_limit()// .checkpoint()// .timeout().service(service).boxed()}
The tower-rs library (which the Apollo Router is built on) comes with many "off-the-shelf" layers. In addition, Apollo provides layers that cover common functionality and integration with third-party products.
Some notable layers are:
- buffered - Make a service
Clone
. Typically requred for anyasync
layers. - checkpoint - Perform a sync call to decide if a request should proceed or not. Useful for validation.
- checkpoint_async - Perform an async call to decide if the request should proceed or not. e.g. for Authentication. Requires
buffered
. - instrument - Add a tracing span around a service.
- map_reqeust - Transform the request before proceeding. e.g. for header manipulation.
- map_response - Transform the response before proceeding. e.g. for header manipulation.
Before implementing a layer yourself, always check whether an existing layer implementation might fit your needs. Reusing layers is significantly faster than implementing layers from scratch.
5. Define necessary context
Sometimes you might need to pass custom information between services. For example:
- Authentication information obtained by the
RouterService
might be required bySubgraphService
s. - Cache control headers from
SubgraphService
s might be aggregated and returned to the client by theRouterService
.
Whenever the Apollo Router receives a request, it creates a corresponding context
object and passes it along to each service. This object can store anything that's Serde-compatible (e.g., all simple types or a custom type).
All of your plugin's hooks can interact with the context
object using the following functions:
insert
context.insert("key1", 1)?;
Adds a value to the context
object. Serialization and deserialization happen automatically. You might sometimes need to specify the type in cases where the Rust compiler can't figure it out by itself.
If multiple threads might write a value to the same context
key, use upsert
instead.
get
let value : u32 = context.get("key1")?;
Fetches a value from the context
object.
upsert
context.upsert("key1", |v: u32| v + 1)?;
Use upsert
if you might need to resolve multiple simultaneous writes to a single context
key (this is most likely for the subgraph_service
hook, because it might be called by multiple threads in parallel). Rust is multi-threaded, and you will get unexpected results if multiple threads write to context
at the same time. This function prevents issues by guaranteeing that modifications happen serially.
Note: upsert
requires v to implement Default
.
6. Register your plugin
To enable the Apollo Router to discover your plugin, you need to register the plugin.
To do so, use the register_plugin!()
macro provided by apollo-router-core
. This takes 3 arguments:
- A group name
- A plugin name
- A struct implementing the
Plugin
trait
For example:
register_plugin!("example", "hello_world", HelloWorld);
Choose a group name that represents your organization and a name that represents your plugin's functionality.
7. Configure your plugin
After you register your plugin, you can add custom configuration for it to your YAML configuration file in the plugins:
section:
plugins:example.hello_world:# Any values here are passed to the plugin as part of your configuration
Plugin Lifecycle
Like individual requests, plugins follow their own strict lifecycle that helps provide structure to the Apollo Router's execution.
Creation
When the router starts, all plugins included in the executable are registered. At this time, the router calls the new
method of all plugins. If any of these methods fail, the router terminates with helpful error messages.
There is no sequencing for plugin registration, and registrations might even execute in parallel. A plugin should never rely on the existence of another plugin during initialization.
Activate
When the router is ready to start serving requests, it calls each plugin's activate
method. Plugins are started in the same order they're declared in your YAML configuration file.
Note that if a plugin is registered but is not listed in the configuration file, the router does not call startup
on it. If any plugin fails to start, the router terminates with helpful error messages.
Lifecycle notes
If a router is listening for dynamic changes to its configuration, it also triggers lifecycle events when those changes occur.
Before switching to an updated configuration, the router ensures that the new configuration is valid. This process includes starting up replacement plugins for the new configuration. This means that a plugin should not assume that it's the only executing instance of that plugin in a single router.
After the new configuration is deemed valid, the router shifts to it. The previous configuration is dropped and its corresponding plugins are shut down. Errors during the shutdown of these plugins are logged and do not affect router execution.
Testing plugins
Unit testing of a plugin is typically most helpful and there are extensive examples of plugin testing in the examples and plugins directories.