-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathservice.rs
490 lines (428 loc) · 16.5 KB
/
service.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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
use std::convert::TryFrom;
use std::fmt;
use std::pin::Pin;
use std::task::Context;
use std::task::Poll;
use std::time::Instant;
use graph::data::query::QueryResults;
use graph::prelude::*;
use graph::{components::server::query::GraphQLServerError, data::query::QueryTarget};
use http::header;
use http::header::{
ACCESS_CONTROL_ALLOW_HEADERS, ACCESS_CONTROL_ALLOW_METHODS, ACCESS_CONTROL_ALLOW_ORIGIN,
CONTENT_TYPE, LOCATION,
};
use hyper::service::Service;
use hyper::{Body, Method, Request, Response, StatusCode};
use crate::request::parse_graphql_request;
pub struct GraphQLServiceMetrics {
query_execution_time: Box<HistogramVec>,
}
impl fmt::Debug for GraphQLServiceMetrics {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "GraphQLServiceMetrics {{ }}")
}
}
impl GraphQLServiceMetrics {
pub fn new(registry: Arc<dyn MetricsRegistry>) -> Self {
let query_execution_time = registry
.new_histogram_vec(
"query_execution_time",
"Execution time for successful GraphQL queries",
vec![String::from("deployment"), String::from("status")],
vec![0.1, 0.5, 1.0, 10.0, 100.0],
)
.expect("failed to create `query_execution_time` histogram");
Self {
query_execution_time,
}
}
pub fn observe_query(&self, duration: Duration, results: &QueryResults) {
let id = results
.deployment_hash()
.map(|h| h.as_str())
.unwrap_or_else(|| {
if results.not_found() {
"notfound"
} else {
"unknown"
}
});
let status = if results.has_errors() {
"failed"
} else {
"success"
};
self.query_execution_time
.with_label_values(&[id, status])
.observe(duration.as_secs_f64());
}
}
pub type GraphQLServiceResult = Result<Response<Body>, GraphQLServerError>;
/// An asynchronous response to a GraphQL request.
pub type GraphQLServiceResponse =
Pin<Box<dyn std::future::Future<Output = GraphQLServiceResult> + Send>>;
/// A Hyper Service that serves GraphQL over a POST / endpoint.
#[derive(Debug)]
pub struct GraphQLService<Q> {
logger: Logger,
metrics: Arc<GraphQLServiceMetrics>,
graphql_runner: Arc<Q>,
ws_port: u16,
node_id: NodeId,
}
impl<Q> Clone for GraphQLService<Q> {
fn clone(&self) -> Self {
Self {
logger: self.logger.clone(),
metrics: self.metrics.clone(),
graphql_runner: self.graphql_runner.clone(),
ws_port: self.ws_port,
node_id: self.node_id.clone(),
}
}
}
impl<Q> GraphQLService<Q>
where
Q: GraphQlRunner,
{
/// Creates a new GraphQL service.
pub fn new(
logger: Logger,
metrics: Arc<GraphQLServiceMetrics>,
graphql_runner: Arc<Q>,
ws_port: u16,
node_id: NodeId,
) -> Self {
GraphQLService {
logger,
metrics,
graphql_runner,
ws_port,
node_id,
}
}
fn graphiql_html(&self) -> String {
include_str!("../assets/index.html")
.replace("__WS_PORT__", format!("{}", self.ws_port).as_str())
}
async fn index(self) -> GraphQLServiceResult {
Ok(Response::builder()
.status(200)
.header(ACCESS_CONTROL_ALLOW_ORIGIN, "*")
.header(CONTENT_TYPE, "text/plain")
.body(Body::from(String::from(
"Access deployed subgraphs by deployment ID at \
/subgraphs/id/<ID> or by name at /subgraphs/name/<NAME>",
)))
.unwrap())
}
/// Serves a dynamically created file.
fn serve_dynamic_file(&self, contents: String) -> GraphQLServiceResponse {
async {
Ok(Response::builder()
.status(200)
.header(ACCESS_CONTROL_ALLOW_ORIGIN, "*")
.header(CONTENT_TYPE, "text/html")
.body(Body::from(contents))
.unwrap())
}
.boxed()
}
fn handle_graphiql(&self) -> GraphQLServiceResponse {
self.serve_dynamic_file(self.graphiql_html())
}
async fn handle_graphql_query_by_name(
self,
subgraph_name: String,
request: Request<Body>,
) -> GraphQLServiceResult {
let subgraph_name = SubgraphName::new(subgraph_name.as_str()).map_err(|()| {
GraphQLServerError::ClientError(format!("Invalid subgraph name {:?}", subgraph_name))
})?;
self.handle_graphql_query(subgraph_name.into(), request.into_body())
.await
}
fn handle_graphql_query_by_id(
self,
id: String,
request: Request<Body>,
) -> GraphQLServiceResponse {
let res = DeploymentHash::new(id)
.map_err(|id| GraphQLServerError::ClientError(format!("Invalid subgraph id `{}`", id)));
match res {
Err(_) => self.handle_not_found(),
Ok(id) => self
.handle_graphql_query(id.into(), request.into_body())
.boxed(),
}
}
async fn handle_graphql_query(
self,
target: QueryTarget,
request_body: Body,
) -> GraphQLServiceResult {
let service = self.clone();
let service_metrics = self.metrics.clone();
let start = Instant::now();
let body = hyper::body::to_bytes(request_body)
.map_err(|_| GraphQLServerError::InternalError("Failed to read request body".into()))
.await?;
let query = parse_graphql_request(&body);
let result = match query {
Ok(query) => service.graphql_runner.run_query(query, target).await,
Err(GraphQLServerError::QueryError(e)) => QueryResult::from(e).into(),
Err(e) => return Err(e),
};
service_metrics.observe_query(start.elapsed(), &result);
Ok(result.as_http_response())
}
// Handles OPTIONS requests
fn handle_graphql_options(&self, _request: Request<Body>) -> GraphQLServiceResponse {
async {
Ok(Response::builder()
.status(200)
.header(ACCESS_CONTROL_ALLOW_ORIGIN, "*")
.header(ACCESS_CONTROL_ALLOW_HEADERS, "Content-Type, User-Agent")
.header(ACCESS_CONTROL_ALLOW_METHODS, "GET, OPTIONS, POST")
.header(CONTENT_TYPE, "text/html")
.body(Body::from(""))
.unwrap())
}
.boxed()
}
/// Handles 302 redirects
async fn handle_temp_redirect(self, destination: String) -> GraphQLServiceResult {
header::HeaderValue::try_from(destination)
.map_err(|_| {
GraphQLServerError::ClientError("invalid characters in redirect URL".into())
})
.map(|loc_header_val| {
Response::builder()
.status(StatusCode::FOUND)
.header(ACCESS_CONTROL_ALLOW_ORIGIN, "*")
.header(LOCATION, loc_header_val)
.header(CONTENT_TYPE, "text/plain")
.body(Body::from("Redirecting..."))
.unwrap()
})
}
/// Handles 404s.
fn handle_not_found(&self) -> GraphQLServiceResponse {
async {
Ok(Response::builder()
.status(StatusCode::NOT_FOUND)
.header(CONTENT_TYPE, "text/plain")
.header(ACCESS_CONTROL_ALLOW_ORIGIN, "*")
.body(Body::from("Not found"))
.unwrap())
}
.boxed()
}
fn handle_call(self, req: Request<Body>) -> GraphQLServiceResponse {
let method = req.method().clone();
let path = req.uri().path().to_owned();
let path_segments = {
let mut segments = path.split('/');
// Remove leading '/'
assert_eq!(segments.next(), Some(""));
segments.collect::<Vec<_>>()
};
match (method, path_segments.as_slice()) {
(Method::GET, [""]) => self.index().boxed(),
(Method::GET, &["subgraphs", "id", _, "graphql"])
| (Method::GET, &["subgraphs", "name", _, "graphql"])
| (Method::GET, &["subgraphs", "name", _, _, "graphql"])
| (Method::GET, &["subgraphs", "network", _, _, "graphql"])
| (Method::GET, &["subgraphs", "graphql"]) => self.handle_graphiql(),
(Method::GET, path @ ["subgraphs", "id", _])
| (Method::GET, path @ ["subgraphs", "name", _])
| (Method::GET, path @ ["subgraphs", "name", _, _])
| (Method::GET, path @ ["subgraphs", "network", _, _])
| (Method::GET, path @ ["subgraphs"]) => {
let dest = format!("/{}/graphql", path.join("/"));
self.handle_temp_redirect(dest).boxed()
}
(Method::POST, &["subgraphs", "id", subgraph_id]) => {
self.handle_graphql_query_by_id(subgraph_id.to_owned(), req)
}
(Method::OPTIONS, ["subgraphs", "id", _]) => self.handle_graphql_options(req),
(Method::POST, &["subgraphs", "name", subgraph_name]) => self
.handle_graphql_query_by_name(subgraph_name.to_owned(), req)
.boxed(),
(Method::POST, ["subgraphs", "name", subgraph_name_part1, subgraph_name_part2]) => {
let subgraph_name = format!("{}/{}", subgraph_name_part1, subgraph_name_part2);
self.handle_graphql_query_by_name(subgraph_name, req)
.boxed()
}
(Method::POST, ["subgraphs", "network", subgraph_name_part1, subgraph_name_part2]) => {
let subgraph_name =
format!("network/{}/{}", subgraph_name_part1, subgraph_name_part2);
self.handle_graphql_query_by_name(subgraph_name, req)
.boxed()
}
(Method::OPTIONS, ["subgraphs", "name", _])
| (Method::OPTIONS, ["subgraphs", "name", _, _])
| (Method::OPTIONS, ["subgraphs", "network", _, _]) => self.handle_graphql_options(req),
_ => self.handle_not_found(),
}
}
}
impl<Q> Service<Request<Body>> for GraphQLService<Q>
where
Q: GraphQlRunner,
{
type Response = Response<Body>;
type Error = GraphQLServerError;
type Future = GraphQLServiceResponse;
fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
}
fn call(&mut self, req: Request<Body>) -> Self::Future {
let logger = self.logger.clone();
let service = self.clone();
// Returning Err here will prevent the client from receiving any response.
// Instead, we generate a Response with an error code and return Ok
Box::pin(async move {
let result = service.handle_call(req).await;
match result {
Ok(response) => Ok(response),
Err(err @ GraphQLServerError::ClientError(_)) => Ok(Response::builder()
.status(400)
.header(CONTENT_TYPE, "text/plain")
.header(ACCESS_CONTROL_ALLOW_ORIGIN, "*")
.body(Body::from(err.to_string()))
.unwrap()),
Err(err @ GraphQLServerError::QueryError(_)) => {
error!(logger, "GraphQLService call failed: {}", err);
Ok(Response::builder()
.status(400)
.header(CONTENT_TYPE, "text/plain")
.header(ACCESS_CONTROL_ALLOW_ORIGIN, "*")
.body(Body::from(format!("Query error: {}", err)))
.unwrap())
}
Err(err @ GraphQLServerError::InternalError(_)) => {
error!(logger, "GraphQLService call failed: {}", err);
Ok(Response::builder()
.status(500)
.header(CONTENT_TYPE, "text/plain")
.header(ACCESS_CONTROL_ALLOW_ORIGIN, "*")
.body(Body::from(format!("Internal server error: {}", err)))
.unwrap())
}
}
})
}
}
#[cfg(test)]
mod tests {
use graph::data::value::Object;
use http::status::StatusCode;
use hyper::service::Service;
use hyper::{Body, Method, Request};
use graph::data::{
graphql::effort::LoadManager,
query::{QueryResults, QueryTarget},
};
use graph::prelude::*;
use graph_mock::MockMetricsRegistry;
use crate::test_utils;
use super::GraphQLService;
use super::GraphQLServiceMetrics;
/// A simple stupid query runner for testing.
pub struct TestGraphQlRunner;
lazy_static! {
static ref USERS: DeploymentHash = DeploymentHash::new("users").unwrap();
}
#[async_trait]
impl GraphQlRunner for TestGraphQlRunner {
async fn run_query_with_complexity(
self: Arc<Self>,
_query: Query,
_target: QueryTarget,
_complexity: Option<u64>,
_max_depth: Option<u8>,
_max_first: Option<u32>,
_max_skip: Option<u32>,
) -> QueryResults {
unimplemented!();
}
async fn run_query(self: Arc<Self>, _query: Query, _target: QueryTarget) -> QueryResults {
QueryResults::from(Object::from_iter(
vec![(
String::from("name"),
r::Value::String(String::from("Jordi")),
)]
.into_iter(),
))
}
async fn run_subscription(
self: Arc<Self>,
_subscription: Subscription,
_target: QueryTarget,
) -> Result<SubscriptionResult, SubscriptionError> {
unreachable!();
}
fn load_manager(&self) -> Arc<LoadManager> {
unimplemented!()
}
}
#[test]
fn posting_invalid_query_yields_error_response() {
let logger = Logger::root(slog::Discard, o!());
let metrics_registry = Arc::new(MockMetricsRegistry::new());
let metrics = Arc::new(GraphQLServiceMetrics::new(metrics_registry));
let subgraph_id = USERS.clone();
let graphql_runner = Arc::new(TestGraphQlRunner);
let node_id = NodeId::new("test").unwrap();
let mut service = GraphQLService::new(logger, metrics, graphql_runner, 8001, node_id);
let request = Request::builder()
.method(Method::POST)
.uri(format!(
"http://localhost:8000/subgraphs/id/{}",
subgraph_id
))
.body(Body::from("{}"))
.unwrap();
let response =
futures03::executor::block_on(service.call(request)).expect("Should return a response");
let errors = test_utils::assert_error_response(response, StatusCode::BAD_REQUEST, false);
let message = errors[0].as_str().expect("Error message is not a string");
assert_eq!(
message,
"GraphQL server error (client error): The \"query\" field is missing in request data"
);
}
#[tokio::test(flavor = "multi_thread")]
async fn posting_valid_queries_yields_result_response() {
let logger = Logger::root(slog::Discard, o!());
let metrics_registry = Arc::new(MockMetricsRegistry::new());
let metrics = Arc::new(GraphQLServiceMetrics::new(metrics_registry));
let subgraph_id = USERS.clone();
let graphql_runner = Arc::new(TestGraphQlRunner);
let node_id = NodeId::new("test").unwrap();
let mut service = GraphQLService::new(logger, metrics, graphql_runner, 8001, node_id);
let request = Request::builder()
.method(Method::POST)
.uri(format!(
"http://localhost:8000/subgraphs/id/{}",
subgraph_id
))
.body(Body::from("{\"query\": \"{ name }\"}"))
.unwrap();
// The response must be a 200
let response = tokio::spawn(service.call(request))
.await
.unwrap()
.expect("Should return a response");
let data = test_utils::assert_successful_response(response);
// The body should match the simulated query result
let name = data
.get("name")
.expect("Query result data has no \"name\" field")
.as_str()
.expect("Query result field \"name\" is not a string");
assert_eq!(name, "Jordi".to_string());
}
}