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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/// This is unstable

mod graphiql;

use crate::{Request, Response, Error, Data, Body};
use crate::header::{self, RequestHeader, Method, StatusCode, Mime};
use crate::routes::Route;
use crate::util::PinnedFuture;
use crate::error::{ClientErrorKind};

use std::any::{Any, TypeId};

use juniper::{
	RootNode, GraphQLType, GraphQLTypeAsync, GraphQLSubscriptionType,
	ScalarValue
};
use juniper::http::{GraphQLRequest, GraphQLBatchRequest};


#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct GraphiQl {
	uri: &'static str,
	graphql_uri: &'static str
}

impl GraphiQl {
	pub const fn new(uri: &'static str, graphql_uri: &'static str) -> Self {
		Self { uri, graphql_uri }
	}
}

impl Route for GraphiQl {
	fn check(&self, header: &RequestHeader) -> bool {
		header.method() == &Method::GET &&
		header.uri().path().starts_with(self.uri)
	}

	fn validate_data(&self, _data: &Data) {}

	fn call<'a>(
		&'a self,
		_req: &'a mut Request,
		_: &'a Data
	) -> PinnedFuture<'a, crate::Result<Response>> {
		PinnedFuture::new(async move {
			Ok(Response::html(
				graphiql::graphiql_source(self.graphql_uri)
			))
		})
	}
}

pub struct GraphQlContext {
	data: Data,
	request_header: RequestHeader
}

impl GraphQlContext {
	// Gets data or RequestHeader
	pub fn get<D>(&self) -> Option<&D>
	where D: Any {
		if TypeId::of::<D>() == TypeId::of::<RequestHeader>() {
			<dyn Any>::downcast_ref(&self.request_header)
		} else {
			self.data.get()
		}
	}
}

impl juniper::Context for GraphQlContext {}


/// This only supports POST requests
pub struct GraphQl<Q, M, Sub, S>
where
	Q: GraphQLType<S, Context=GraphQlContext>,
	M: GraphQLType<S, Context=GraphQlContext>,
	Sub: GraphQLType<S, Context=GraphQlContext>,
	S: ScalarValue
{
	uri: &'static str,
	root_node: RootNode<'static, Q, M, Sub, S>
}

impl<Q, M, Sub, S> GraphQl<Q, M, Sub, S>
where
	Q: GraphQLType<S, Context=GraphQlContext>,
	M: GraphQLType<S, Context=GraphQlContext>,
	Sub: GraphQLType<S, Context=GraphQlContext>,
	S: ScalarValue
{
	pub fn new(
		uri: &'static str,
		root_node: RootNode<'static, Q, M, Sub, S>
	) -> Self {
		Self { uri, root_node }
	}
}

impl<Q, M, Sub, S> Route for GraphQl<Q, M, Sub, S>
where
	Q: GraphQLTypeAsync<S, Context=GraphQlContext> + Send,
	Q::TypeInfo: Send + Sync,
	M: GraphQLTypeAsync<S, Context=GraphQlContext> + Send,
	M::TypeInfo: Send + Sync,
	Sub: GraphQLSubscriptionType<S, Context=GraphQlContext> + Send,
	Sub::TypeInfo: Send + Sync,
	S: ScalarValue + Send + Sync
{
	fn check(&self, header: &RequestHeader) -> bool {
		header.method() == &Method::POST &&
		header.uri().path().starts_with(self.uri)
	}

	fn validate_data(&self, _data: &Data) {}

	fn call<'a>(
		&'a self,
		req: &'a mut Request,
		data: &'a Data
	) -> PinnedFuture<'a, crate::Result<Response>> {
		PinnedFuture::new(async move {
			// get content-type of request
			let content_type = req.header().value(header::CONTENT_TYPE)
				.unwrap_or("");

			let gql_req: GraphQLBatchRequest<S> = match content_type {
				"application/json" => {
					// read json
					req.deserialize().await?
				},
				"application/graphql" => {
					let body = req.body.take().into_string().await
						.map_err(Error::from_client_io)?;

					GraphQLBatchRequest::Single(
						GraphQLRequest::new(body, None, None)
					)
				},
				_ => return Err(ClientErrorKind::BadRequest.into())
			};

			let ctx = GraphQlContext {
				data: data.clone(),
				request_header: req.header().clone()
			};
			let res = gql_req.execute(&self.root_node, &ctx).await;

			let mut resp = Response::builder()
				.content_type(Mime::JSON);

			if !res.is_ok() {
				resp = resp.status_code(StatusCode::BAD_REQUEST);
			}

			Ok(resp.body(Body::serialize(&res).unwrap()).build())
		})
	}
}