apollo-router 3.0.0-alpha.2

A configurable, high-performance routing runtime for Apollo Federation 🚀
Documentation
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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
//! Tower layers wrapping old non-tower layer-like things into real tower layers.
//!
//! Long-term, we should move the actual implementation code into a service structure, but that is
//! more work especially to translate the tests.

use std::sync::Arc;

use futures::future::BoxFuture;
use http::StatusCode;
use tower::BoxError;
use tower::Service;

use crate::apollo_studio_interop::UsageReporting;
use crate::compute_job::MaybeBackPressureError;
use crate::context::OPERATION_KIND;
use crate::context::OPERATION_NAME;
use crate::error::Error as RouterError;
use crate::graphql::ErrorExtension;
use crate::graphql::IntoGraphQLErrors;
use crate::query_planner::OperationKind;
use crate::services::query_parsing;
use crate::services::query_parsing::ParsedDocument;
use crate::services::supergraph;
use crate::spec::SpecError;

/// Parses the GraphQL in the supergraph request.
///
/// # Context
/// This stores values in the request context:
/// - [`ParsedDocument`]
/// - "operation_name" and "operation_kind"
/// - [`Arc`]`<`[`UsageReporting`]`>` if there was an error; normally, this would be populated
///   by the caching query planner, but we do not reach that code if we fail early here.
pub(crate) struct ParseQueryLayer {
    query_parsing_service: query_parsing::BoxCloneService,
    redact_query_validation_errors: bool,
}

impl ParseQueryLayer {
    pub(crate) fn new(
        query_parsing_service: query_parsing::BoxCloneService,
        redact_query_validation_errors: bool,
    ) -> Self {
        Self {
            query_parsing_service,
            redact_query_validation_errors,
        }
    }
}

impl<S> tower::Layer<S> for ParseQueryLayer {
    type Service = ParseQueryService<S>;

    fn layer(&self, inner: S) -> Self::Service {
        ParseQueryService {
            inner,
            query_parsing_service: self.query_parsing_service.clone(),
            redact_query_validation_errors: self.redact_query_validation_errors,
        }
    }
}

#[derive(Clone)]
pub(crate) struct ParseQueryService<S> {
    inner: S,
    query_parsing_service: query_parsing::BoxCloneService,
    redact_query_validation_errors: bool,
}

impl<S> Service<supergraph::Request> for ParseQueryService<S>
where
    S: Service<supergraph::Request, Response = supergraph::Response, Error = BoxError>
        + Clone
        + Send
        + 'static,
    S::Future: Send + 'static,
{
    type Response = supergraph::Response;
    type Error = BoxError;
    type Future = BoxFuture<'static, Result<Self::Response, Self::Error>>;

    fn poll_ready(
        &mut self,
        cx: &mut std::task::Context<'_>,
    ) -> std::task::Poll<Result<(), Self::Error>> {
        std::task::ready!(self.query_parsing_service.poll_ready(cx)).map_err(|err| match err {
            MaybeBackPressureError::PermanentError(err) => Box::new(err) as BoxError,
            // Technically a temporary error is supposed to be a backpressure error,
            // but we should not get an error here if it truly is "just" backpressure.
            MaybeBackPressureError::TemporaryError(err) => Box::new(err) as BoxError,
        })?;
        self.inner.poll_ready(cx)
    }

    fn call(&mut self, req: supergraph::Request) -> Self::Future {
        let query_parsing_service = self.query_parsing_service.clone();
        let mut query_parsing_service =
            std::mem::replace(&mut self.query_parsing_service, query_parsing_service);

        let inner = self.inner.clone();
        let mut inner = std::mem::replace(&mut self.inner, inner);

        let redact_query_validation_errors = self.redact_query_validation_errors;

        Box::pin(async move {
            let query = req.supergraph_request.body().query.as_ref();
            if query.is_none() || query.unwrap().trim().is_empty() {
                let errors = vec![
                    RouterError::builder()
                        .message("Must provide query string.".to_string())
                        .extension_code("MISSING_QUERY_STRING")
                        .build(),
                ];
                return Ok(supergraph::Response::builder()
                    .errors(errors)
                    .status_code(StatusCode::BAD_REQUEST)
                    .context(req.context)
                    .build()
                    .expect("response is valid"));
            }

            let operation_name = req.supergraph_request.body().operation_name.clone();
            let query = req
                .supergraph_request
                .body()
                .query
                .clone()
                .expect("query presence was already checked");

            match query_parsing_service
                .call(query_parsing::Request::new(query, operation_name.clone()))
                .await
            {
                Ok(doc) => {
                    req.context
                        .insert(OPERATION_NAME, doc.operation.name.clone())
                        .expect("cannot insert operation name into context; this is a bug");
                    let operation_kind = OperationKind::from(doc.operation.operation_type);
                    req.context
                        .insert(OPERATION_KIND, operation_kind)
                        .expect("cannot insert operation kind in the context; this is a bug");

                    req.context
                        .extensions()
                        .with_lock(|lock| lock.insert::<ParsedDocument>(doc));

                    inner.call(req).await
                }
                Err(MaybeBackPressureError::PermanentError(errors)) => {
                    // TODO(@goto-bus-stop): validation error redaction should prob be a layer very
                    // early on in the stack, that works on the JSON representation of errors?
                    let errors = if redact_query_validation_errors
                        && matches!(errors, SpecError::ValidationError(_))
                    {
                        SpecError::Redacted
                    } else {
                        errors
                    };

                    req.context.extensions().with_lock(|lock| {
                        lock.insert(Arc::new(UsageReporting::Error(
                            errors.get_error_key().to_string(),
                        )))
                    });
                    let errors = match errors.into_graphql_errors() {
                        Ok(v) => v,
                        Err(errors) => vec![
                            crate::graphql::Error::builder()
                                .message(errors.to_string())
                                .extension_code(errors.extension_code())
                                .build(),
                        ],
                    };
                    Ok(supergraph::Response::builder()
                        .errors(errors)
                        .status_code(StatusCode::BAD_REQUEST)
                        .context(req.context)
                        .build()
                        .expect("response is valid"))
                }
                Err(MaybeBackPressureError::TemporaryError(error)) => {
                    req.context.extensions().with_lock(|lock| {
                        let error_key =
                            SpecError::ValidationError(crate::error::ValidationErrors {
                                errors: vec![],
                            })
                            .get_error_key();
                        lock.insert(Arc::new(UsageReporting::Error(error_key.to_string())))
                    });
                    Ok(supergraph::Response::builder()
                        .error(error.to_graphql_error())
                        .status_code(StatusCode::SERVICE_UNAVAILABLE)
                        .context(req.context)
                        .build()
                        .expect("response is valid"))
                }
            }
        })
    }
}

#[cfg(test)]
mod tests {
    use std::sync::Arc;

    use http::StatusCode;
    use tower::Service as _;
    use tower::ServiceBuilder;
    use tower::ServiceExt as _;

    use super::ParseQueryLayer;
    use crate::Configuration;
    use crate::compute_job::MaybeBackPressureError;
    use crate::context::OPERATION_KIND;
    use crate::context::OPERATION_NAME;
    use crate::services::OperationKind;
    use crate::services::query_parsing;
    use crate::services::supergraph;

    const SCHEMA: &str = include_str!("../../../testing_schema.graphql");

    fn downcast_mock_err(err: tower::BoxError) -> query_parsing::ServiceError {
        *err.downcast()
            .expect("mock should only return ServiceErrors")
    }

    async fn mock_parser(
        mut handle: tower_test::mock::Handle<query_parsing::Request, query_parsing::ParsedDocument>,
        schema: Arc<crate::spec::Schema>,
        config: Arc<Configuration>,
    ) {
        while let Some((req, responder)) = handle.next_request().await {
            match crate::spec::Query::parse_document(
                &req.query,
                req.operation_name.as_deref(),
                &schema,
                &config,
            ) {
                Ok(document) => responder.send_response(document),
                Err(err) => responder.send_error(MaybeBackPressureError::PermanentError(err)),
            }
        }
    }

    #[tokio::test]
    async fn it_accepts_valid_query() {
        let (query_parsing_service, query_parsing_handle) =
            tower_test::mock::pair::<query_parsing::Request, query_parsing::ParsedDocument>();
        let query_parsing_service = ServiceBuilder::new()
            .map_err(downcast_mock_err)
            .service(query_parsing_service)
            .boxed_clone();

        let config = Arc::new(Configuration::default());
        let schema = Arc::new(crate::spec::Schema::parse(SCHEMA, &config).unwrap());
        let query_parsing_driver = tokio::spawn(mock_parser(query_parsing_handle, schema, config));

        let (mock, mut handle) =
            tower_test::mock::pair::<supergraph::Request, supergraph::Response>();
        let inner_driver = tokio::spawn(async move {
            let (req, responder) = handle.next_request().await.unwrap();

            // The document, operation name and operation kind should already be in context by
            // the time the inner service is called.
            assert!(
                req.context
                    .extensions()
                    .with_lock(|lock| lock.contains_key::<query_parsing::ParsedDocument>())
            );
            assert!(
                req.context
                    .get::<_, Option<String>>(OPERATION_NAME)
                    .unwrap()
                    .is_some()
            );
            assert!(
                req.context
                    .get::<_, OperationKind>(OPERATION_KIND)
                    .unwrap()
                    .is_some()
            );

            responder.send_response(supergraph::Response::fake_builder().build().unwrap());
        });

        let mut service = ServiceBuilder::new()
            .layer(ParseQueryLayer::new(query_parsing_service, false))
            .service(mock);

        let response = service
            .ready()
            .await
            .unwrap()
            .call(
                supergraph::Request::fake_builder()
                    .query("query { me { id } }")
                    .build()
                    .unwrap(),
            )
            .await
            .unwrap();

        assert_eq!(http::StatusCode::OK, response.response.status());

        drop(service);
        crate::plugin::test::await_mock_driver(query_parsing_driver).await;
        crate::plugin::test::await_mock_driver(inner_driver).await;
    }

    #[tokio::test]
    async fn it_rejects_backpressure() {
        let (query_parsing_service, mut query_parsing_handle) =
            tower_test::mock::pair::<query_parsing::Request, query_parsing::ParsedDocument>();
        let query_parsing_service = ServiceBuilder::new()
            .map_err(downcast_mock_err)
            .service(query_parsing_service)
            .boxed_clone();
        let (mock, handle) = tower_test::mock::pair::<supergraph::Request, supergraph::Response>();

        let query_parsing_driver = tokio::task::spawn(async move {
            let (_req, responder) = query_parsing_handle.next_request().await.unwrap();
            responder.send_error(MaybeBackPressureError::TemporaryError(
                crate::compute_job::ComputeBackPressureError,
            ) as query_parsing::ServiceError);
        });

        let mut service = ServiceBuilder::new()
            .layer(ParseQueryLayer::new(query_parsing_service, false))
            .service(mock);

        let mut response = service
            .ready()
            .await
            .unwrap()
            .call(
                supergraph::Request::fake_builder()
                    .query("query { me { id } }")
                    .build()
                    .unwrap(),
            )
            .await
            .unwrap();

        assert_eq!(StatusCode::SERVICE_UNAVAILABLE, response.response.status());
        let graphql_response = response.next_response().await.unwrap();
        assert!(graphql_response.contains_error_code("REQUEST_CONCURRENCY_LIMITED"));

        drop(service);
        crate::plugin::test::await_mock_driver(query_parsing_driver).await;
        // The inner service is not actually reached
        crate::plugin::test::assert_no_mock_calls(handle).await;
    }

    #[tokio::test]
    async fn it_rejects_missing_query() {
        let (query_parsing_service, query_parsing_handle) =
            tower_test::mock::pair::<query_parsing::Request, query_parsing::ParsedDocument>();
        let query_parsing_service = ServiceBuilder::new()
            .map_err(downcast_mock_err)
            .service(query_parsing_service)
            .boxed_clone();
        let (mock, handle) = tower_test::mock::pair::<supergraph::Request, supergraph::Response>();

        let mut service = ServiceBuilder::new()
            .layer(ParseQueryLayer::new(query_parsing_service, false))
            .service(mock);

        let mut response = service
            .ready()
            .await
            .unwrap()
            .call(supergraph::Request::fake_builder().build().unwrap())
            .await
            .unwrap();

        assert_eq!(StatusCode::BAD_REQUEST, response.response.status());
        let graphql_response = response.next_response().await.unwrap();
        assert!(graphql_response.contains_error_code("MISSING_QUERY_STRING"));

        // Neither service is actually reached.
        crate::plugin::test::assert_no_mock_calls(query_parsing_handle).await;
        crate::plugin::test::assert_no_mock_calls(handle).await;
    }

    #[tokio::test]
    async fn it_rejects_empty_query() {
        let (query_parsing_service, query_parsing_handle) =
            tower_test::mock::pair::<query_parsing::Request, query_parsing::ParsedDocument>();
        let query_parsing_service = ServiceBuilder::new()
            .map_err(downcast_mock_err)
            .service(query_parsing_service)
            .boxed_clone();
        let (mock, handle) = tower_test::mock::pair::<supergraph::Request, supergraph::Response>();

        let mut service = ServiceBuilder::new()
            .layer(ParseQueryLayer::new(query_parsing_service, false))
            .service(mock);

        let mut response = service
            .ready()
            .await
            .unwrap()
            .call(
                supergraph::Request::fake_builder()
                    .query("")
                    .build()
                    .unwrap(),
            )
            .await
            .unwrap();

        assert_eq!(StatusCode::BAD_REQUEST, response.response.status());
        let graphql_response = response.next_response().await.unwrap();
        assert!(graphql_response.contains_error_code("MISSING_QUERY_STRING"));

        // Neither service is actually reached.
        crate::plugin::test::assert_no_mock_calls(query_parsing_handle).await;
        crate::plugin::test::assert_no_mock_calls(handle).await;
    }

    #[tokio::test]
    async fn it_rejects_invalid_query() {
        let (query_parsing_service, query_parsing_handle) =
            tower_test::mock::pair::<query_parsing::Request, query_parsing::ParsedDocument>();
        let query_parsing_service = ServiceBuilder::new()
            .map_err(downcast_mock_err)
            .service(query_parsing_service)
            .boxed_clone();
        let config = Arc::new(Configuration::default());
        let schema = Arc::new(crate::spec::Schema::parse(SCHEMA, &config).unwrap());
        let query_parsing_driver = tokio::spawn(mock_parser(query_parsing_handle, schema, config));

        let (mock, handle) = tower_test::mock::pair::<supergraph::Request, supergraph::Response>();

        let mut service = ServiceBuilder::new()
            .layer(ParseQueryLayer::new(query_parsing_service, false))
            .service(mock);

        let mut response = service
            .ready()
            .await
            .unwrap()
            .call(
                supergraph::Request::fake_builder()
                    .query("query Missing { doesNotExist }")
                    .build()
                    .unwrap(),
            )
            .await
            .unwrap();

        assert_eq!(StatusCode::BAD_REQUEST, response.response.status());
        let graphql_response = response.next_response().await.unwrap();
        assert!(graphql_response.contains_error_code("GRAPHQL_VALIDATION_FAILED"));

        drop(service);
        crate::plugin::test::await_mock_driver(query_parsing_driver).await;
        // The inner service is not reached.
        crate::plugin::test::assert_no_mock_calls(handle).await;
    }

    #[tokio::test]
    async fn it_redacts_validation_error() {
        let (query_parsing_service, query_parsing_handle) =
            tower_test::mock::pair::<query_parsing::Request, query_parsing::ParsedDocument>();
        let query_parsing_service = ServiceBuilder::new()
            .map_err(downcast_mock_err)
            .service(query_parsing_service)
            .boxed_clone();
        let config = Arc::new(Configuration::default());
        let schema = Arc::new(crate::spec::Schema::parse(SCHEMA, &config).unwrap());
        let query_parsing_driver = tokio::spawn(mock_parser(query_parsing_handle, schema, config));

        let (mock, handle) = tower_test::mock::pair::<supergraph::Request, supergraph::Response>();

        let mut service = ServiceBuilder::new()
            .layer(ParseQueryLayer::new(query_parsing_service, true))
            .service(mock);

        let mut response = service
            .ready()
            .await
            .unwrap()
            .call(
                supergraph::Request::fake_builder()
                    .query("query Missing { doesNotExist }")
                    .build()
                    .unwrap(),
            )
            .await
            .unwrap();

        assert_eq!(StatusCode::BAD_REQUEST, response.response.status());
        let graphql_response = response.next_response().await.unwrap();
        assert!(graphql_response.contains_error_code("UNKNOWN_ERROR"));
        assert!(
            !serde_json::to_string(&graphql_response)
                .unwrap()
                .contains("doesNotExist")
        );

        drop(service);
        crate::plugin::test::await_mock_driver(query_parsing_driver).await;
        // The inner service is not reached.
        crate::plugin::test::assert_no_mock_calls(handle).await;
    }
}