HooksExtension

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,
        ctx: &RequestContext,
        status: StatusCode,
        headers: &mut Headers,
        event_queue: EventQueue,
    ) -> Result<(), Error> { ... }
    fn on_graphql_subgraph_request(
        &mut self,
        ctx: &AuthorizedOperationContext,
        subgraph_name: &str,
        parts: &mut HttpRequestParts,
    ) -> Result<(), Error> { ... }
    fn on_virtual_subgraph_request(
        &mut self,
        ctx: &AuthorizedOperationContext,
        subgraph_name: &str,
        headers: &mut Headers,
    ) -> 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, RequestContext},
    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,
        ctx: &RequestContext,
        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.

It can also be used to define the contract key to use for the contracts extension if you have any configured:

#[allow(refining_impl_trait)]
fn on_request(&mut self, url: &str, method: Method, headers: &mut GatewayHeaders) -> Result<OnRequestOutput, ErrorResponse> {
    Ok(OnRequestOutput::new().contract_key("my-contract-key"))
}
Source

fn on_response( &mut self, ctx: &RequestContext, 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_graphql_subgraph_request( &mut self, ctx: &AuthorizedOperationContext, subgraph_name: &str, parts: &mut HttpRequestParts, ) -> Result<(), Error>

Called when a GraphQL subgraph request is made, allowing you to modify the request parts before they are sent to the subgraph.

Source

fn on_virtual_subgraph_request( &mut self, ctx: &AuthorizedOperationContext, subgraph_name: &str, headers: &mut Headers, ) -> Result<(), Error>

Called when a virtual subgraph request is made through an extension, allowing you to modify the request headers before sending it to the extension.

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§