libgraphql-parser 0.0.5

A blazing fast, error-focused, lossless GraphQL parser for schema, executable, and mixed documents.
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
//! Tests for parser error handling, error recovery, and lexer error integration.
//!
//! This module contains tests organized into several categories:
//! - Error Recovery - tests for collecting multiple errors
//! - Value Error Tests - integer overflow, etc.
//! - Unclosed Delimiter Tests - various unclosed brackets/braces
//! - Reserved Name Tests - validation of reserved names
//! - Directive Location Tests - validation of directive locations
//! - Document Enforcement Tests - schema vs executable validation
//! - Schema Extension - schema extension handling
//! - Error Recovery - recovery after errors
//! - Lexer Errors - lexer error propagation
//!
//! Written by Claude Code, reviewed by a human.

use crate::tests::utils::parse_executable;
use crate::tests::utils::parse_schema;

// =============================================================================
// Error Recovery
// =============================================================================

/// Verifies that multiple errors are collected in a single parse.
///
/// When a document contains multiple syntax errors, the parser should collect
/// all of them rather than stopping at the first error. This enables better
/// developer experience by reporting all issues at once.
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn recovery_multiple_errors() {
    let result = parse_schema("type A { } type B { field: !! } type C { }");
    assert!(result.has_errors());
}

/// Verifies that parsing continues after encountering a syntax error.
///
/// After an error in one type definition, the parser should recover and
/// continue parsing subsequent definitions. This tests the parser's ability
/// to synchronize after errors.
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn recovery_continues_after_error() {
    let result = parse_schema("type A { field:: } type B { field: String }");
    assert!(result.has_errors());
}

// =============================================================================
// Value Error Tests
// =============================================================================

/// Verifies that very large integers that overflow i64 produce errors.
///
/// GraphQL integers are 32-bit signed per the spec, but the parser accepts
/// values up to i64 range. Values beyond that should produce errors.
///
/// Spec reference:
/// <https://spec.graphql.org/September2025/#sec-Int-Value>
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn value_int_overflow_error() {
    let result = parse_executable(
        "query { field(arg: 99999999999999999999999999) }"
    );
    assert!(result.has_errors());
}

// =============================================================================
// Unclosed Delimiter Tests
// =============================================================================

/// Verifies that an unclosed `[` in a list value produces an error.
///
/// List values must have matching brackets. Missing the closing `]` should
/// result in a parse error.
///
/// Spec reference:
/// <https://spec.graphql.org/September2025/#sec-List-Value>
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn list_value_unclosed_bracket_error() {
    let result = parse_executable("query { field(arg: [1, 2) }");
    assert!(result.has_errors());
}

/// Verifies that an unclosed `{` in an object value produces an error.
///
/// Object values must have matching braces. Missing the closing `}` should
/// result in a parse error.
///
/// Spec reference:
/// <https://spec.graphql.org/September2025/#sec-Input-Object-Values>
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn object_value_unclosed_brace_error() {
    let result = parse_executable("query { field(arg: {a: 1) }");
    assert!(result.has_errors());
}

/// Verifies that a missing colon in an object value field produces an error.
///
/// Object field entries require the format `name: value`. Missing the colon
/// should result in a parse error.
///
/// Spec reference:
/// <https://spec.graphql.org/September2025/#sec-Input-Object-Values>
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn object_value_missing_colon_error() {
    let result = parse_executable("query { field(arg: {field 1}) }");
    assert!(result.has_errors());
}

/// Verifies that an unclosed type definition body produces an error.
///
/// Type definitions with field lists must have matching braces.
///
/// Spec reference:
/// <https://spec.graphql.org/September2025/#ObjectTypeDefinition>
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn field_definition_unclosed_brace() {
    let result = parse_schema("type T { f: String");
    assert!(result.has_errors());
}

/// Verifies that an unclosed input object definition produces an error.
///
/// Input object definitions with field lists must have matching braces.
///
/// Spec reference:
/// <https://spec.graphql.org/September2025/#InputObjectTypeDefinition>
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn input_object_unclosed_brace() {
    let result = parse_schema("input I { f: String");
    assert!(result.has_errors());
}

/// Verifies that an unclosed enum definition produces an error.
///
/// Enum definitions with value lists must have matching braces.
///
/// Spec reference:
/// <https://spec.graphql.org/September2025/#EnumTypeDefinition>
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn enum_definition_unclosed_brace() {
    let result = parse_schema("enum E { A");
    assert!(result.has_errors());
}

/// Verifies that an unclosed argument list produces an error.
///
/// Argument lists must have matching parentheses.
///
/// Spec reference:
/// <https://spec.graphql.org/September2025/#sec-Language.Arguments>
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn field_args_unclosed_paren_error() {
    let result = parse_executable("query { field(arg: 1 }");
    assert!(result.has_errors());
}

/// Verifies that an unclosed list type annotation produces an error.
///
/// List type annotations must have matching brackets.
///
/// Spec reference:
/// <https://spec.graphql.org/September2025/#sec-Type-References>
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn type_list_unclosed_bracket_error() {
    let result = parse_schema("type Q { f: [String }");
    assert!(result.has_errors());
}

// =============================================================================
// Reserved Name Tests
// =============================================================================

/// Verifies that `false` as an enum value produces an error.
///
/// Per GraphQL spec, `true`, `false`, and `null` cannot be enum values since
/// they would be ambiguous with boolean/null literals.
///
/// Spec reference:
/// <https://spec.graphql.org/September2025/#sec-Enum-Value-Uniqueness>
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn enum_value_false_reserved_error() {
    let result = parse_schema("enum Bool { false }");
    assert!(result.has_errors());
}

/// Verifies that reserved names can be used in non-reserved contexts.
///
/// While `true`, `false`, `null` cannot be enum values, they can be field
/// names in selection sets since the context makes them unambiguous.
///
/// Spec reference:
/// <https://spec.graphql.org/September2025/#sec-Names>
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn reserved_names_allowed_in_field_names() {
    let result = parse_executable("{ true false null }");
    assert!(!result.has_errors());
}

// =============================================================================
// Directive Location Tests
// =============================================================================

/// Verifies that an unknown directive location produces an error.
///
/// Directive definitions must use valid location names from the spec.
///
/// Spec reference:
/// <https://spec.graphql.org/September2025/#DirectiveLocation>
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn directive_unknown_location_error() {
    let result = parse_schema("directive @d on UNKNOWN");
    assert!(result.has_errors());
}

/// Verifies that directive location names are case-sensitive.
///
/// `FIELD` is a valid location, but `field` is not since directive locations
/// must be uppercase.
///
/// Spec reference:
/// <https://spec.graphql.org/September2025/#DirectiveLocation>
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn directive_location_case_sensitive() {
    let result = parse_schema("directive @d on field");
    assert!(result.has_errors());
}

// =============================================================================
// Document Enforcement Tests
// =============================================================================

/// Verifies that a type definition with description in executable doc errors.
///
/// When parsing as executable, type definitions (even with descriptions) are
/// not allowed since executable documents only contain operations and fragments.
///
/// Spec reference:
/// <https://spec.graphql.org/September2025/#sec-Executable-Documents>
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn executable_rejects_type_with_description() {
    let result = parse_executable(r#""description" type T { f: Int }"#);
    assert!(result.has_errors());
}

/// Verifies that schema definition in an executable document produces an error.
///
/// Schema definitions are only valid in schema documents, not executable ones.
///
/// Spec reference:
/// <https://spec.graphql.org/September2025/#sec-Executable-Documents>
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn executable_rejects_schema_definition() {
    let result = parse_executable("schema { query: Query }");
    assert!(result.has_errors());
}

/// Verifies that scalar definition in an executable document produces an error.
///
/// Scalar definitions are only valid in schema documents.
///
/// Spec reference:
/// <https://spec.graphql.org/September2025/#sec-Executable-Documents>
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn executable_rejects_scalar_definition() {
    let result = parse_executable("scalar DateTime");
    assert!(result.has_errors());
}

/// Verifies that interface definition in an executable document produces error.
///
/// Interface definitions are only valid in schema documents.
///
/// Spec reference:
/// <https://spec.graphql.org/September2025/#sec-Executable-Documents>
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn executable_rejects_interface_definition() {
    let result = parse_executable("interface Node { id: ID! }");
    assert!(result.has_errors());
}

/// Verifies that union definition in an executable document produces an error.
///
/// Union definitions are only valid in schema documents.
///
/// Spec reference:
/// <https://spec.graphql.org/September2025/#sec-Executable-Documents>
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn executable_rejects_union_definition() {
    let result = parse_executable("union Result = A | B");
    assert!(result.has_errors());
}

/// Verifies that enum definition in an executable document produces an error.
///
/// Enum definitions are only valid in schema documents.
///
/// Spec reference:
/// <https://spec.graphql.org/September2025/#sec-Executable-Documents>
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn executable_rejects_enum_definition() {
    let result = parse_executable("enum Status { ACTIVE }");
    assert!(result.has_errors());
}

/// Verifies that input definition in an executable document produces an error.
///
/// Input object definitions are only valid in schema documents.
///
/// Spec reference:
/// <https://spec.graphql.org/September2025/#sec-Executable-Documents>
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn executable_rejects_input_definition() {
    let result = parse_executable("input CreateInput { name: String }");
    assert!(result.has_errors());
}

// =============================================================================
// Schema Extension
// =============================================================================

/// Verifies that schema extension is handled without crashing.
///
/// The GraphQL spec defines schema extensions. This test verifies that the
/// parser handles them gracefully (either parsing successfully or returning
/// an error) without panicking.
///
/// Spec reference:
/// <https://spec.graphql.org/September2025/#sec-Schema-Extension>
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn schema_extension_parses() {
    // Either succeeds or has errors, but should not panic
    let _ = parse_schema("extend schema { query: Query }");
}

// =============================================================================
// Error Recovery
// =============================================================================

/// Verifies that the parser can recover from an error in a type definition.
///
/// After encountering a syntax error in one type definition, the parser should
/// be able to recover and continue parsing subsequent definitions.
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn recovery_after_error_in_type_definition() {
    let result = parse_schema(
        "type A { field:: } type B { field: String }"
    );
    assert!(result.has_errors());
}

/// Verifies that recovery works across multiple definitions with errors.
///
/// When multiple type definitions have errors, the parser should attempt to
/// recover and continue parsing, collecting all errors.
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn recovery_multiple_type_errors() {
    let result = parse_schema(
        "type A { bad:: } type B { also_bad!! } type C { field: String }"
    );
    assert!(result.has_errors());
}

/// Verifies that recovery works for operations with errors.
///
/// The parser should be able to recover from errors in operation definitions
/// and continue parsing subsequent operations.
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn recovery_operation_errors() {
    let result = parse_executable("query A { field( } query B { field }");
    assert!(result.has_errors());
}

/// Verifies recovery handles empty selection sets gracefully.
///
/// Empty selection sets are invalid per the spec and should produce an error,
/// but the parser should handle them without crashing.
///
/// Spec reference:
/// <https://spec.graphql.org/September2025/#sec-Selection-Sets>
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn recovery_empty_selection_set() {
    let result = parse_executable("{ }");
    assert!(result.has_errors());
}

/// Verifies that deeply nested unclosed delimiters produce errors.
///
/// When multiple levels of nesting are left unclosed, the parser should
/// detect and report the missing closing delimiters.
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn recovery_deeply_nested_unclosed() {
    let result = parse_executable("{ a { b { c { d");
    assert!(result.has_errors());
}

// =============================================================================
// Lexer Errors
// =============================================================================

/// Verifies that an unterminated string produces a lexer error.
///
/// Strings must be properly terminated with a closing quote. The lexer should
/// detect unterminated strings and report an error.
///
/// Spec reference:
/// <https://spec.graphql.org/September2025/#sec-String-Value>
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn lexer_error_unterminated_string() {
    let result = parse_executable(r#"{ field(arg: "unterminated) }"#);
    assert!(result.has_errors());
}

/// Verifies that an unterminated block string produces a lexer error.
///
/// Block strings must be properly terminated with `"""`. The lexer should
/// detect unterminated block strings and report an error.
///
/// Spec reference:
/// <https://spec.graphql.org/September2025/#sec-String-Value>
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn lexer_error_unterminated_block_string() {
    let result = parse_executable(r#"{ field(arg: """unterminated) }"#);
    assert!(result.has_errors());
}

/// Verifies that invalid characters produce lexer errors.
///
/// Characters not part of the GraphQL lexical grammar should produce errors.
/// For example, the backtick character is not valid in GraphQL.
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn lexer_error_invalid_character() {
    let result = parse_executable("{ field` }");
    assert!(result.has_errors());
}

/// Verifies that invalid escape sequences in strings produce lexer errors.
///
/// Only specific escape sequences are valid in GraphQL strings. Invalid
/// escape sequences like `\q` should produce errors.
///
/// Spec reference:
/// <https://spec.graphql.org/September2025/#EscapedCharacter>
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn lexer_error_invalid_escape_sequence() {
    let result = parse_executable(r#"{ field(arg: "hello\qworld") }"#);
    assert!(result.has_errors());
}

/// Verifies that invalid number formats produce lexer errors.
///
/// Leading zeros are not allowed in GraphQL integers (except for `0` itself).
/// Numbers like `007` should produce errors.
///
/// Spec reference:
/// <https://spec.graphql.org/September2025/#sec-Int-Value>
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn lexer_error_number_format() {
    let result = parse_executable("{ field(arg: 007) }");
    assert!(result.has_errors());
}

/// Verifies that an exponent without digits produces a lexer error.
///
/// Float values with exponents must have digits after the `e` or `E`.
/// Values like `1e` are invalid and should produce errors.
///
/// Spec reference:
/// <https://spec.graphql.org/September2025/#sec-Float-Value>
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn lexer_error_exponent_without_digits() {
    let result = parse_executable("{ field(arg: 1e) }");
    assert!(result.has_errors());
}