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
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
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
//! Unit tests for argument-value validation (§ 5.6.1, § 5.8.5, § 6.1.2).

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

use serde_json::json;

use super::*;
use crate::graphql::types::GraphQLType;

fn named(name: &str, nullable: bool) -> GraphQLType {
    GraphQLType {
        name: name.to_string(),
        nullable,
        list: false,
        list_nullable: true,
    }
}

fn var(name: &str, type_name: &str, nullable: bool) -> VariableDefinition {
    VariableDefinition {
        name:          name.to_string(),
        var_type:      named(type_name, nullable),
        default_value: None,
    }
}

fn literal(name: &str, value_type: &str, value_json: &str) -> GraphQLArgument {
    GraphQLArgument {
        name:       name.to_string(),
        value_type: value_type.to_string(),
        value_json: value_json.to_string(),
    }
}

fn var_use(name: &str, var_name: &str) -> GraphQLArgument {
    GraphQLArgument {
        name:       name.to_string(),
        value_type: "variable".to_string(),
        value_json: serde_json::to_string(&value_json::variable_ref(var_name)).unwrap(),
    }
}

fn limit_arg() -> Vec<ArgumentDefinition> {
    vec![ArgumentDefinition::optional("limit", FieldType::Int)]
}

fn message(result: Result<()>) -> String {
    match result {
        Err(FraiseQLError::Validation { message, .. }) => message,
        other => panic!("expected a Validation error, got {other:?}"),
    }
}

// ---------------------------------------------------------------- § 5.6.1

#[test]
fn an_int_literal_is_accepted_at_an_int_argument() {
    let args = [literal("limit", "int", "2")];
    assert!(validate_argument_values("Query.things", &limit_arg(), &args, &[]).is_ok());
}

#[test]
fn a_string_literal_is_refused_at_an_int_argument() {
    let args = [literal("limit", "string", "\"2\"")];
    let msg = message(validate_argument_values("Query.things", &limit_arg(), &args, &[]));
    assert!(msg.contains("limit"), "message was: {msg}");
    assert!(msg.contains("Int"), "message was: {msg}");
    assert!(msg.contains("String"), "message was: {msg}");
}

#[test]
fn a_float_literal_is_refused_at_an_int_argument() {
    let args = [literal("limit", "float", "2.5")];
    assert!(validate_argument_values("Query.things", &limit_arg(), &args, &[]).is_err());
}

#[test]
fn a_boolean_literal_is_refused_at_an_int_argument() {
    let args = [literal("limit", "boolean", "true")];
    assert!(validate_argument_values("Query.things", &limit_arg(), &args, &[]).is_err());
}

#[test]
fn an_int_literal_past_the_32_bit_range_is_refused() {
    let args = [literal("limit", "int", "99999999999999")];
    assert!(validate_argument_values("Query.things", &limit_arg(), &args, &[]).is_err());
}

#[test]
fn an_explicit_null_is_accepted_because_nullability_is_not_adjudicated_here() {
    let args = [literal("limit", "null", "null")];
    assert!(validate_argument_values("Query.things", &limit_arg(), &args, &[]).is_ok());
}

#[test]
fn an_argument_the_field_does_not_declare_is_left_to_the_name_rule() {
    // `first` is accepted by name on a relay query but carries no published
    // type, so it is not adjudicated here.
    let args = [literal("first", "string", "\"2\"")];
    assert!(validate_argument_values("Query.things", &limit_arg(), &args, &[]).is_ok());
}

#[test]
fn a_json_argument_accepts_every_shape() {
    let declared = vec![ArgumentDefinition::optional("where", FieldType::Json)];
    for (kind, body) in [("string", "\"x\""), ("int", "1"), ("object", "{\"a\":1}")] {
        let args = [literal("where", kind, body)];
        assert!(
            validate_argument_values("Query.things", &declared, &args, &[]).is_ok(),
            "JSON argument refused a {kind} value"
        );
    }
}

#[test]
fn an_enum_literal_is_not_adjudicated_as_a_scalar() {
    let declared = vec![ArgumentDefinition::optional("status", FieldType::String)];
    let args = [literal("status", "enum", "\"PENDING\"")];
    assert!(validate_argument_values("Query.things", &declared, &args, &[]).is_ok());
}

#[test]
fn a_string_literal_is_accepted_at_a_uuid_argument() {
    let declared = vec![ArgumentDefinition::new("id", FieldType::Uuid)];
    let args = [literal(
        "id",
        "string",
        "\"11110000-0000-0000-0000-000000000001\"",
    )];
    assert!(validate_argument_values("Query.thing", &declared, &args, &[]).is_ok());
}

#[test]
fn an_int_literal_is_refused_at_a_uuid_argument() {
    let declared = vec![ArgumentDefinition::new("id", FieldType::Uuid)];
    let args = [literal("id", "int", "1")];
    assert!(validate_argument_values("Query.thing", &declared, &args, &[]).is_err());
}

// ---------------------------------------------------------------- § 5.8.5

#[test]
fn an_int_declared_variable_is_usable_at_an_int_argument() {
    let vars = [var("n", "Int", false)];
    let args = [var_use("limit", "n")];
    assert!(validate_argument_values("Query.things", &limit_arg(), &args, &vars).is_ok());
}

#[test]
fn a_string_declared_variable_is_refused_at_an_int_argument() {
    let vars = [var("n", "String", false)];
    let args = [var_use("limit", "n")];
    let msg = message(validate_argument_values("Query.things", &limit_arg(), &args, &vars));
    assert!(msg.contains("$n"), "message was: {msg}");
    assert!(msg.contains("String"), "message was: {msg}");
    assert!(msg.contains("Int"), "message was: {msg}");
}

#[test]
fn a_string_declared_variable_is_allowed_at_a_uuid_argument() {
    // A code generator that maps a custom scalar to String is not the mistake
    // this rule exists to catch.
    let declared = vec![ArgumentDefinition::new("id", FieldType::Uuid)];
    let vars = [var("id", "String", false)];
    let args = [var_use("id", "id")];
    assert!(validate_argument_values("Query.thing", &declared, &args, &vars).is_ok());
}

#[test]
fn a_variable_declared_at_a_type_the_schema_does_not_publish_is_not_adjudicated() {
    let vars = [var("n", "SomeProjectScalar", false)];
    let args = [var_use("limit", "n")];
    assert!(validate_argument_values("Query.things", &limit_arg(), &args, &vars).is_ok());
}

#[test]
fn an_undeclared_variable_reference_is_left_to_the_5_8_3_rule() {
    let args = [var_use("limit", "nowhere")];
    assert!(validate_argument_values("Query.things", &limit_arg(), &args, &[]).is_ok());
}

// ---------------------------------------------------------------- § 6.1.2

#[test]
fn a_supplied_value_of_the_declared_type_is_accepted() {
    let vars = [var("n", "Int", false)];
    let values = json!({"n": 2});
    assert!(validate_variable_values(Some("Q"), &vars, Some(&values)).is_ok());
}

#[test]
fn a_supplied_value_contradicting_its_declaration_is_refused() {
    let vars = [var("n", "Int", false)];
    let values = json!({"n": "2"});
    let msg = message(validate_variable_values(Some("Q"), &vars, Some(&values)));
    assert!(msg.contains("$n"), "message was: {msg}");
    assert!(msg.contains("Int"), "message was: {msg}");
    assert!(msg.contains('Q'), "message was: {msg}");
}

#[test]
fn a_non_null_variable_with_no_value_is_refused() {
    let vars = [var("n", "Int", false)];
    let msg = message(validate_variable_values(None, &vars, None));
    assert!(msg.contains("$n"), "message was: {msg}");
    assert!(msg.contains("no value was supplied"), "message was: {msg}");
}

#[test]
fn a_non_null_variable_supplied_as_null_is_refused() {
    let vars = [var("n", "Int", false)];
    let values = json!({"n": null});
    assert!(validate_variable_values(None, &vars, Some(&values)).is_err());
}

#[test]
fn a_nullable_variable_with_no_value_is_accepted() {
    // Load-bearing: this is what lets `limit: $limit` fall back to the query's
    // compiled default instead of forcing `LIMIT NULL`.
    let vars = [var("n", "Int", true)];
    assert!(validate_variable_values(None, &vars, None).is_ok());
}

#[test]
fn a_non_null_variable_with_a_default_and_no_value_is_accepted() {
    let mut v = var("n", "Int", false);
    v.default_value = Some("10".to_string());
    assert!(validate_variable_values(None, &[v], None).is_ok());
}

#[test]
fn a_list_declared_variable_is_not_adjudicated() {
    let mut v = var("ids", "Int", true);
    v.var_type.list = true;
    let values = json!({"ids": [1, 2, 3]});
    assert!(validate_variable_values(None, &[v], Some(&values)).is_ok());
}

#[test]
fn a_variable_of_a_project_scalar_is_not_adjudicated() {
    let vars = [var("payload", "SomeProjectScalar", false)];
    let values = json!({"payload": {"anything": true}});
    assert!(validate_variable_values(None, &vars, Some(&values)).is_ok());
}

#[test]
fn an_out_of_range_int_says_so_rather_than_int_where_int_was_expected() {
    let args = [literal("limit", "int", "99999999999999")];
    let msg = message(validate_argument_values("Query.things", &limit_arg(), &args, &[]));
    assert!(msg.contains("32-bit"), "message was: {msg}");
    assert!(!msg.contains("a Int value"), "message was: {msg}");
}

// ── Enum membership — § 5.6.1 / § 6.1.2 for enums (#1362) ────────────────────

use crate::schema::{
    CompiledSchema, EnumDefinition, EnumValueDefinition, InputFieldDefinition,
    InputObjectDefinition,
};

/// `OrderStatus` with three members, `CreateOrderInput { reference, status }`, and a
/// `where`-shaped nesting so the walk into input objects is exercised by the same
/// fixture the defect was reported against.
fn enum_schema() -> CompiledSchema {
    let mut schema = CompiledSchema::new();
    schema.enums.push(
        EnumDefinition::new("OrderStatus")
            .with_value(EnumValueDefinition::new("PENDING"))
            .with_value(EnumValueDefinition::new("SHIPPED"))
            .with_value(EnumValueDefinition::new("CANCELLED")),
    );
    schema.input_types.push(
        InputObjectDefinition::new("CreateOrderInput")
            .with_field(InputFieldDefinition::new("reference", "String"))
            .with_field(InputFieldDefinition::new("status", "OrderStatus"))
            .with_field(InputFieldDefinition::new("history", "[OrderStatus!]"))
            .with_field(InputFieldDefinition::new("nested", "CreateOrderInput")),
    );
    schema
}

fn input_arg() -> Vec<ArgumentDefinition> {
    vec![ArgumentDefinition::optional(
        "input",
        // The compiler emits an input-type reference as `Object`, never `Input`.
        FieldType::Object("CreateOrderInput".to_string()),
    )]
}

fn status_arg() -> Vec<ArgumentDefinition> {
    vec![ArgumentDefinition::optional(
        "status",
        FieldType::Enum("OrderStatus".to_string()),
    )]
}

/// The whole finding in one case: the shape the issue reported, through the entry
/// every transport that writes uses.
#[test]
fn a_non_member_at_an_enum_input_field_is_refused() {
    let schema = enum_schema();
    let values = json!({"input": {"reference": "r-1", "status": "BANANA"}});
    let err = message(validate_mutation_argument_values(
        &schema,
        "Mutation.createOrder",
        &input_arg(),
        Some(&values),
    ));
    assert!(err.contains("OrderStatus"), "the refusal must name the enum: {err}");
    assert!(err.contains("input.status"), "the refusal must name the path: {err}");
    assert!(
        err.contains("PENDING, SHIPPED, CANCELLED"),
        "the refusal must name the members introspection already publishes: {err}"
    );
    assert!(
        !err.contains("BANANA"),
        "the offending value is described, never quoted back (#1197): {err}"
    );
}

#[test]
fn a_member_at_an_enum_input_field_is_accepted() {
    let schema = enum_schema();
    let values = json!({"input": {"reference": "r-1", "status": "SHIPPED"}});
    assert!(
        validate_mutation_argument_values(&schema, "M.m", &input_arg(), Some(&values)).is_ok(),
        "a declared member must pass"
    );
}

/// Every row of the issue's table, in one place. The value spelling matters: a
/// member's *value* rather than its name, and a JSON number, were both forwarded.
#[test]
fn the_reported_non_member_spellings_are_each_refused() {
    let schema = enum_schema();
    for wrote in [
        json!("BANANA"),
        json!("pending"),
        json!("anything at all"),
        json!(42),
    ] {
        let values = json!({"input": {"status": wrote}});
        assert!(
            validate_mutation_argument_values(&schema, "M.m", &input_arg(), Some(&values)).is_err(),
            "{wrote} is not a member of OrderStatus and must be refused"
        );
    }
}

#[test]
fn a_non_member_inside_a_list_of_enums_is_refused() {
    let schema = enum_schema();
    let values = json!({"input": {"history": ["PENDING", "BANANA"]}});
    let err =
        message(validate_mutation_argument_values(&schema, "M.m", &input_arg(), Some(&values)));
    assert!(err.contains("input.history[1]"), "the refusal must name the element: {err}");
}

/// § 3.11 — a bare value stands for a one-element list, so a list declaration must
/// not become a hole an unchecked enum fits through.
#[test]
fn a_bare_non_member_written_at_a_list_of_enums_is_refused() {
    let schema = enum_schema();
    let values = json!({"input": {"history": "BANANA"}});
    assert!(
        validate_mutation_argument_values(&schema, "M.m", &input_arg(), Some(&values)).is_err()
    );
}

#[test]
fn a_non_member_nested_one_input_object_deeper_is_refused() {
    let schema = enum_schema();
    let values = json!({"input": {"nested": {"status": "BANANA"}}});
    let err =
        message(validate_mutation_argument_values(&schema, "M.m", &input_arg(), Some(&values)));
    assert!(err.contains("input.nested.status"), "the refusal must name the path: {err}");
}

#[test]
fn a_non_member_at_a_bare_enum_argument_is_refused() {
    let schema = enum_schema();
    let values = json!({"status": "BANANA"});
    assert!(
        validate_mutation_argument_values(&schema, "Q.q", &status_arg(), Some(&values)).is_err()
    );
}

#[test]
fn an_absent_or_null_enum_field_is_left_to_the_required_field_rule() {
    let schema = enum_schema();
    for values in [
        json!({"input": {"reference": "r"}}),
        json!({"input": {"status": null}}),
    ] {
        assert!(
            validate_mutation_argument_values(&schema, "M.m", &input_arg(), Some(&values)).is_ok(),
            "nullability is #414's question, not this one"
        );
    }
}

/// The leniency policy still holds for everything the schema does not enumerate.
#[test]
fn a_field_whose_type_the_schema_does_not_declare_is_not_adjudicated() {
    let mut schema = enum_schema();
    schema.input_types.push(
        InputObjectDefinition::new("Loose")
            .with_field(InputFieldDefinition::new("whatever", "SomeProjectScalar")),
    );
    let declared = vec![ArgumentDefinition::optional(
        "input",
        FieldType::Object("Loose".to_string()),
    )];
    let values = json!({"input": {"whatever": "anything at all"}});
    assert!(validate_mutation_argument_values(&schema, "M.m", &declared, Some(&values)).is_ok());
}

// ── the literal path ─────────────────────────────────────────────────────────

#[test]
fn a_non_member_written_as_an_inline_enum_literal_is_refused() {
    let schema = enum_schema();
    // `value_json` encodes a bare GraphQL enum name as a JSON string — the same
    // shape a variable supplies, which is what lets one walk cover both.
    let args = [literal("status", "enum", "\"BANANA\"")];
    assert!(
        validate_enum_argument_literals(&schema, "Query.orders", &status_arg(), &args).is_err(),
        "an inline literal must be adjudicated, not only a variable"
    );
}

#[test]
fn a_member_written_as_an_inline_enum_literal_is_accepted() {
    let schema = enum_schema();
    let args = [literal("status", "enum", "\"SHIPPED\"")];
    assert!(validate_enum_argument_literals(&schema, "Query.orders", &status_arg(), &args).is_ok());
}

/// A literal that is a *reference* is the variable check's half. Adjudicating the
/// marker object here would refuse every `status: $s`.
#[test]
fn a_variable_reference_is_left_to_the_variable_check() {
    let schema = enum_schema();
    let args = [var_use("status", "s")];
    assert!(validate_enum_argument_literals(&schema, "Query.orders", &status_arg(), &args).is_ok());
}

// ── the variable path (§ 6.1.2) ──────────────────────────────────────────────

#[test]
fn a_non_member_supplied_for_an_enum_variable_is_refused() {
    let schema = enum_schema();
    let defs = [var("s", "OrderStatus", true)];
    let values = json!({"s": "BANANA"});
    let err = message(validate_enum_variable_values(&schema, Some("Op"), &defs, Some(&values)));
    assert!(err.contains("$s"), "the refusal must name the variable: {err}");
    assert!(err.contains("Op"), "the refusal must name the operation: {err}");
}

#[test]
fn a_non_member_supplied_inside_an_input_object_variable_is_refused() {
    let schema = enum_schema();
    let defs = [var("input", "CreateOrderInput", true)];
    let values = json!({"input": {"status": "BANANA"}});
    let err = message(validate_enum_variable_values(&schema, None, &defs, Some(&values)));
    assert!(err.contains("status"), "the refusal must name the field: {err}");
}

#[test]
fn a_member_supplied_for_an_enum_variable_is_accepted() {
    let schema = enum_schema();
    let defs = [var("s", "OrderStatus", true)];
    assert!(
        validate_enum_variable_values(&schema, None, &defs, Some(&json!({"s": "PENDING"}))).is_ok()
    );
}

/// A schema that declares no enums cannot refuse anything — the #939 principle:
/// reject what the schema positively contradicts, not an absence of evidence.
#[test]
fn a_schema_declaring_no_enums_adjudicates_nothing() {
    let schema = CompiledSchema::new();
    let defs = [var("s", "OrderStatus", true)];
    assert!(
        validate_enum_variable_values(&schema, None, &defs, Some(&json!({"s": "BANANA"}))).is_ok()
    );
}

// ── The write walk: scalars at every depth, declared keys only (#1528) ───────

#[test]
fn a_wrong_scalar_nested_in_a_mutation_input_is_refused_with_its_path() {
    let schema = enum_schema();
    let values = json!({"input": {"nested": {"reference": ["r"]}}});
    let err =
        message(validate_mutation_argument_values(&schema, "M.m", &input_arg(), Some(&values)));
    assert!(err.contains("`input.nested.reference`"), "names the path: {err}");
    assert!(
        err.contains("`String`") && err.contains("wrote a list"),
        "names the types: {err}"
    );
}

#[test]
fn an_undeclared_key_in_a_mutation_input_is_refused() {
    let schema = enum_schema();
    let values = json!({"input": {"reference": "r", "colour": "red"}});
    let err =
        message(validate_mutation_argument_values(&schema, "M.m", &input_arg(), Some(&values)));
    assert!(
        err.contains("`input.colour`") && err.contains("declares no such field"),
        "{err}"
    );
}

#[test]
fn a_localized_input_field_is_left_to_its_own_pass() {
    let mut schema = enum_schema();
    let mut name = InputFieldDefinition::new("name", "String");
    name.localized = true;
    schema.input_types[0].fields.push(name);
    let values = json!({"input": {"name": {"fr-FR": "Pomme"}}});
    assert!(validate_mutation_argument_values(&schema, "M.m", &input_arg(), Some(&values)).is_ok());
}

#[test]
fn a_string_that_is_not_a_uuid_is_refused_at_a_uuid_argument() {
    let declared = vec![ArgumentDefinition::optional("id", FieldType::Uuid)];
    let args = [literal("id", "string", "\"x\"")];
    let err = message(validate_argument_values("Query.thing", &declared, &args, &[]));
    assert!(err.contains("not a UUID"), "{err}");
    let ok = [literal(
        "id",
        "string",
        "\"3f2504e0-4f89-11d3-9a0c-0305e82c3301\"",
    )];
    assert!(
        validate_argument_values("Query.thing", &declared, &ok, &[]).is_ok(),
        "a UUID passes"
    );
}

/// #1610: in a document literal, a nested `{"$var": …}` is a variable reference, left to the
/// variable's own check; as data (a payload, a variable's value) the same object is an object
/// written at an enum position, and is refused.
#[test]
fn a_variable_marker_is_a_reference_only_in_a_document_literal() {
    let schema = enum_schema();
    let marker = json!({"status": {"$var": "s"}});
    let reference = literal("input", "object", &marker.to_string());

    assert!(
        validate_enum_argument_literals(&schema, "Query.orders", &input_arg(), &[reference])
            .is_ok(),
        "a nested variable reference in a literal is the variable's to adjudicate"
    );
    let payload = json!({"input": marker});
    assert!(
        message(validate_mutation_argument_values(
            &schema,
            "Mutation.createOrder",
            &input_arg(),
            Some(&payload)
        ))
        .contains("OrderStatus"),
        "a marker-shaped object in a payload is data, refused at an enum position"
    );
    assert!(
        message(validate_enum_variable_values(
            &schema,
            None,
            &[var("i", "CreateOrderInput", true)],
            Some(&json!({"i": marker}))
        ))
        .contains("OrderStatus"),
        "a marker-shaped object in a variable's value is data, refused at an enum position"
    );
}

// ── Resolved list items — § 3.11 for `[T!]` (#1614) ──────────────────────────

/// `LabelWhere { name: StringFilter, note: String, _and: [LabelWhere!] }` over
/// `StringFilter { nin: [String!] }`: the shape a derived filter takes.
fn where_schema() -> (CompiledSchema, Vec<ArgumentDefinition>) {
    let mut schema = CompiledSchema::new();
    schema.input_types.push(
        InputObjectDefinition::new("StringFilter")
            .with_field(InputFieldDefinition::new("nin", "[String!]")),
    );
    schema.input_types.push(
        InputObjectDefinition::new("LabelWhere")
            .with_field(InputFieldDefinition::new("name", "StringFilter"))
            .with_field(InputFieldDefinition::new("note", "String"))
            .with_field(InputFieldDefinition::new("_and", "[LabelWhere!]")),
    );
    let declared = vec![ArgumentDefinition::optional(
        "where",
        FieldType::Object("LabelWhere".to_string()),
    )];
    (schema, declared)
}

fn list_items(where_value: &Value) -> Result<()> {
    let (schema, declared) = where_schema();
    validate_argument_list_items(
        &schema,
        || "Query.labels".to_string(),
        &declared,
        |name| (name == "where").then_some(where_value),
    )
}

/// Every path the walk is skipped or taken on: a null item under the argument's input
/// object, one under a list of input objects, and a null that is no list's item.
#[test]
fn a_null_item_is_found_wherever_it_sits_in_a_resolved_argument() {
    assert!(
        message(list_items(&json!({"name": {"nin": [null, "first"]}})))
            .contains("where.name.nin[0]"),
        "an operand list under the argument's input object"
    );
    assert!(
        message(list_items(&json!({"_and": [{"name": {"nin": ["first", null]}}]})))
            .contains("where._and[0].name.nin[1]"),
        "an operand list under a list of input objects"
    );
    assert!(
        list_items(&json!({"note": null, "name": {"nin": ["first"]}})).is_ok(),
        "a null field value is no list's item"
    );
}

// ---------------------------------------------------------------- Decimal input

fn payment_schema() -> CompiledSchema {
    let mut schema = CompiledSchema::new();
    schema.input_types.push(
        InputObjectDefinition::new("PayInput")
            .with_field(InputFieldDefinition::new("amount", "Decimal"))
            .with_field(InputFieldDefinition::new("splits", "[Decimal!]")),
    );
    schema
}

fn pay_arg() -> Vec<ArgumentDefinition> {
    vec![ArgumentDefinition::optional(
        "input",
        FieldType::Object("PayInput".to_string()),
    )]
}

/// A request's JSON text, parsed as a server parses it: a number with a fraction, an
/// exponent or more digits than a `u64` holds is already an `f64`.
fn parsed(text: &str) -> Value {
    serde_json::from_str(text).unwrap()
}

/// A Decimal takes its digits as a string, or an integer held exactly; a number with a
/// fraction or an exponent was rounded to an `f64` before anything could read it.
#[test]
fn a_decimal_takes_a_string_or_an_exact_integer() {
    let schema = payment_schema();
    let pays = |amount: &str| {
        let values =
            parsed(&format!(r#"{{"input": {{"amount": {amount}, "splits": [{amount}]}}}}"#));
        validate_mutation_argument_values(&schema, "Mutation.pay", &pay_arg(), Some(&values))
    };
    for held in [
        r#""12345678901234567.89""#,
        r#""-0.010""#,
        "7",
        "-9223372036854775808",
        "18446744073709551615",
    ] {
        assert!(pays(held).is_ok(), "{held}");
    }
    for rounded in ["1.5", "1e3", "1.0", "18446744073709551616"] {
        let msg = message(pays(rounded));
        assert!(msg.contains("input.amount") && msg.contains("Decimal"), "{rounded}: {msg}");
        assert!(msg.contains("as a string"), "the refusal says how to write one: {msg}");
    }
}

#[test]
fn a_decimal_argument_and_variable_take_the_same_values() {
    let declared = vec![ArgumentDefinition::new("min", FieldType::Decimal)];
    let literal_at = |text: &str, kind: &str| {
        validate_argument_values("Query.orders", &declared, &[literal("min", kind, text)], &[])
    };
    assert!(literal_at("10", "int").is_ok());
    assert!(literal_at(r#""10.25""#, "string").is_ok());
    assert!(literal_at("10.25", "float").is_err(), "a Float literal is already rounded");

    let vars = [var("min", "Decimal", false)];
    let supplied = |text: &str| {
        validate_variable_values(Some("Q"), &vars, Some(&parsed(&format!(r#"{{"min": {text}}}"#))))
    };
    assert!(supplied(r#""10.25""#).is_ok());
    assert!(supplied("10").is_ok());
    assert!(supplied("10.25").is_err());
}

#[test]
fn a_float_declared_variable_is_refused_at_a_decimal_argument() {
    let declared = vec![ArgumentDefinition::new("min", FieldType::Decimal)];
    let vars = [var("min", "Float", false)];
    let msg = message(validate_argument_values(
        "Query.orders",
        &declared,
        &[var_use("min", "min")],
        &vars,
    ));
    assert!(msg.contains("$min") && msg.contains("Float"), "{msg}");
}