Writing a Plugin
Hive Router is designed to be flexible and extensible, allowing you to customize its behavior to fit your specific needs. This guide demonstrates how to extend the router’s functionality using custom plugins written in Rust.
For technical details and API reference, see the Router Plugin System documentation.
Creating a Custom Plugin
Hive Router is built using Rust, which allows for high performance and safety. One of the powerful features of Hive Router is the ability to create custom builds with your own Rust plugins. This enables you to add new capabilities or modify existing ones to better suit your requirements.
Create a new Rust project
First, ensure you have the necessary development environment set up for Rust.
Next, create a new Rust project for your custom router:
Install Dependencies
Add hive-router and serde as dependencies in your Cargo.toml file:
main and router entrypoint
Next, you need to create an entrypoint for your custom router. This is where you’ll initialize the
router and register your plugins. Create a new file src/main.rs and add the following code:
Configure your Router
To use the Router, you’ll need to create a router.config.yaml file in the root of your project.
This file will contain the configuration for your router, including the supergraph source and any plugins you want to use.
This configuration file must also point to a valid, composed Supergraph file.
If you already have a Supergraph available for testing, place it in your project directory. Alternatively, you can use the example supergraph as a starting point:
Then, point to your Supergraph in your router.config.yaml:
Run your custom router
At this point, you should be able to run your router by executing the following command:
By default, the Router serves on port 4000. Open
http://localhost:4000/graphql in your browser to access the
interactive GraphQL playground.
Create a custom plugin
Now you can create a custom plugin by implementing the
RouterPlugin trait.
Then, create a src/plugin.rs file with the following template:
The plugin above uses the Plugin System Hooks API and hooks into
the on_graphql_params phase to print the received GraphQL operation to the log.
Register your plugin
Now, register your plugin in main.rs:
Enable and configure your plugin
With the plugin registered, the Router is ready to use it. Enable and configure your plugin in the
router.config.yaml file:
Try your plugin
You can now compile and run your Router again:
Use the interactive GraphQL playground at
http://localhost:4000/graphql to run a query. If your plugin is
registered, configured, and enabled correctly, any GraphQL operation will print a log message as
defined in the on_graphql_params hook of the custom plugin.
Build your Router
Now that you have a custom plugin that extends the Router’s behavior, you’ll need to compile it in release mode to use it in production.
To do that, verify that your Cargo.toml file has a defined binary and entrypoint:
Then build using the Rust compiler in release mode:
Your artifact will now be located in ./target/release/hive_router_with_my_plugin and you can run
it as-is.
Distribute your custom Router
We recommend wrapping your custom Router in a Docker image and building it using a Dockerfile:
Then, run the following Docker build command:
After the image is built, you can run a container from it:
Additional Resources
The following links and examples can help you implement custom plugins and extend the Router in different ways.