grafbase_hooks/
lib.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#![doc = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/README.md"))]
#![deny(missing_docs)]

#[cfg(feature = "derive")]
pub use grafbase_hooks_derive::grafbase_hooks;

mod hooks;
pub mod host_io;

pub use hooks::{
    hooks, EdgeNodePostExecutionArguments, EdgePostExecutionArguments, EdgePreExecutionArguments, HookExports,
    HookImpls, Hooks, NodePreExecutionArguments, ParentEdgePostExecutionArguments, SubgraphRequest,
};
pub use wit::{
    CacheStatus, Context, Error, ErrorResponse, ExecutedHttpRequest, ExecutedOperation, ExecutedSubgraphRequest,
    FieldError, GraphqlResponseStatus, HeaderError, Headers, LogError, RequestError, SharedContext,
    SubgraphRequestExecutionKind, SubgraphResponse,
};

#[doc(hidden)]
pub fn init_hooks(hooks: fn() -> Box<dyn hooks::Hooks>) {
    // SAFETY: This function is called by the gateway at startup, and the hooks are initialized only once. There can
    // be no hook calls during initialization. A hook call is by definition single-threaded.
    unsafe {
        hooks::HOOKS = Some(hooks());
    }
}

/// Registers the hooks type to the gateway. This macro must be called in the library crate root for the local hooks implementation.
#[macro_export]
macro_rules! register_hooks {
    ($name:ident < ($args:tt)* >) => {
        #[doc(hidden)]
        #[export_name = "init-hooks"]
        pub extern "C" fn __init_hooks() -> i64 {
            grafbase_hooks::init_hooks(|| Box::new(<$name<$($args)*> as grafbase_hooks::Hooks>::new()));
            grafbase_hooks::hooks().hook_implementations() as i64
        }

        impl<$($args)*> grafbase_hooks::HookExports for $name<$($args)*> {}
    };
    ($hook_type:ty) => {
        #[doc(hidden)]
        #[export_name = "init-hooks"]
        pub extern "C" fn __init_hooks() -> i64 {
            grafbase_hooks::init_hooks(|| Box::new(<$hook_type as grafbase_hooks::Hooks>::new()));
            grafbase_hooks::hooks().hook_implementations() as i64
        }

        impl grafbase_hooks::HookExports for $hook_type {}
    };
}

mod wit {
    #![allow(clippy::too_many_arguments, clippy::missing_safety_doc, missing_docs)]

    wit_bindgen::generate!({
        skip: ["init-hooks"],
        path: "./wit/world.wit",
    });
}

struct Component;

wit::export!(Component with_types_in wit);