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
#[cfg(feature = "graphql-protocol")]
pub mod graphql;
pub mod json;

use query_core::{protocol::EngineProtocol, schema::QuerySchemaRef, QueryDocument};

#[derive(Debug)]
pub enum RequestBody {
    #[cfg(feature = "graphql-protocol")]
    Graphql(graphql::GraphqlBody),
    Json(json::JsonBody),
}

impl RequestBody {
    pub fn into_doc(self, query_schema: &QuerySchemaRef) -> crate::Result<QueryDocument> {
        match self {
            #[cfg(feature = "graphql-protocol")]
            RequestBody::Graphql(body) => body.into_doc(),
            RequestBody::Json(body) => body.into_doc(query_schema),
        }
    }

    pub fn try_from_str(val: &str, engine_protocol: EngineProtocol) -> Result<RequestBody, serde_json::Error> {
        match engine_protocol {
            #[cfg(feature = "graphql-protocol")]
            EngineProtocol::Graphql => serde_json::from_str::<graphql::GraphqlBody>(val).map(Self::from),
            EngineProtocol::Json => serde_json::from_str::<json::JsonBody>(val).map(Self::from),
        }
    }

    pub fn try_from_slice(val: &[u8], engine_protocol: EngineProtocol) -> Result<RequestBody, serde_json::Error> {
        match engine_protocol {
            #[cfg(feature = "graphql-protocol")]
            EngineProtocol::Graphql => serde_json::from_slice::<graphql::GraphqlBody>(val).map(Self::from),
            EngineProtocol::Json => serde_json::from_slice::<json::JsonBody>(val).map(Self::from),
        }
    }
}

#[cfg(feature = "graphql-protocol")]
impl From<graphql::GraphqlBody> for RequestBody {
    fn from(body: graphql::GraphqlBody) -> Self {
        Self::Graphql(body)
    }
}

impl From<json::JsonBody> for RequestBody {
    fn from(body: json::JsonBody) -> Self {
        Self::Json(body)
    }
}