grafbase_sdk/
extension.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
#![allow(static_mut_refs)]

pub mod resolver;

pub use resolver::Resolver;

use crate::{
    types::FieldInputs,
    wit::{Directive, Error, ExtensionType, FieldDefinition, FieldOutput, Guest, SharedContext},
    Component,
};

/// A trait representing an extension that can be initialized from schema directives.
///
/// This trait is intended to define a common interface for extensions in Grafbase Gateway,
/// particularly focusing on their initialization. Extensions are constructed using
/// a vector of `Directive` instances provided by the type definitions in the schema.
pub trait Extension {
    /// Creates a new instance of the extension from the given schema directives.
    ///
    /// The directives must be defined in the extension configuration, and written
    /// to the federated schema. The directives are deserialized from their GraphQL
    /// definitions to the corresponding `Directive` instances.
    fn new(schema_directives: Vec<crate::types::Directive>) -> Result<Self, Box<dyn std::error::Error>>
    where
        Self: Sized;
}

impl Guest for Component {
    fn init_gateway_extension(r#type: ExtensionType, directives: Vec<Directive>) -> Result<(), String> {
        match r#type {
            ExtensionType::Resolver => {
                resolver::init(directives.into_iter().map(Into::into).collect()).map_err(|e| e.to_string())
            }
        }
    }

    fn resolve_field(
        context: SharedContext,
        directive: Directive,
        definition: FieldDefinition,
        inputs: Vec<Vec<u8>>,
    ) -> Result<FieldOutput, Error> {
        let result = resolver::get_extension()?.resolve_field(
            context,
            directive.into(),
            definition.into(),
            FieldInputs::new(inputs),
        );

        result.map(Into::into)
    }
}