fraiseql-core 2.19.0

Core execution engine for FraiseQL v2 - Compiled GraphQL over SQL
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
//! Unit tests for the shared selection-resolution routine.

#![allow(clippy::unwrap_used, clippy::panic)] // Reason: test code, panics acceptable

use serde_json::json;

use super::*;
use crate::{
    graphql::{parse_query, types::FieldSelection},
    schema::DEFAULT_MAX_QUERY_DEPTH,
};

/// Response keys of the root field's children, in order.
fn child_names(selections: &[FieldSelection]) -> Vec<String> {
    selections
        .first()
        .map(|root| root.nested_fields.iter().map(|f| f.response_key().to_string()).collect())
        .unwrap_or_default()
}

fn resolved(query: &str, variables: &serde_json::Value) -> Vec<String> {
    let parsed = parse_query(query).expect("query must parse");
    let vars = variables_map(Some(variables));
    let out =
        resolve_and_filter(&parsed.selections, &parsed.fragments, &vars, DEFAULT_MAX_QUERY_DEPTH)
            .expect("selection set must resolve");
    child_names(&out)
}

#[test]
fn a_spread_contributes_its_fields_at_the_position_it_appears() {
    assert_eq!(
        resolved("fragment F on User { name } { users { email ...F id } }", &json!({})),
        vec!["email", "name", "id"]
    );
}

#[test]
fn a_skipped_spread_contributes_nothing() {
    assert_eq!(
        resolved(
            "fragment F on User { name email } { users { id ...F @skip(if: true) } }",
            &json!({})
        ),
        vec!["id"]
    );
}

#[test]
fn a_spread_directive_composes_with_a_field_directive() {
    // The spread permits; the field inside withholds. `@skip` wins.
    assert_eq!(
        resolved(
            "fragment F on User { name email @skip(if: true) } { users { id ...F @include(if: \
             true) } }",
            &json!({})
        ),
        vec!["id", "name"]
    );
}

#[test]
fn a_variable_condition_on_a_spread_is_evaluated() {
    let query = "fragment F on User { name } query($lite: Boolean!) { users { id ...F @skip(if: \
                 $lite) } }";
    assert_eq!(resolved(query, &json!({"lite": true})), vec!["id"]);
    assert_eq!(resolved(query, &json!({"lite": false})), vec!["id", "name"]);
}

#[test]
fn an_undefined_variable_in_a_condition_is_an_error() {
    let parsed = parse_query("fragment F on User { name } { users { id ...F @skip(if: $nope) } }")
        .expect("query must parse");
    let err = resolve_and_filter(
        &parsed.selections,
        &parsed.fragments,
        &variables_map(None),
        DEFAULT_MAX_QUERY_DEPTH,
    )
    .expect_err("an undefined condition variable must not silently include the fragment");
    assert!(matches!(err, SelectionError::Directive(_)), "got: {err:?}");
}

#[test]
fn an_undefined_fragment_is_an_error() {
    let parsed = parse_query("{ users { id ...Missing } }").expect("query must parse");
    let err = resolve(&parsed.selections, &parsed.fragments, DEFAULT_MAX_QUERY_DEPTH)
        .expect_err("a spread naming no fragment must be refused");
    assert!(matches!(err, SelectionError::Fragment(_)), "got: {err:?}");
}

#[test]
fn resolve_is_independent_of_variables() {
    // The executor's parse cache is keyed by the query string alone, so the
    // expansion half must not depend on the request. If this ever stops holding,
    // cached mutation selections leak one request's variables into the next.
    let parsed =
        parse_query("fragment F on User { name } { users { id ...F @skip(if: $x) } }").unwrap();
    let a = resolve(&parsed.selections, &parsed.fragments, DEFAULT_MAX_QUERY_DEPTH).unwrap();
    let b = resolve(&parsed.selections, &parsed.fragments, DEFAULT_MAX_QUERY_DEPTH).unwrap();
    assert_eq!(child_names(&a), child_names(&b));
    assert_eq!(child_names(&a), vec!["id", "name"], "expansion must not evaluate conditions");
}

#[test]
fn variables_map_treats_a_non_object_payload_as_empty() {
    assert!(variables_map(None).is_empty());
    assert!(variables_map(Some(&json!(null))).is_empty());
    assert!(variables_map(Some(&json!([1, 2]))).is_empty());
    assert_eq!(variables_map(Some(&json!({"a": 1}))).len(), 1);
}

/// The resolver refuses at the depth it is handed, not at a fixed one: the executor
/// hands it its gate's `max_query_depth`, so the two are one bound.
#[test]
fn resolve_refuses_past_the_depth_it_is_handed() {
    let nested = |levels: usize| {
        format!("{{ users {}{{ id }}{} }}", "{ f ".repeat(levels), " }".repeat(levels))
    };
    let parsed = parse_query(&nested(30)).unwrap();
    assert!(resolve(&parsed.selections, &parsed.fragments, 64).is_ok());
    let err = resolve(&parsed.selections, &parsed.fragments, 20).expect_err("30 levels past 20");
    assert!(err.to_string().contains("20-level limit"), "{err}");
}

// ── § 6.3.2 field merging ────────────────────────────────────────────────────

/// The resolved root's children, each with its own children's response keys.
fn resolved_tree(query: &str, variables: &serde_json::Value) -> Vec<(String, Vec<String>)> {
    let parsed = parse_query(query).expect("query must parse");
    let vars = variables_map(Some(variables));
    let out =
        resolve_and_filter(&parsed.selections, &parsed.fragments, &vars, DEFAULT_MAX_QUERY_DEPTH)
            .expect("selection set must resolve");
    out[0]
        .nested_fields
        .iter()
        .map(|f| {
            let children = f.nested_fields.iter().map(|c| c.response_key().to_string()).collect();
            (f.response_key().to_string(), children)
        })
        .collect()
}

fn tree(entries: &[(&str, &[&str])]) -> Vec<(String, Vec<String>)> {
    entries
        .iter()
        .map(|(key, children)| {
            ((*key).to_string(), children.iter().map(|c| (*c).to_string()).collect())
        })
        .collect()
}

#[test]
fn a_field_written_twice_is_one_field_with_both_sub_selections() {
    assert_eq!(
        resolved_tree("{ users { posts { id } posts { title } } }", &json!({})),
        tree(&[("posts", &["id", "title"])])
    );
}

#[test]
fn a_field_selected_by_two_fragments_is_one_field_with_both_sub_selections() {
    assert_eq!(
        resolved_tree(
            "{ users { ...A ...B } } \
             fragment A on User { posts { id } } fragment B on User { posts { title } }",
            &json!({})
        ),
        tree(&[("posts", &["id", "title"])])
    );
}

#[test]
fn a_field_under_one_alias_twice_is_one_field_with_both_sub_selections() {
    assert_eq!(
        resolved_tree("{ users { p: posts { id } p: posts { title } } }", &json!({})),
        tree(&[("p", &["id", "title"])])
    );
}

#[test]
fn a_second_alias_of_the_same_field_stays_its_own_key() {
    assert_eq!(
        resolved_tree("{ users { posts { id } p: posts { title } } }", &json!({})),
        tree(&[("posts", &["id"]), ("p", &["title"])])
    );
}

#[test]
fn a_merged_field_keeps_the_position_of_its_first_occurrence() {
    assert_eq!(
        resolved_tree("{ users { posts { id } name posts { title } } }", &json!({})),
        tree(&[("posts", &["id", "title"]), ("name", &[])])
    );
}

#[test]
fn a_skipped_occurrence_contributes_nothing_to_the_merge() {
    // Directives are evaluated per occurrence before merging (CollectFields).
    assert_eq!(
        resolved_tree(
            "query($s: Boolean!) { users { posts @skip(if: $s) { id } posts { title } } }",
            &json!({"s": true})
        ),
        tree(&[("posts", &["title"])])
    );
}

#[test]
fn nested_occurrences_merge_at_every_level() {
    assert_eq!(
        resolved_tree(
            "{ users { posts { author { id } } posts { author { name } } } }",
            &json!({})
        ),
        tree(&[("posts", &["author"])])
    );
    let parsed =
        parse_query("{ users { posts { author { id } } posts { author { name } } } }").unwrap();
    let out = resolve_and_filter(
        &parsed.selections,
        &parsed.fragments,
        &variables_map(Some(&json!({}))),
        DEFAULT_MAX_QUERY_DEPTH,
    )
    .unwrap();
    let author: Vec<&str> = out[0].nested_fields[0].nested_fields[0]
        .nested_fields
        .iter()
        .map(FieldSelection::response_key)
        .collect();
    assert_eq!(author, vec!["id", "name"]);
}

#[test]
fn occurrences_with_different_arguments_are_not_merged() {
    // Merging them would answer one of the two argument sets with the other's rows.
    // § 5.3.2 refuses such a document; that check needs the type conditions named
    // spreads lose at expansion, so until it exists the occurrences stay apart.
    assert_eq!(
        resolved_tree("{ users { posts(limit: 1) { id } posts(limit: 2) { title } } }", &json!({})),
        tree(&[("posts", &["id"]), ("posts", &["title"])])
    );
}

#[test]
fn two_fields_under_one_alias_are_not_merged() {
    // `a` cannot be both `id` and `name` (§ 5.3.2); the occurrences stay apart.
    assert_eq!(
        resolved_tree("{ users { a: id a: name } }", &json!({})),
        tree(&[("a", &[]), ("a", &[])])
    );
}

fn written(query: &str) -> CollectedFields {
    let parsed = parse_query(query).expect("query must parse");
    CollectedFields::new(
        resolve(&parsed.selections, &parsed.fragments, DEFAULT_MAX_QUERY_DEPTH).unwrap(),
    )
}

/// `collect` answers as `filter` does, as a `Debug` rendering of the fields or of the error.
fn collect_and_filter(fields: &CollectedFields, variables: &serde_json::Value) -> (String, String) {
    let vars = variables_map(Some(variables));
    let collected = fields.collect(&vars).map(|c| format!("{:?}", c.fields()));
    let filtered = filter(fields.written(), &vars).map(|f| format!("{:?}", &f[..]));
    (format!("{collected:?}"), format!("{filtered:?}"))
}

/// One document, requests whose directives keep different fields: each collects what it
/// keeps, not what the first request sharing the document kept, whichever order they come in.
#[test]
fn each_set_of_directive_outcomes_collects_its_own_fields() {
    let fields = written(
        "query Q($a: Boolean!, $b: Boolean!) { users { id name @include(if: $a) \
                 email @skip(if: $b) } }",
    );
    for vars in [
        json!({"a": true, "b": true}),
        json!({"a": false, "b": false}),
        json!({"a": true, "b": true}),
        json!({"a": false, "b": true}),
        json!({"a": true, "b": false}),
        json!({"a": false, "b": false}),
    ] {
        let (collected, filtered) = collect_and_filter(&fields, &vars);
        assert_eq!(collected, filtered, "{vars}");
    }
}

/// A condition `filter` never evaluates (after a deciding `@skip`, or under a skipped
/// field) cannot refuse the request here either; one it does evaluate refuses it the same.
#[test]
fn collecting_refuses_exactly_what_filtering_refuses() {
    for (query, vars) in [
        ("{ users { id name @skip(if: true) @include(if: $missing) } }", json!({})),
        (
            "{ users { id posts @skip(if: true) { title @include(if: $missing) } } }",
            json!({}),
        ),
        ("query Q($s: Boolean!) { users { id name @skip(if: $s) } }", json!({})),
        ("query Q($s: Boolean!) { users { id name @skip(if: $s) } }", json!({"s": "yes"})),
    ] {
        let fields = written(query);
        let first = collect_and_filter(&fields, &vars);
        assert_eq!(first.0, first.1, "{query} {vars}");
        let again = collect_and_filter(&fields, &vars);
        assert_eq!(again, first, "a second request: {query} {vars}");
    }
}

/// Outcomes that a parent decides: a nested field's outcome is recorded only when its parent
/// is kept, so `{a}` kept with its child skipped and `{a}` skipped cannot share an answer.
#[test]
fn a_nested_outcome_is_recorded_under_its_parents() {
    let fields = written(
        "query Q($p: Boolean!, $c: Boolean!) { users { id posts @include(if: $p) { title \
         @include(if: $c) body } } }",
    );
    for vars in [
        json!({"p": true, "c": false}),
        json!({"p": false, "c": true}),
        json!({"p": true, "c": true}),
        json!({"p": false, "c": false}),
    ] {
        let (collected, filtered) = collect_and_filter(&fields, &vars);
        assert_eq!(collected, filtered, "{vars}");
    }
}

/// More directive-bearing fields than outcomes fit: the request still collects what
/// `filter` keeps.
#[test]
fn a_document_with_more_outcomes_than_fit_still_collects_its_fields() {
    use std::fmt::Write as _;
    let mut many = String::new();
    for i in 0..70 {
        let _ = write!(many, "f{i}: name @include(if: $v{}) ", i % 2);
    }
    let fields =
        written(&format!("query Q($v0: Boolean!, $v1: Boolean!) {{ users {{ {many} }} }}"));
    for vars in [
        json!({"v0": true, "v1": false}),
        json!({"v0": false, "v1": true}),
    ] {
        let (collected, filtered) = collect_and_filter(&fields, &vars);
        assert_eq!(collected, filtered, "{vars}");
    }
}

/// A check that passed on collected fields is not run on them again; one that failed is.
#[test]
fn a_check_on_collected_fields_is_kept_only_once_it_passes() {
    let fields = written("{ users { id } }");
    let vars = variables_map(None);
    let runs = std::cell::Cell::new(0);
    let refuse = |_: &[FieldSelection]| {
        runs.set(runs.get() + 1);
        Err(())
    };
    assert!(
        fields
            .collect(&vars)
            .unwrap()
            .check_once(FieldsCheck::Document, refuse)
            .is_err()
    );
    assert!(
        fields
            .collect(&vars)
            .unwrap()
            .check_once(FieldsCheck::Document, refuse)
            .is_err()
    );
    let pass = |_: &[FieldSelection]| {
        runs.set(runs.get() + 1);
        Ok::<(), ()>(())
    };
    fields.collect(&vars).unwrap().check_once(FieldsCheck::Document, pass).unwrap();
    fields.collect(&vars).unwrap().check_once(FieldsCheck::Document, pass).unwrap();
    assert_eq!(runs.get(), 3);
}

/// Each check keeps its own verdict: one that passed does not stand in for another on
/// the same fields, which still runs and still refuses.
#[test]
fn a_passed_check_does_not_stand_in_for_another() {
    let fields = written("{ users { id } }");
    let vars = variables_map(None);
    let collected = fields.collect(&vars).unwrap();
    collected.check_once(FieldsCheck::Document, |_| Ok::<(), ()>(())).unwrap();
    assert!(collected.check_once(FieldsCheck::Locale, |_| Err(())).is_err());
    collected.check_once(FieldsCheck::Locale, |_| Ok::<(), ()>(())).unwrap();
    assert!(collected.check_once(FieldsCheck::Document, |_| Err(())).is_ok(), "kept");
}

/// A projection built for collected fields is built once per key and shared; one that
/// failed to build is built again, and one under another key is built and not kept.
#[test]
fn a_projection_of_collected_fields_is_kept_only_once_it_is_built() {
    use crate::{
        db::DatabaseType,
        graphql::selection_set::{KeptProjection, ProjectionKey},
        schema::SqlProjectionHint,
    };

    let fields = written("{ users { id } }");
    let vars = variables_map(None);
    let first_key = ProjectionKey::new(None, []);
    let builds = std::cell::Cell::new(0);
    let fail = |_: &[FieldSelection], _: &ProjectionKey| {
        builds.set(builds.get() + 1);
        Err(())
    };
    assert!(fields.collect(&vars).unwrap().projection_once(&first_key, fail).is_err());
    assert!(fields.collect(&vars).unwrap().projection_once(&first_key, fail).is_err());
    let build = |_: &[FieldSelection], _: &ProjectionKey| {
        builds.set(builds.get() + 1);
        Ok::<_, ()>(KeptProjection {
            hint:             std::sync::Arc::new(SqlProjectionHint::new(
                DatabaseType::PostgreSQL,
                "p".to_string(),
                0,
            )),
            decimals_as_text: false,
            built:            true,
        })
    };
    let first = fields.collect(&vars).unwrap().projection_once(&first_key, build).unwrap();
    let second = fields.collect(&vars).unwrap().projection_once(&first_key, build).unwrap();
    assert_eq!(builds.get(), 3);
    assert!(
        std::sync::Arc::ptr_eq(&first.hint, &second.hint),
        "the second read shares the first's"
    );

    let other_key = ProjectionKey::new(Some(vec!["de-DE".to_string()]), []);
    let other = fields.collect(&vars).unwrap().projection_once(&other_key, build).unwrap();
    assert_eq!(builds.get(), 4, "another key builds its own");
    assert!(!std::sync::Arc::ptr_eq(&first.hint, &other.hint), "another key reads its own");
    let again = fields.collect(&vars).unwrap().projection_once(&first_key, build).unwrap();
    assert_eq!(builds.get(), 4, "the first key's is still kept");
    assert!(std::sync::Arc::ptr_eq(&first.hint, &again.hint));
}

/// A key is the caller's masks as a set: the order they were found in and a repeat do not
/// fork it, and a mask of another field does.
#[test]
fn a_projection_key_holds_the_callers_masks_as_a_set() {
    use crate::graphql::selection_set::ProjectionKey;

    let mask = |t: &str, f: &str| (t.to_string(), f.to_string());
    let key = ProjectionKey::new(None, [mask("User", "salary"), mask("Profile", "email")]);
    assert_eq!(
        key,
        ProjectionKey::new(
            None,
            [
                mask("Profile", "email"),
                mask("User", "salary"),
                mask("User", "salary")
            ]
        )
    );
    assert_ne!(key, ProjectionKey::new(None, [mask("User", "salary")]));
    assert_ne!(key, ProjectionKey::new(None, []));
    assert!(key.masks("User", "salary"));
    assert!(key.masks("Profile", "email"));
    assert!(!key.masks("Profile", "salary"), "a mask is of one type's field");
    assert!(!key.masks("User", "email"), "a mask is of one type's field");
}