pub trait HooksExtension: Sized + 'static {
// Required methods
fn new(config: Configuration) -> Result<Self, Error>;
fn on_request(
&mut self,
url: &str,
method: Method,
headers: &mut GatewayHeaders,
) -> Result<(), ErrorResponse>;
fn on_response(
&mut self,
status: StatusCode,
headers: &mut GatewayHeaders,
event_queue: EventQueue,
) -> Result<(), String>;
}Expand description
The Hooks extension allows you to hook into an incoming request or an outgoing response.
You have mutable access to the headers, and information about the request or response to decide whether to continue processing or not.
Keep in mind this is not meant for authentication purposes.
§Example
use grafbase_sdk::{
HooksExtension,
types::{GatewayHeaders, Configuration, Error, ErrorResponse},
host_io::event_queue::EventQueue,
};
#[derive(HooksExtension)]
struct MyHooks {
config: Config,
}
#[derive(serde::Deserialize)]
struct Config {
// Define your configuration fields here. They are parsed from
// the grafbase.toml configuration.
something: String,
}
impl HooksExtension for MyHooks {
fn new(config: Configuration) -> Result<Self, Error> {
let config = config.deserialize()?;
Ok(Self { config })
}
fn on_request(&mut self, url: &str, method: http::Method, headers: &mut GatewayHeaders) -> Result<(), ErrorResponse> {
// Implement your request hook logic here.
Ok(())
}
fn on_response(
&mut self,
status: http::StatusCode,
headers: &mut GatewayHeaders,
event_queue: EventQueue,
) -> Result<(), String> {
// Implement your response hook logic here.
Ok(())
}
}Required Methods§
Sourcefn new(config: Configuration) -> Result<Self, Error>
fn new(config: Configuration) -> Result<Self, Error>
Creates a new instance of the extension. The Configuration will contain all the
configuration defined in the grafbase.toml by the extension user in a serialized format.
§Example
The following TOML configuration:
[extensions.my-hooks.config]
my_custom_key = "value"can be easily deserialized with:
#[derive(serde::Deserialize)]
struct Config {
my_custom_key: String
}
let config: Config = config.deserialize()?;Sourcefn on_request(
&mut self,
url: &str,
method: Method,
headers: &mut GatewayHeaders,
) -> Result<(), ErrorResponse>
fn on_request( &mut self, url: &str, method: Method, headers: &mut GatewayHeaders, ) -> Result<(), ErrorResponse>
Called immediately when a request is received, before entering the GraphQL engine.
This hook can be used to modify the request headers before they are processed by the GraphQL engine, and provides a way to audit the headers, URL, and method before processing the operation.
Sourcefn on_response(
&mut self,
status: StatusCode,
headers: &mut GatewayHeaders,
event_queue: EventQueue,
) -> Result<(), String>
fn on_response( &mut self, status: StatusCode, headers: &mut GatewayHeaders, event_queue: EventQueue, ) -> Result<(), String>
Called right before the response is sent back to the client.
This hook can be used to modify the response headers before the response is sent back to the client.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.