grafbase_sdk/test/
mock.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
mod builder;
mod entity_resolver;
mod resolver;
mod server;

use std::{net::SocketAddr, sync::Arc};

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

/// 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())
    }

    async fn execute(&self, request: async_graphql::Request) -> async_graphql::Response {
        self.schema.execute(request).await
    }

    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,
    listen_address: SocketAddr,
}

impl DynamicSubgraph {
    pub(crate) fn name(&self) -> &str {
        &self.name
    }

    pub(crate) fn start(self) -> MockGraphQlServer {
        MockGraphQlServer::new(Arc::new(self.schema), self.listen_address)
    }

    pub(crate) fn sdl(&self) -> &str {
        self.schema.sdl()
    }

    pub(crate) fn url(&self) -> Url {
        format!("http://{}", self.listen_address).parse().unwrap()
    }
}