vantage-api-client 2.2.2

Vantage extension for REST and GraphQL HTTP API backends
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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
//! `GraphqlSelect`: the query document and variables it renders, and the
//! synchronous preview of the same document. Filters render inline as
//! GraphQL value syntax; paging goes through typed `$limit` / `$offset`
//! variables.

use serde_json::json;
use vantage_api_client::{FilterDialect, GraphqlCondition, GraphqlSelect};
use vantage_expressions::{DeferredFn, ExpressiveEnum, Order};

fn launches() -> GraphqlSelect {
    GraphqlSelect::new()
        .with_root_field("launches")
        .with_field("id")
}

#[tokio::test]
async fn a_minimal_select_is_one_root_field_with_its_fields() {
    let rendered = GraphqlSelect::new()
        .with_root_field("launches")
        .with_field("id")
        .with_field("mission_name")
        .render()
        .await
        .unwrap();
    assert_eq!(rendered.query, "query { launches { id mission_name } }");
    assert!(rendered.variables.is_empty());
}

#[tokio::test]
async fn the_hasura_dialect_filters_with_where() {
    let rendered = launches()
        .with_dialect(FilterDialect::Hasura)
        .with_condition(GraphqlCondition::eq("year", 2018))
        .render()
        .await
        .unwrap();
    assert_eq!(
        rendered.query,
        "query { launches(where: {year: {_eq: 2018}}) { id } }"
    );
}

#[tokio::test]
async fn a_filter_argument_name_replaces_the_dialects() {
    let rendered = launches()
        .with_filter_arg_name("filter")
        .with_condition(GraphqlCondition::eq("year", 2018))
        .render()
        .await
        .unwrap();
    assert_eq!(
        rendered.query,
        "query { launches(filter: {year: 2018}) { id } }"
    );
}

#[tokio::test]
async fn several_conditions_are_anded_into_one_filter() {
    let rendered = launches()
        .with_dialect(FilterDialect::Generic)
        .with_condition(GraphqlCondition::eq("year", 2018))
        .with_condition(GraphqlCondition::eq("site", "KSC"))
        .render()
        .await
        .unwrap();
    assert_eq!(
        rendered.query,
        "query { launches(find: {year: 2018, site: \"KSC\"}) { id } }"
    );
}

#[tokio::test]
async fn filter_values_render_as_graphql_values() {
    let rendered = launches()
        .with_dialect(FilterDialect::Hasura)
        .with_condition(GraphqlCondition::eq("flag", true))
        .with_condition(GraphqlCondition::eq("gone", json!(null)))
        .with_condition(GraphqlCondition::eq("tags", json!(["a", "b"])))
        .with_condition(GraphqlCondition::eq("pos", json!({ "x": 1.5 })))
        .render()
        .await
        .unwrap();
    assert_eq!(
        rendered.query,
        "query { launches(where: {flag: {_eq: true}, gone: {_eq: null}, tags: {_eq: [\"a\", \"b\"]}, pos: {_eq: {x: 1.5}}}) { id } }"
    );
}

#[tokio::test]
async fn string_values_are_escaped() {
    let query = launches()
        .with_condition(GraphqlCondition::eq(
            "quoted",
            "say \"hi\" \\ now\nnext\tcol\r",
        ))
        .with_condition(GraphqlCondition::eq("control", "a\u{0}b\u{1b}c\u{7f}"))
        .with_condition(GraphqlCondition::eq("unicode", "Zürich ✓"))
        .render()
        .await
        .unwrap()
        .query;
    assert!(
        query.contains(r#"quoted: "say \"hi\" \\ now\nnext\tcol\r""#),
        "{query}"
    );
    assert!(
        query.contains(r#"control: "a\u0000b\u001Bc\u007F""#),
        "{query}"
    );
    assert!(query.contains(r#"unicode: "Zürich ✓""#), "{query}");
}

#[tokio::test]
async fn paging_goes_through_variables() {
    let rendered = launches()
        .with_limit(Some(10), Some(20))
        .render()
        .await
        .unwrap();
    assert_eq!(
        rendered.query,
        "query ($limit: Int, $offset: Int) { launches(limit: $limit, offset: $offset) { id } }"
    );
    assert_eq!(rendered.variables.get("limit"), Some(&json!(10)));
    assert_eq!(rendered.variables.get("offset"), Some(&json!(20)));
}

#[tokio::test]
async fn a_limit_alone_declares_only_the_limit() {
    let rendered = launches().with_limit(Some(5), None).render().await.unwrap();
    assert_eq!(
        rendered.query,
        "query ($limit: Int) { launches(limit: $limit) { id } }"
    );
    assert_eq!(rendered.variables.len(), 1);
}

#[tokio::test]
async fn the_filter_comes_before_the_paging_arguments() {
    let rendered = launches()
        .with_condition(GraphqlCondition::eq("year", 2018))
        .with_limit(Some(10), Some(0))
        .render()
        .await
        .unwrap();
    assert_eq!(
        rendered.query,
        "query ($limit: Int, $offset: Int) { launches(find: {year: 2018}, limit: $limit, offset: $offset) { id } }"
    );
}

#[tokio::test]
async fn an_operation_name_follows_the_query_keyword() {
    let named = launches().with_operation_name("GetLaunches");
    assert_eq!(
        named.render().await.unwrap().query,
        "query GetLaunches { launches { id } }"
    );
    let paged = named.with_limit(Some(5), None);
    assert_eq!(
        paged.render().await.unwrap().query,
        "query GetLaunches($limit: Int) { launches(limit: $limit) { id } }"
    );
}

#[tokio::test]
async fn hasura_sorts_with_order_by_and_generic_leaves_the_sort_out() {
    let hasura = GraphqlSelect::new()
        .with_root_field("users")
        .with_field("id")
        .with_dialect(FilterDialect::Hasura)
        .with_order("created_at", Order::Desc)
        .with_order("name", Order::Asc);
    assert_eq!(
        hasura.render().await.unwrap().query,
        "query { users(order_by: [{created_at: desc, name: asc}]) { id } }"
    );

    let generic = GraphqlSelect::new()
        .with_root_field("users")
        .with_field("id")
        .with_order("created_at", Order::Desc);
    assert_eq!(
        generic.render().await.unwrap().query,
        "query { users { id } }"
    );
}

#[tokio::test]
async fn a_sub_selection_carries_its_own_arguments() {
    let rocket = GraphqlSelect::new().with_field("id").with_field("name");
    let rendered = launches()
        .with_field("mission_name")
        .with_sub_selection("rocket", rocket)
        .render()
        .await
        .unwrap();
    assert_eq!(
        rendered.query,
        "query { launches { id mission_name rocket { id name } } }"
    );

    let filtered = GraphqlSelect::new()
        .with_field("id")
        .with_condition(GraphqlCondition::eq("kind", "heavy"))
        .with_limit(Some(2), Some(1));
    let rendered = launches()
        .with_sub_selection("rockets", filtered)
        .render()
        .await
        .unwrap();
    assert_eq!(
        rendered.query,
        "query { launches { id rockets(find: {kind: \"heavy\"}, limit: 2, offset: 1) { id } } }"
    );
    assert!(rendered.variables.is_empty());
}

#[tokio::test]
async fn dotted_fields_group_into_nested_selections() {
    let rendered = GraphqlSelect::new()
        .with_root_field("searchRuns")
        .with_field("run.id")
        .with_field("run.state")
        .with_field("run.commit.hash")
        .with_field("stack.id")
        .with_field("isModule")
        .render()
        .await
        .unwrap();
    assert_eq!(
        rendered.query,
        "query { searchRuns { run { id state commit { hash } } stack { id } isModule } }"
    );
}

#[tokio::test]
async fn root_arguments_render_before_the_filter() {
    let rendered = GraphqlSelect::new()
        .with_root_field("searchRuns")
        .with_root_args(json!({ "input": {} }))
        .with_field("run.id")
        .with_condition(GraphqlCondition::eq("state", "OK"))
        .render()
        .await
        .unwrap();
    assert_eq!(
        rendered.query,
        "query { searchRuns(input: {}, find: {state: \"OK\"}) { run { id } } }"
    );
}

#[tokio::test]
async fn root_arguments_that_are_not_an_object_are_ignored() {
    let rendered = launches()
        .with_root_args(json!("input"))
        .render()
        .await
        .unwrap();
    assert_eq!(rendered.query, "query { launches { id } }");
}

#[tokio::test]
async fn the_response_path_wraps_the_selection_set() {
    let rendered = GraphqlSelect::new()
        .with_root_field("searchRuns")
        .with_root_args(json!({ "input": {} }))
        .with_response_path(vec!["edges".into(), "node".into()])
        .with_field("run.id")
        .with_field("stack.id")
        .render()
        .await
        .unwrap();
    assert_eq!(
        rendered.query,
        "query { searchRuns(input: {}) { edges { node { run { id } stack { id } } } } }"
    );
}

#[tokio::test]
async fn an_empty_selection_set_is_an_error() {
    let err = GraphqlSelect::new()
        .with_root_field("launches")
        .render()
        .await
        .unwrap_err();
    assert!(err.to_string().contains("selection set"), "{err}");
}

#[tokio::test]
async fn a_missing_root_field_is_an_error() {
    let err = GraphqlSelect::new()
        .with_field("id")
        .render()
        .await
        .unwrap_err();
    assert!(err.to_string().contains("root_field"), "{err}");
}

#[tokio::test]
async fn a_dialect_that_cannot_spell_the_filter_fails_the_render() {
    let err = launches()
        .with_condition(GraphqlCondition::Or(vec![
            GraphqlCondition::eq("a", 1),
            GraphqlCondition::eq("b", 2),
        ]))
        .render()
        .await
        .unwrap_err();
    assert!(err.to_string().contains("Generic"), "{err}");
}

#[tokio::test]
async fn every_name_spliced_into_the_document_is_checked() {
    let cases = [
        (
            launches().with_root_field("launches { secret }"),
            "root field",
        ),
        (launches().with_field("run.id }"), "field"),
        (launches().with_operation_name("Q { x }"), "operation name"),
        (
            launches().with_response_path(vec!["edges".into(), "no de".into()]),
            "response path segment",
        ),
        (
            launches().with_order("id: asc}, {x", Order::Asc),
            "order field",
        ),
        (
            launches()
                .with_filter_arg_name("find)")
                .with_condition(GraphqlCondition::eq("id", "1")),
            "filter argument",
        ),
        (
            launches().with_root_args(json!({ "in put": {} })),
            "root argument",
        ),
        (
            launches().with_root_args(json!({ "input": { "a}": 1 } })),
            "root argument key",
        ),
        (
            launches().with_sub_selection("rocket x", GraphqlSelect::new().with_field("id")),
            "field",
        ),
        (
            launches().with_condition(GraphqlCondition::eq(
                "name}) { id } q: launches(find: {x",
                "v",
            )),
            "filter key",
        ),
    ];
    for (select, kind) in cases {
        let err = select.render().await.unwrap_err().to_string();
        assert!(err.contains("GraphQL name is not valid"), "{kind}: {err}");
        assert!(err.contains(kind), "{kind}: {err}");
    }
}

#[tokio::test]
async fn object_keys_inside_filter_values_are_checked_at_every_depth() {
    let fine = json!({ "_eq": [{ "b": 1 }] });
    let rendered = launches()
        .with_condition(GraphqlCondition::eq("a", fine))
        .render()
        .await;
    assert!(rendered.is_ok());

    for bad in [
        json!({ "b c": 1 }),
        json!([{ "b c": 1 }]),
        json!({ "ok": { "x}": 1 } }),
    ] {
        let err = launches()
            .with_condition(GraphqlCondition::eq("a", bad))
            .render()
            .await
            .unwrap_err()
            .to_string();
        assert!(err.contains("filter key"), "{err}");
    }

    let text_is_not_a_name = launches()
        .with_condition(GraphqlCondition::eq("a", json!({ "k": "b c }" })))
        .render()
        .await;
    assert!(text_is_not_a_name.is_ok(), "values are not names");
}

#[tokio::test]
async fn names_follow_the_graphql_grammar() {
    for ok in ["id", "_id", "__typename", "a1", "mission_name", "X"] {
        let rendered = launches().with_field(ok).render().await;
        assert!(rendered.is_ok(), "{ok}");
    }
    for bad in [
        "1a", "a-b", "a b", "a}", "é", "a\n", "run.", ".run", "run..id", "run.1d",
    ] {
        let err = launches().with_field(bad).render().await.unwrap_err();
        assert!(
            err.to_string().contains("GraphQL name is not valid"),
            "{bad:?}: {err}"
        );
    }
}

#[test]
fn the_accessors_report_what_was_set() {
    let select = launches()
        .with_dialect(FilterDialect::Hasura)
        .with_condition(GraphqlCondition::eq("a", 1));
    assert_eq!(select.root_field(), Some("launches"));
    assert_eq!(select.fields(), ["id"]);
    assert_eq!(select.conditions().len(), 1);
    assert_eq!(select.dialect(), FilterDialect::Hasura);
    assert_eq!(GraphqlSelect::new().root_field(), None);
    assert_eq!(GraphqlSelect::new().dialect(), FilterDialect::Generic);
}

#[tokio::test]
async fn the_preview_is_the_rendered_document_for_a_literal_query() {
    let select = GraphqlSelect::new()
        .with_root_field("launches")
        .with_field("id")
        .with_field("rocket.name")
        .with_dialect(FilterDialect::Generic)
        .with_condition(GraphqlCondition::eq("mission_name", "FalconSat"));
    let rendered = select.render().await.unwrap();
    assert_eq!(select.preview(), rendered.query);
    assert!(select.preview().contains("\"FalconSat\""));
}

#[tokio::test]
async fn the_preview_matches_render_for_a_rich_query() {
    let select = GraphqlSelect::new()
        .with_root_field("searchRuns")
        .with_operation_name("Runs")
        .with_root_args(json!({ "input": {} }))
        .with_response_path(vec!["edges".into(), "node".into()])
        .with_dialect(FilterDialect::Hasura)
        .with_field("run.id")
        .with_order("state", Order::Asc)
        .with_condition(GraphqlCondition::eq("state", "OK"))
        .with_sub_selection(
            "stack",
            GraphqlSelect::new()
                .with_field("id")
                .with_condition(GraphqlCondition::eq("kind", "x")),
        );
    let rendered = select.render().await.unwrap();
    assert_eq!(select.preview(), rendered.query);
}

#[test]
fn the_preview_inlines_paging() {
    let preview = launches().with_limit(Some(5), Some(10)).preview();
    assert_eq!(preview, "query { launches(limit: 5, offset: 10) { id } }");

    let named = launches().with_operation_name("Q").preview();
    assert_eq!(named, "query Q { launches { id } }");

    let child = GraphqlSelect::new()
        .with_field("id")
        .with_condition(GraphqlCondition::eq("kind", "x"))
        .with_limit(Some(2), Some(1));
    let nested = launches().with_sub_selection("rockets", child).preview();
    assert_eq!(
        nested,
        "query { launches { id rockets(find: {kind: \"x\"}, limit: 2, offset: 1) { id } } }"
    );
}

#[test]
fn the_preview_marks_a_deferred_condition_instead_of_awaiting_it() {
    let deferred = GraphqlCondition::deferred_field(
        "stack_id",
        vantage_api_client::GraphqlOp::Eq,
        DeferredFn::new(|| {
            Box::pin(async {
                Ok(ExpressiveEnum::Scalar(
                    vantage_api_client::AnyGraphqlType::from("x"),
                ))
            })
        }),
    );
    let preview = launches().with_condition(deferred.clone()).preview();
    assert!(preview.contains("**deferred(stack_id"), "{preview}");

    let child = GraphqlSelect::new()
        .with_field("id")
        .with_condition(deferred);
    let nested = launches().with_sub_selection("stacks", child).preview();
    assert!(nested.contains("**deferred(stack_id"), "{nested}");
}

#[test]
fn the_preview_shows_what_the_dialect_cannot_spell() {
    let preview = launches()
        .with_condition(GraphqlCondition::Or(vec![GraphqlCondition::eq("a", 1)]))
        .preview();
    assert!(preview.contains("<unrenderable"), "{preview}");

    let child = GraphqlSelect::new()
        .with_field("id")
        .with_condition(GraphqlCondition::Not(Box::new(GraphqlCondition::eq(
            "a", 1,
        ))));
    let nested = launches().with_sub_selection("rockets", child).preview();
    assert!(nested.contains("<unrenderable"), "{nested}");
}

#[test]
fn the_preview_reports_what_is_missing() {
    let no_fields = GraphqlSelect::new().with_root_field("launches").preview();
    assert!(no_fields.contains("<empty selection set>"), "{no_fields}");

    let no_root = GraphqlSelect::new().with_field("id").preview();
    assert!(no_root.contains("<no root_field>"), "{no_root}");
}

#[test]
fn the_preview_orders_hasura_selects_only() {
    let hasura = launches()
        .with_dialect(FilterDialect::Hasura)
        .with_order("id", Order::Desc)
        .preview();
    assert_eq!(hasura, "query { launches(order_by: [{id: desc}]) { id } }");
    let generic = launches().with_order("id", Order::Desc).preview();
    assert_eq!(generic, "query { launches { id } }");
}