Creating a Custom Apollo Router Core Binary
Compile a custom router binary from source
Learn how to compile a custom binary from Apollo Router Core source, which is required to create custom native Rust plugins for the router.
⚠️ Apollo doesn't recommend creating native plugins for the Apollo Router Core or GraphOS Router, for the following reasons:
Native plugins require familiarity with programming in Rust.
Native plugins require compiling a custom router binary from source, which can introduce unexpected behavior in your router that's difficult to diagnose and support.
Instead, for most router customizations, Apollo recommends creating either a Rhai script or an external coprocessor. Both of these customizations are supported by Apollo and provide strong separation of concerns and fault isolation.
If you must create a native plugin, please open a GitHub issue, and Apollo can investigate adding the custom capability to the stock router binary.
Prerequisites
To compile the router, you need to have Rust 1.85.0 or later installed.
1. Create a new project
Use the
cargo new
command to create a project for your custom router:Bash1cargo new --bin starstuff
For the purposes of this tutorial, set your project's name to starstuff
.
After your project is created, change to the
starstuff
directory:Bash1cd starstuff
Write the source code for your custom binary.
2. Compile the router
Create a debug build of the router with the following command:
1cargo build
The resulting debug binary is located in target/debug/router
.
To create a release build for production environments, use this command instead:
1cargo build --release
The resulting release binary is now located in target/release/router
.
3. Run the compiled binary
Now you can test out your compiled router with an example supergraph schema.
Download the example schema with the following command:
Bash1curl -sSL https://supergraph.demo.starstuff.dev/ > supergraph-schema.graphql
Run the router and provide the example schema like so:
Bash1cargo run -- --hot-reload --config router.yaml --supergraph supergraph-schema.graphql
During development, it's helpful to use
cargo run
to run the router.
If you're using managed federation, you set the APOLLO_KEY
and APOLLO_GRAPH_REF
environment variables instead of specifying the supergraph schema as a file. For details, see this section.
4. Create a plugin
From within your project directory, implement your new plugin.
Add configuration options for the created plugin to your
router.yaml
file:YAMLrouter.yaml1plugins: 2 starstuff.hello_world: 3 message: "starting my plugin"
Run the router again:
Bash1cargo run -- --hot-reload --config router.yaml --supergraph supergraph-schema.graphql
This time, you should see a log line like the following:
Bash12022-05-21T09:16:33.160288Z INFO router::plugins::hello_world: starting my plugin
Nice work! You now have a custom router binary with an associated plugin. Next, you can extend the plugin with the functionality you need or add more plugins.
Memory allocator
On Linux the apollo-router
crate sets jemalloc
as the global memory allocator for Rust
to reduce memory fragmentation.
Future versions may do so on more platforms, or switch to yet a different allocator.
This is enabled by default and controlled by a global-allocator
Cargo feature flag.
If you want to choose a different allocator, disable it in your Cargo.toml
:
1[dependencies]
2apollo-router = {version = "[…]", default-features = false}
If you make a library crate, also specify default-features = false
in order to leave the choice open for the eventual executable crate.
(Cargo default features are only disabled if all dependents specify default-features = false
.)