suku 0.1.0

GraphQL over HTTP for Rust
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
//! Error formatter tests: masking, context, batches, prepared queries and subscriptions.

mod support;

use std::{
    sync::{Arc, Mutex},
    time::Duration,
};

use ::http::{Method, StatusCode, header};
use futures::StreamExt as _;
use gql::{Field, GraphQLError, Location, ObjectType, PathSegment, Schema, TypeRef, Value};
use suku::{
    Context, ErrorContext, ErrorFormatter, ErrorKind, GraphQLRequest, HttpConfig, HttpError,
    HttpResult, PreparedOperation, PreparedQueries, Server, http::handle, mask_internal,
};

fn path_of(field: &str) -> Vec<PathSegment> {
    vec![PathSegment::field(field)]
}

async fn execute(server: &Server, request: &GraphQLRequest) -> gql::Response {
    server.execute(request, Context::empty()).await
}

fn buffered(result: HttpResult) -> suku::HttpResponse {
    match result {
        HttpResult::Response(response) => response,
        HttpResult::Stream(_) => panic!("expected a buffered response"),
    }
}

fn json_body(response: &suku::HttpResponse) -> serde_json::Value {
    serde_json::from_slice(&response.body).expect("JSON body")
}

#[tokio::test]
async fn no_formatter_leaves_errors_unchanged() {
    let server = support::server();
    assert!(!server.has_error_formatter());

    let execution = execute(&server, &GraphQLRequest::new("{ boom }")).await;
    assert_eq!(execution.errors[0].message(), "boom");

    let request = execute(&server, &GraphQLRequest::new("{ nope }")).await;
    assert!(
        request.errors[0].message().contains("nope"),
        "{:?}",
        request.errors[0]
    );
}

#[tokio::test]
async fn formatter_sees_execution_context_and_preserves_path() {
    let server = support::server().format_error(|error, context| {
        assert_eq!(context.kind(), ErrorKind::Execution);
        assert!(context.is_execution());
        assert_eq!(context.operation_name(), Some("Boom"));
        assert_eq!(context.request_id(), None);
        assert_eq!(context.path(), error.path());
        error.clone().with_message("redacted")
    });

    let request = GraphQLRequest::new("query Boom { boom }").operation_name("Boom");
    let response = execute(&server, &request).await;

    assert_eq!(response.errors[0].message(), "redacted");
    assert_eq!(response.errors[0].path(), Some(path_of("boom").as_slice()));
}

#[tokio::test]
async fn mask_internal_masks_uncoded_execution_errors() {
    let server = support::server().format_error(mask_internal);
    let response = execute(&server, &GraphQLRequest::new("{ boom }")).await;

    assert_eq!(response.errors[0].message(), "Internal server error");
    assert_eq!(response.errors[0].path(), Some(path_of("boom").as_slice()));
}

#[tokio::test]
async fn mask_internal_preserves_locations_and_path() {
    let server = support::server().format_error(mask_internal);
    let response = execute(&server, &GraphQLRequest::new("{ located }")).await;

    assert_eq!(response.errors[0].message(), "Internal server error");
    assert_eq!(response.errors[0].locations(), &[Location::new(3, 5)]);
    assert_eq!(
        response.errors[0].path(),
        Some(path_of("located").as_slice())
    );
}

#[tokio::test]
async fn mask_internal_keeps_coded_execution_errors() {
    let server = support::server().format_error(mask_internal);
    let response = execute(&server, &GraphQLRequest::new("{ coded }")).await;

    assert_eq!(response.errors[0].message(), "secret database detail");
    assert_eq!(
        response.errors[0]
            .extensions()
            .and_then(|extensions| extensions.get("code")),
        Some(&Value::from("INTERNAL"))
    );
    assert_eq!(response.errors[0].path(), Some(path_of("coded").as_slice()));
}

#[tokio::test]
async fn mask_internal_keeps_request_errors() {
    let kinds = Arc::new(Mutex::new(Vec::new()));
    let seen = Arc::clone(&kinds);
    let server = support::server().format_error(move |error, context| {
        seen.lock().expect("lock").push(context.kind());
        mask_internal(error, context)
    });

    let response = execute(&server, &GraphQLRequest::new("{ nope }")).await;

    assert!(response.errors[0].message().contains("nope"));
    assert_eq!(*kinds.lock().expect("lock"), vec![ErrorKind::Request]);
}

#[tokio::test]
async fn custom_formatter_can_redact_request_errors() {
    let server = support::server().format_error(|error, context| {
        assert!(context.is_request());
        assert_eq!(context.path(), None);
        error.clone().with_message("request redacted")
    });

    let response = execute(&server, &GraphQLRequest::new("{ nope }")).await;
    assert_eq!(response.errors[0].message(), "request redacted");
}

#[tokio::test]
async fn batches_format_every_response_in_request_order() {
    let server = support::server().format_error(|error, _| {
        error
            .clone()
            .with_message(format!("masked:{}", error.message()))
    });

    let requests = [
        GraphQLRequest::new("{ hello }"),
        GraphQLRequest::new("{ boom }"),
        GraphQLRequest::new("{ nope }"),
        GraphQLRequest::new("{ coded }"),
    ];
    let responses = server.execute_batch(&requests, Context::empty()).await;

    assert_eq!(responses.len(), 4);
    assert_eq!(
        responses[0]
            .data
            .as_ref()
            .and_then(Value::as_object)
            .and_then(|data| data.get("hello"))
            .and_then(Value::as_str),
        Some("world")
    );
    assert_eq!(responses[1].errors[0].message(), "masked:boom");
    assert!(responses[2].errors[0].message().starts_with("masked:"));
    assert_eq!(
        responses[3].errors[0].message(),
        "masked:secret database detail"
    );
    // The formatter replaced the code-bearing error's message but the code itself survives.
    assert_eq!(
        responses[3].errors[0]
            .extensions()
            .and_then(|extensions| extensions.get("code")),
        Some(&Value::from("INTERNAL"))
    );
}

#[tokio::test]
async fn prepared_execution_exposes_the_prepared_id() {
    let schema = support::schema();
    let prepared =
        PreparedQueries::from_operations(&schema, [PreparedOperation::new("boom", "{ boom }")])
            .expect("prepared queries");
    let server = Server::new(schema)
        .prepared_queries(prepared)
        .format_error(|error, context| {
            assert_eq!(context.request_id(), Some("boom"));
            assert_eq!(context.operation_name(), None);
            assert_eq!(context.kind(), ErrorKind::Execution);
            error.clone().with_message("prepared redacted")
        });

    let response = execute(&server, &GraphQLRequest::persisted("boom")).await;
    assert_eq!(response.errors[0].message(), "prepared redacted");
    assert_eq!(response.errors[0].path(), Some(path_of("boom").as_slice()));
}

#[tokio::test]
async fn prepared_request_errors_expose_the_prepared_id() {
    let schema = support::schema();
    let prepared =
        PreparedQueries::from_operations(&schema, [PreparedOperation::new("hello", "{ hello }")])
            .expect("prepared queries");
    let server = Server::new(schema)
        .prepared_queries(prepared)
        .format_error(|error, context| {
            assert_eq!(context.request_id(), Some("missing"));
            assert!(context.is_request());
            error.clone().with_message("unknown prepared query")
        });

    let response = execute(&server, &GraphQLRequest::persisted("missing")).await;
    assert_eq!(response.errors[0].message(), "unknown prepared query");
}

#[tokio::test]
async fn subscription_setup_errors_are_formatted_as_request_errors() {
    let server = support::server().format_error(|error, context| {
        assert_eq!(context.kind(), ErrorKind::Request);
        error.clone().with_message("setup redacted")
    });

    let error = server
        .subscribe(
            &GraphQLRequest::new("subscription { nope }"),
            Context::empty(),
        )
        .await
        .expect_err("validation error");
    assert_eq!(error.errors[0].message(), "setup redacted");
}

#[tokio::test]
async fn subscription_events_are_formatted_without_reordering() {
    let server = support::server().format_error(|error, context| {
        assert_eq!(context.kind(), ErrorKind::Execution);
        assert_eq!(context.operation_name(), Some("Failing"));
        assert_eq!(context.path(), error.path());
        error.clone().with_message("event redacted")
    });

    let request = GraphQLRequest::new("subscription Failing { failing }").operation_name("Failing");
    let mut subscription = server
        .subscribe(&request, Context::empty())
        .await
        .expect("subscription");

    let first = subscription.stream.next().await.expect("first event");
    assert!(!first.has_errors(), "{:?}", first.errors);
    assert_eq!(
        first
            .data
            .as_ref()
            .and_then(Value::as_object)
            .and_then(|data| data.get("failing")),
        Some(&Value::from(1))
    );

    let second = subscription.stream.next().await.expect("second event");
    assert_eq!(second.errors[0].message(), "event redacted");
    assert_eq!(second.errors[0].path(), Some(path_of("failing").as_slice()));
}

#[tokio::test]
async fn trait_object_formatter_is_supported() {
    struct Redact;

    impl ErrorFormatter for Redact {
        fn format(&self, error: &GraphQLError, _: &ErrorContext<'_>) -> GraphQLError {
            error.clone().with_message("trait object")
        }
    }

    let server = support::server().format_error_with(Arc::new(Redact));
    assert!(server.has_error_formatter());
    let response = execute(&server, &GraphQLRequest::new("{ boom }")).await;
    assert_eq!(response.errors[0].message(), "trait object");
}

#[tokio::test]
async fn http_request_errors_are_formatted() {
    let server = support::server().format_error(|error, context| {
        assert!(context.is_request());
        assert_eq!(context.request_id(), None);
        error.clone().with_message("transport redacted")
    });

    let parts = support::parts(
        Method::POST,
        "/graphql",
        &[
            (header::CONTENT_TYPE, "application/json"),
            (header::ACCEPT, "application/json"),
        ],
    );
    let response = buffered(
        handle(
            &server,
            &HttpConfig::default(),
            &parts,
            b"{not json",
            support::empty_context,
        )
        .await,
    );

    assert_eq!(response.status, StatusCode::BAD_REQUEST);
    assert_eq!(
        json_body(&response)["errors"][0]["message"],
        "transport redacted"
    );
}

#[tokio::test]
async fn extractor_denials_are_formatted() {
    let server = support::server()
        .format_error(|error, _| error.clone().with_message("denied and redacted"));

    let parts = support::parts(
        Method::POST,
        "/graphql",
        &[
            (header::CONTENT_TYPE, "application/json"),
            (header::ACCEPT, "application/json"),
        ],
    );
    let response = buffered(
        handle(&server, &HttpConfig::default(), &parts, b"{}", |_| async {
            Err(HttpError::unauthorized(suku::error_response(
                "denied detail",
            )))
        })
        .await,
    );

    assert_eq!(response.status, StatusCode::UNAUTHORIZED);
    assert_eq!(
        json_body(&response)["errors"][0]["message"],
        "denied and redacted"
    );
}

#[tokio::test]
async fn transport_rejections_are_formatted() {
    let server = support::server().format_error(|error, context| {
        assert!(context.is_request());
        error
            .clone()
            .with_message(format!("{} (formatted)", error.message()))
    });

    let put = support::parts(
        Method::PUT,
        "/graphql",
        &[(header::ACCEPT, "application/json")],
    );
    let response = buffered(
        handle(
            &server,
            &HttpConfig::default(),
            &put,
            b"{}",
            support::empty_context,
        )
        .await,
    );
    assert_eq!(response.status, StatusCode::METHOD_NOT_ALLOWED);
    assert_eq!(
        json_body(&response)["errors"][0]["message"],
        "Only GET and POST are supported. (formatted)"
    );

    let post = support::parts(
        Method::POST,
        "/graphql",
        &[
            (header::CONTENT_TYPE, "application/json"),
            (header::ACCEPT, "text/html"),
        ],
    );
    let response = buffered(
        handle(
            &server,
            &HttpConfig::default(),
            &post,
            b"{\"query\":\"{ hello }\"}",
            support::empty_context,
        )
        .await,
    );
    assert_eq!(response.status, StatusCode::NOT_ACCEPTABLE);
    assert_eq!(
        json_body(&response)["errors"][0]["message"],
        "No acceptable response type; use `application/json` or `application/graphql-response+json`. (formatted)"
    );
}

#[cfg(feature = "axum")]
#[tokio::test]
async fn timeouts_are_formatted_as_execution_errors() {
    let slow = Field::new("slow", TypeRef::string().non_null()).resolve(|_| {
        Box::pin(async {
            tokio::time::sleep(Duration::from_millis(200)).await;
            Ok(Value::from("late"))
        })
    });
    let query = ObjectType::new("Query").field(slow);
    let mut builder = Schema::builder();
    builder.add_type(query.into());
    let schema = builder.query("Query").build().expect("valid schema");

    let server = Server::new(schema)
        .timeout(Duration::from_millis(10))
        .format_error(|error, context| {
            assert_eq!(context.kind(), ErrorKind::Execution);
            error.clone().with_message("timed out and redacted")
        });

    let response = execute(&server, &GraphQLRequest::new("{ slow }")).await;
    assert_eq!(response.errors[0].message(), "timed out and redacted");
}

#[tokio::test]
#[should_panic(expected = "formatter panic")]
async fn formatter_panics_are_not_caught() {
    let server = support::server().format_error(|_, _| panic!("formatter panic"));
    let _ = execute(&server, &GraphQLRequest::new("{ boom }")).await;
}