hive-router 0.3.0

GraphQL router for Federation, part of the Hive platform
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
//! Input coercion of OneOf input objects (spec §3.10.1) for JSON variables: exactly one field
//! must be given, and its value must not be `null`.

use super::harness::{assert_coerced, assert_rejected, assert_rejected_by_validation};

/// `OneOfTestInput @oneOf { a: String, b: Int }` (graphql-js `oneof-test.ts`, the spec's
/// `ExampleOneOfInputObject`)
const ONE_OF: &str = "query ($input: OneOfTestInput!) { test(input: $input) { a b } }";
/// `IntOneOf @oneOf { foo: Int, bar: Int }`
const INT_ONE_OF: &str = "query ($input: IntOneOf) { intOneOf(input: $input) }";
/// `[OneOfTestInput!]`
const ONE_OF_LIST: &str = "query ($input: [OneOfTestInput!]) { oneOfList(input: $input) }";
/// `OneOfHolder { one: OneOfTestInput, many: [OneOfTestInput!], name: String }`
const ONE_OF_HOLDER: &str = "query ($input: OneOfHolder) { oneOfHolder(input: $input) }";
/// `RecursiveOneOf @oneOf { leaf: Int, and: [RecursiveOneOf!], not: RecursiveOneOf }`
const RECURSIVE_ONE_OF: &str = "query ($input: RecursiveOneOf) { recursiveOneOf(input: $input) }";

/// The error for a OneOf value that doesn't have exactly one non-null field.
fn one_of_error(type_name: &str) -> String {
    format!(
        r#"Within OneOf Input Object type "{type_name}", exactly one field must be specified, and the value for that field must be non-null."#
    )
}

/// Asserts that `variables` is rejected with `Variable "$input" has invalid value<path>: <reason>`.
#[track_caller]
fn rejects(operation: &str, variables: &str, path: &str, reason: &str) {
    assert_rejected(
        operation,
        variables,
        &format!(r#"Variable "$input" has invalid value{path}: {reason}"#),
    );
}

/// Asserts that `variables` is accepted and forwarded unchanged.
#[track_caller]
fn accepts(operation: &str, variables: &str) {
    assert_coerced(operation, variables, variables);
}

mod valid_values {
    use super::*;

    /// graphql-js `oneof-test.ts` "accepts a good variable", "accepts a good variable with an
    /// undefined key"; `validateInputValue-test.ts` "for GraphQLInputObject that isOneOf > no error
    /// for a valid input", "does not count undefined keys as provided"; `coerceInputValue-test.ts`
    /// "for GraphQLInputObject that isOneOf > returns for valid input", "for GraphQLInputObject
    /// that isOneOf > returns no error for a valid input"
    #[test]
    fn accepts_oneof_with_single_string_member() {
        accepts(ONE_OF, r#"{"input":{"a":"abc"}}"#);
    }

    /// spec §3.10.1
    #[test]
    fn accepts_oneof_with_single_int_member() {
        accepts(ONE_OF, r#"{"input":{"b":123}}"#);
    }

    /// `0`, `""` and `false` are not `null`.
    #[test]
    fn accepts_oneof_with_falsy_non_null_member() {
        accepts(ONE_OF, r#"{"input":{"b":0}}"#);
        accepts(ONE_OF, r#"{"input":{"a":""}}"#);
    }

    /// spec §6.1.2: an explicit `null` beats the default, and a `null` value has no fields to
    /// count.
    #[test]
    fn accepts_explicit_null_for_nullable_oneof_variable_with_default() {
        accepts(
            r#"query ($input: OneOfTestInput = {a: "abc"}) { test(input: $input) { a b } }"#,
            r#"{"input":null}"#,
        );
    }

    /// The rule applies to `@oneOf` types only.
    #[test]
    fn does_not_apply_oneof_rule_to_regular_input_object() {
        accepts(
            "query ($input: ExampleInputObject) { exampleInput(input: $input) }",
            r#"{"input":{"a":"abc","b":1}}"#,
        );
    }
}

/// spec §3.10.1: the value must have exactly one field, and it must not be `null`.
mod count_and_null {
    use super::*;

    /// spec §3.10.1
    #[test]
    fn rejects_oneof_with_no_fields() {
        rejects(
            ONE_OF,
            r#"{"input":{}}"#,
            "",
            &one_of_error("OneOfTestInput"),
        );
    }

    /// graphql-js `oneof-test.ts` "rejects a variable with multiple non-null keys"; spec §3.10.1
    #[test]
    fn rejects_oneof_with_two_non_null_fields() {
        rejects(
            ONE_OF,
            r#"{"input":{"a":"abc","b":123}}"#,
            "",
            &one_of_error("OneOfTestInput"),
        );
    }

    /// graphql-js `oneof-test.ts` "rejects a variable with multiple nullable keys";
    /// `validateInputValue-test.ts` "returns error if more than one field is specified";
    /// `coerceInputValue-test.ts` "invalid if more than one field is specified", "returns an error
    /// if more than one field is specified". A field given as `null` still counts.
    #[test]
    fn rejects_oneof_with_additional_null_field() {
        rejects(
            ONE_OF,
            r#"{"input":{"a":"abc","b":null}}"#,
            "",
            &one_of_error("OneOfTestInput"),
        );
    }

    /// spec §2.10.8: key order does not matter.
    #[test]
    fn rejects_oneof_with_null_field_listed_first() {
        rejects(
            ONE_OF,
            r#"{"input":{"b":null,"a":"abc"}}"#,
            "",
            &one_of_error("OneOfTestInput"),
        );
    }

    /// spec §3.10.1
    #[test]
    fn rejects_oneof_with_all_fields_null() {
        rejects(
            ONE_OF,
            r#"{"input":{"a":null,"b":null}}"#,
            "",
            &one_of_error("OneOfTestInput"),
        );
    }

    /// spec §3.10.1: the number of fields is checked before their values. graphql-js reports
    /// `… at .a: String cannot represent a non string value: 456` first.
    #[test]
    fn rejects_oneof_with_two_invalid_members() {
        rejects(
            ONE_OF,
            r#"{"input":{"a":456,"b":"xyz"}}"#,
            "",
            &one_of_error("OneOfTestInput"),
        );
    }

    /// graphql-js `oneof-test.ts` "rejects a variable with a nulled key";
    /// `validateInputValue-test.ts` "returns error if the one field is null";
    /// `coerceInputValue-test.ts` "invalid if the one field is null", "returns an error the one
    /// field is null"
    #[test]
    fn rejects_oneof_with_single_null_member() {
        rejects(
            ONE_OF,
            r#"{"input":{"a":null}}"#,
            " at .a",
            &one_of_error("OneOfTestInput"),
        );
    }
}

mod member_values_and_unknown_fields {
    use super::*;

    /// graphql-js `validateInputValue-test.ts` "returns error for an invalid field";
    /// `coerceInputValue-test.ts` "for GraphQLInputObject that isOneOf > invalid for an invalid
    /// field", "for GraphQLInputObject that isOneOf > returns an error for an invalid field" (`NaN`
    /// there; JSON cannot carry it)
    #[test]
    fn rejects_oneof_member_of_wrong_type() {
        rejects(
            INT_ONE_OF,
            r#"{"input":{"foo":1.5}}"#,
            " at .foo",
            "Int cannot represent non-integer value: 1.5",
        );
    }

    /// spec §3.10.1
    #[test]
    fn rejects_non_object_for_oneof() {
        rejects(
            INT_ONE_OF,
            r#"{"input":123}"#,
            "",
            r#"Expected value of type "IntOneOf" to be an object, found: 123."#,
        );
    }

    /// spec §3.10.1
    #[test]
    fn rejects_array_for_non_list_oneof() {
        rejects(
            ONE_OF,
            r#"{"input":[{"a":"abc"}]}"#,
            "",
            r#"Expected value of type "OneOfTestInput" to be an object, found: [{ a: "abc" }]."#,
        );
    }

    /// graphql-js `validateInputValue-test.ts` "for GraphQLInputObject that isOneOf > returns error
    /// for an unknown field"; `coerceInputValue-test.ts` "for GraphQLInputObject that isOneOf >
    /// invalid for an unknown field", "for GraphQLInputObject that isOneOf > returns error for an
    /// unknown field". spec §3.10.1: every given field counts, so this is a OneOf error. graphql-js
    /// reports only the unknown field.
    #[test]
    fn rejects_unknown_field_next_to_valid_member() {
        rejects(
            INT_ONE_OF,
            r#"{"input":{"foo":123,"unknownField":123}}"#,
            "",
            &one_of_error("IntOneOf"),
        );
    }

    /// graphql-js `validateInputValue-test.ts` "for GraphQLInputObject that isOneOf > returns error
    /// for a misspelled field"; `coerceInputValue-test.ts` "for GraphQLInputObject that isOneOf >
    /// returns error for a misspelled field" (the variant without suggestions)
    #[test]
    fn rejects_oneof_with_only_unknown_field() {
        rejects(
            INT_ONE_OF,
            r#"{"input":{"bart":123}}"#,
            "",
            r#"Expected value of type "IntOneOf" not to include unknown field "bart", found: { bart: 123 }."#,
        );
    }
}

mod nested {
    use super::*;

    /// spec §3.10.1 and §3.11
    #[test]
    fn accepts_list_of_valid_oneofs() {
        accepts(ONE_OF_LIST, r#"{"input":[{"a":"abc"},{"b":1}]}"#);
    }

    #[test]
    fn rejects_list_item_with_two_oneof_fields() {
        rejects(
            ONE_OF_LIST,
            r#"{"input":[{"a":"x"},{"a":"x","b":1}]}"#,
            " at [1]",
            &one_of_error("OneOfTestInput"),
        );
    }

    #[test]
    fn rejects_list_item_with_null_oneof_member() {
        rejects(
            ONE_OF_LIST,
            r#"{"input":[{"a":null}]}"#,
            " at [0].a",
            &one_of_error("OneOfTestInput"),
        );
    }

    /// The router forwards the value as sent; the subgraph wraps it into a list.
    #[test]
    fn accepts_single_oneof_for_list_type_unwrapped() {
        accepts(ONE_OF_LIST, r#"{"input":{"a":"abc"}}"#);
    }

    #[test]
    fn rejects_single_empty_oneof_for_list_type() {
        rejects(
            ONE_OF_LIST,
            r#"{"input":{}}"#,
            "",
            &one_of_error("OneOfTestInput"),
        );
    }

    #[test]
    fn accepts_oneof_nested_in_input_object() {
        accepts(
            ONE_OF_HOLDER,
            r#"{"input":{"one":{"a":"abc"},"many":[{"b":1}]}}"#,
        );
    }

    #[test]
    fn accepts_null_for_nullable_nested_oneof_field() {
        accepts(ONE_OF_HOLDER, r#"{"input":{"one":null,"name":"x"}}"#);
    }

    #[test]
    fn rejects_nested_oneof_with_two_fields() {
        rejects(
            ONE_OF_HOLDER,
            r#"{"input":{"one":{"a":"abc","b":1}}}"#,
            " at .one",
            &one_of_error("OneOfTestInput"),
        );
    }

    #[test]
    fn rejects_nested_oneof_with_null_member() {
        rejects(
            ONE_OF_HOLDER,
            r#"{"input":{"one":{"a":null}}}"#,
            " at .one.a",
            &one_of_error("OneOfTestInput"),
        );
    }

    #[test]
    fn rejects_empty_oneof_in_nested_list() {
        rejects(
            ONE_OF_HOLDER,
            r#"{"input":{"many":[{"a":"x"},{}]}}"#,
            " at .many[1]",
            &one_of_error("OneOfTestInput"),
        );
    }

    /// An empty list is a non-null value.
    #[test]
    fn accepts_recursive_oneof_with_empty_list_member() {
        accepts(RECURSIVE_ONE_OF, r#"{"input":{"not":{"and":[]}}}"#);
    }

    #[test]
    fn rejects_invalid_oneof_deep_in_recursive_oneof() {
        rejects(
            RECURSIVE_ONE_OF,
            r#"{"input":{"and":[{"leaf":1},{"not":{}}]}}"#,
            " at .and[1].not",
            &one_of_error("RecursiveOneOf"),
        );
    }

    /// spec §6.1.2: coercion stops at the first error.
    #[test]
    fn many_invalid_oneof_items_yield_one_request_error() {
        let items = vec!["{}"; 1000].join(",");
        rejects(
            ONE_OF_LIST,
            &format!(r#"{{"input":[{items}]}}"#),
            " at [0]",
            &one_of_error("OneOfTestInput"),
        );
    }
}

/// spec §6.1.2: a default is coerced by the variable type when it is used. Invalid OneOf
/// defaults are rejected by validation, before coercion.
mod defaults {
    use super::*;

    /// `query ($input: OneOfTestInput! = <default>) { test(input: $input) { a b } }`
    fn one_of_with_default(default: &str) -> String {
        format!("query ($input: OneOfTestInput! = {default}) {{ test(input: $input) {{ a b }} }}")
    }

    /// `query ($input: TestOneOfInputObject = <default>) { fieldWithOneOfObjectInput(input: $input) }`
    fn test_one_of_with_default(default: &str) -> String {
        format!(
            "query ($input: TestOneOfInputObject = {default}) {{ fieldWithOneOfObjectInput(input: $input) }}"
        )
    }

    /// graphql-js `oneof-test.ts` "accepts a good default value"; `valueFromAST-test.ts` "coerces
    /// input objects according to input coercion rules"; `coerceInputValue-test.ts` "coerces input
    /// objects according to input coercion rules"
    #[test]
    fn accepts_good_oneof_default() {
        assert_coerced(
            &one_of_with_default(r#"{a: "abc"}"#),
            "{}",
            r#"{"input":{"a":"abc"}}"#,
        );
    }

    /// graphql-js `valueFromAST-test.ts` "coerces input objects according to input coercion rules";
    /// `coerceInputValue-test.ts` "coerces input objects according to input coercion rules"
    #[test]
    fn accepts_good_oneof_default_for_second_member() {
        assert_coerced(
            &test_one_of_with_default(r#"{b: "def"}"#),
            "{}",
            r#"{"input":{"b":"def"}}"#,
        );
    }

    /// graphql-js `oneof-test.ts` "rejects a bad default value"; `valueFromAST-test.ts` "coerces
    /// input objects according to input coercion rules"; `coerceInputValue-test.ts` "coerces input
    /// objects according to input coercion rules"
    #[test]
    fn oneof_default_with_two_fields_is_rejected_by_validation() {
        assert_rejected_by_validation(
            &one_of_with_default(r#"{a: "abc", b: 123}"#),
            "ValuesOfCorrectType",
        );
    }

    /// graphql-js `valueFromAST-test.ts` "coerces input objects according to input coercion rules";
    /// `coerceInputValue-test.ts` "coerces input objects according to input coercion rules"
    #[test]
    fn oneof_default_with_additional_null_field_is_rejected_by_validation() {
        assert_rejected_by_validation(
            &test_one_of_with_default(r#"{a: "abc", b: null}"#),
            "ValuesOfCorrectType",
        );
    }

    /// graphql-js `valueFromAST-test.ts` "coerces input objects according to input coercion rules";
    /// `coerceInputValue-test.ts` "coerces input objects according to input coercion rules"
    #[test]
    fn oneof_default_with_single_null_member_is_rejected_by_validation() {
        assert_rejected_by_validation(
            &test_one_of_with_default("{a: null}"),
            "ValuesOfCorrectType",
        );
    }

    /// graphql-js `valueFromAST-test.ts` "coerces input objects according to input coercion rules";
    /// `coerceInputValue-test.ts` "coerces input objects according to input coercion rules"
    #[test]
    fn empty_oneof_default_is_rejected_by_validation() {
        assert_rejected_by_validation(&test_one_of_with_default("{}"), "ValuesOfCorrectType");
    }

    /// graphql-js `valueFromAST-test.ts` "coerces input objects according to input coercion rules";
    /// `coerceInputValue-test.ts` "coerces input objects according to input coercion rules"
    #[test]
    fn oneof_default_with_unknown_field_is_rejected_by_validation() {
        assert_rejected_by_validation(
            &test_one_of_with_default(r#"{a: "abc", c: "def"}"#),
            "ValuesOfCorrectType",
        );
    }
}

/// OneOf argument literals are checked by validation (spec §5.6.1 and §5.8.5).
mod literals {
    use super::*;

    /// graphql-js `validateInputValue-test.ts` "errors with missing variables as the additional
    /// field"; spec §3.10.1
    #[test]
    fn oneof_literal_with_two_fields_is_rejected_by_validation() {
        assert_rejected_by_validation(
            r#"{ test(input: {a: "abc", b: 123}) { a b } }"#,
            "ValuesOfCorrectType",
        );
    }

    /// spec §3.10.1
    #[test]
    fn oneof_literal_with_null_field_is_rejected_by_validation() {
        assert_rejected_by_validation("{ test(input: {a: null}) { a b } }", "ValuesOfCorrectType");
    }

    /// graphql-js `oneof-test.ts` "errors with nulled variable for field";
    /// `validateInputValue-test.ts` "for GraphQLInputObject that isOneOf > allows correct use of
    /// variables". A nullable variable could make the field `null`.
    #[test]
    fn nullable_variable_in_oneof_literal_is_rejected_by_validation() {
        assert_rejected_by_validation(
            "query ($a: String) { test(input: {a: $a}) { a b } }",
            "VariablesInAllowedPosition",
        );
    }

    /// graphql-js `VariablesInAllowedPositionRule.ts`: a default does not help, since the client
    /// can still send `null`.
    #[test]
    fn defaulted_nullable_variable_in_oneof_literal_is_rejected_by_validation() {
        assert_rejected_by_validation(
            r#"query ($a: String = "abc") { test(input: {a: $a}) { a b } }"#,
            "VariablesInAllowedPosition",
        );
    }
}

/// A non-null variable used as the field of a OneOf literal is coerced by its own type.
mod variables_in_literals {
    use super::*;

    const NON_NULL: &str = "query ($a: String!) { test(input: {a: $a}) { a b } }";

    /// graphql-js `validateInputValue-test.ts` "for GraphQLInputObject that isOneOf > allows
    /// correct use of variables"
    #[test]
    fn forwards_non_null_variable_in_oneof_literal() {
        assert_coerced(NON_NULL, r#"{"a":"abc"}"#, r#"{"a":"abc"}"#);
    }

    /// graphql-js `validateInputValue-test.ts` "returns error with variable provided a value of
    /// null"
    #[test]
    fn rejects_null_for_non_null_variable_in_oneof_literal() {
        assert_rejected(
            NON_NULL,
            r#"{"a":null}"#,
            r#"Variable "$a" has invalid value: Expected value of non-null type "String!" not to be null."#,
        );
    }

    /// spec §3.10.1
    #[test]
    fn rejects_missing_non_null_variable_in_oneof_literal() {
        assert_rejected(
            NON_NULL,
            "{}",
            r#"Variable "$a" has invalid value: Expected a value of non-null type "String!" to be provided."#,
        );
    }
}