Trait HooksExtension

Source
pub trait HooksExtension: Sized + 'static {
    // Required method
    fn new(config: Configuration) -> Result<Self, Error>;

    // Provided methods
    fn on_request(
        &mut self,
        url: &str,
        method: Method,
        headers: &mut GatewayHeaders,
    ) -> Result<impl IntoOnRequestOutput, ErrorResponse> { ... }
    fn on_response(
        &mut self,
        status: StatusCode,
        headers: &mut Headers,
        event_queue: EventQueue,
    ) -> Result<(), Error> { ... }
    fn on_subgraph_request(
        &mut self,
        parts: &mut HttpRequestParts,
    ) -> Result<(), Error> { ... }
}
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, Headers, 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 })
    }

    #[allow(refining_impl_trait)]
    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 Headers,
        event_queue: EventQueue,
    ) -> Result<(), Error> {
        // Implement your response hook logic here.
        Ok(())
    }
}

Required Methods§

Source

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()?;

Provided Methods§

Source

fn on_request( &mut self, url: &str, method: Method, headers: &mut GatewayHeaders, ) -> Result<impl IntoOnRequestOutput, 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.

Source

fn on_response( &mut self, status: StatusCode, headers: &mut Headers, event_queue: EventQueue, ) -> Result<(), Error>

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.

Source

fn on_subgraph_request( &mut self, parts: &mut HttpRequestParts, ) -> Result<(), Error>

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.

Implementors§