grafbase_sdk_mock/
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
//! Provides a dynamic GraphQL schema and subgraph implementation that can be built and executed at runtime.
//!
//! This crate allows creating GraphQL schemas dynamically from SDL (Schema Definition Language) strings
//! and executing queries against them. It also provides functionality for running mock GraphQL servers
//! using these dynamic schemas.

#![deny(missing_docs)]

mod builder;
mod entity_resolver;
mod resolver;
mod server;

use std::sync::Arc;

pub use builder::DynamicSchemaBuilder;
pub use server::MockGraphQlServer;

/// A dynamic GraphQL schema that can be built and executed at runtime.
#[derive(Debug)]
pub struct DynamicSchema {
    schema: async_graphql::dynamic::Schema,
    sdl: String,
}

impl DynamicSchema {
    /// Creates a builder for constructing a new dynamic subgraph schema from SDL.
    ///
    /// # Arguments
    ///
    /// * `sdl` - GraphQL schema definition language string to build from
    pub fn builder(sdl: impl AsRef<str>) -> DynamicSchemaBuilder {
        DynamicSchemaBuilder::new(sdl.as_ref())
    }

    /// Executes a GraphQL request against this schema.
    pub async fn execute(&self, request: async_graphql::Request) -> async_graphql::Response {
        self.schema.execute(request).await
    }

    /// Returns the SDL (Schema Definition Language) string for this schema.
    pub fn sdl(&self) -> &str {
        &self.sdl
    }
}

/// A dynamic subgraph implementation that can be started as a mock GraphQL server.
#[derive(Debug)]
pub struct DynamicSubgraph {
    schema: DynamicSchema,
    name: String,
}

impl DynamicSubgraph {
    /// Starts this subgraph as a mock GraphQL server.
    ///
    /// Returns a handle to the running server that can be used to stop it.
    pub async fn start(self) -> MockGraphQlServer {
        MockGraphQlServer::new(self.name, Arc::new(self.schema)).await
    }
}