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
//! Source-slice round-trip tests for the GraphQL parser.
//!
//! These tests verify that parsing a GraphQL source string and then
//! reconstructing it via `doc.to_source(Some(source))` produces output
//! that is character-for-character identical to the original input.
//!
//! This validates that the parser's span tracking is correct: every
//! AST node records accurate byte offsets into the original source, so
//! slicing `&source[start..end]` for each node reproduces the original
//! text verbatim.
//!
//! Written by Claude Code, reviewed by a human.

use crate::ast;
use crate::ast::AstNode;
use crate::tests::utils::parse_executable;
use crate::tests::utils::parse_mixed;
use crate::tests::utils::parse_schema;

// =============================================================================
// Helpers
// =============================================================================

/// Parse source as an executable document, then reconstruct via
/// source-slice and assert the result matches the original.
fn assert_roundtrip_executable(source: &str) {
    let result = parse_executable(source);
    assert!(
        !result.has_errors(),
        "Parse failed:\n{}",
        result.formatted_errors(),
    );
    let (doc, _) = result.into_valid().unwrap();
    let reconstructed = doc.to_source(Some(source));
    assert_eq!(reconstructed, source, "Round-trip mismatch");
}

/// Parse source as a schema document, then reconstruct via
/// source-slice and assert the result matches the original.
fn assert_roundtrip_schema(source: &str) {
    let result = parse_schema(source);
    assert!(
        !result.has_errors(),
        "Parse failed:\n{}",
        result.formatted_errors(),
    );
    let (doc, _) = result.into_valid().unwrap();
    let reconstructed = doc.to_source(Some(source));
    assert_eq!(reconstructed, source, "Round-trip mismatch");
}

/// Parse source as a mixed document, then reconstruct via
/// source-slice and assert the result matches the original.
fn assert_roundtrip_mixed(source: &str) {
    let result = parse_mixed(source);
    assert!(
        !result.has_errors(),
        "Parse failed:\n{}",
        result.formatted_errors(),
    );
    let (doc, _) = result.into_valid().unwrap();
    let reconstructed = doc.to_source(Some(source));
    assert_eq!(reconstructed, source, "Round-trip mismatch");
}

// =============================================================================
// 1. Operations (executable)
// =============================================================================

/// Verifies source-slice round trip for a shorthand query (anonymous
/// selection set without the `query` keyword).
///
/// Also verifies sub-node slicing: the single field's
/// `to_source(Some(source))` should return just `"field"`.
///
/// Per GraphQL spec:
/// <https://spec.graphql.org/September2025/#sec-Language.Operations>
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn roundtrip_shorthand_query() {
    let source = "{ field }";
    assert_roundtrip_executable(source);

    // Sub-node assertion: the field should slice to just "field".
    let (doc, _) = parse_executable(source).into_valid().unwrap();
    if let ast::Definition::OperationDefinition(op) = &doc.definitions[0] {
        if let ast::Selection::Field(field) =
            &op.selection_set.selections[0]
        {
            assert_eq!(
                field.to_source(Some(source)),
                "field",
                "Sub-node field slice mismatch",
            );
        } else {
            panic!("Expected Field selection");
        }
    } else {
        panic!("Expected OperationDefinition");
    }
}

/// Verifies source-slice round trip for a named query with multiple
/// fields.
///
/// Also verifies sub-node slicing: the operation's
/// `to_source(Some(source))` should reproduce the full operation text.
///
/// Per GraphQL spec:
/// <https://spec.graphql.org/September2025/#sec-Language.Operations>
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn roundtrip_named_query() {
    let source = "query GetUser { name age }";
    assert_roundtrip_executable(source);

    // Sub-node assertion: the operation should slice to the full
    // source since it's the only definition.
    let (doc, _) = parse_executable(source).into_valid().unwrap();
    if let ast::Definition::OperationDefinition(op) = &doc.definitions[0] {
        assert_eq!(
            op.to_source(Some(source)),
            source,
            "Sub-node operation slice mismatch",
        );
    } else {
        panic!("Expected OperationDefinition");
    }
}

/// Verifies source-slice round trip for a query with variable
/// definitions, including a default value.
///
/// Per GraphQL spec:
/// <https://spec.graphql.org/September2025/#sec-Language.Variables>
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn roundtrip_query_with_variables() {
    let source =
        "query Q($id: ID!, $limit: Int = 10) { user(id: $id) { name } }";
    assert_roundtrip_executable(source);
}

/// Verifies source-slice round trip for a mutation operation.
///
/// Per GraphQL spec:
/// <https://spec.graphql.org/September2025/#sec-Language.Operations>
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn roundtrip_mutation() {
    let source =
        "mutation CreateUser($name: String!) { createUser(name: $name) { id } }";
    assert_roundtrip_executable(source);
}

/// Verifies source-slice round trip for a subscription operation.
///
/// Per GraphQL spec:
/// <https://spec.graphql.org/September2025/#sec-Language.Operations>
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn roundtrip_subscription() {
    let source = "subscription OnMessage { messageAdded { text } }";
    assert_roundtrip_executable(source);
}

// =============================================================================
// 2. Fragments (executable)
// =============================================================================

/// Verifies source-slice round trip for a document containing both a
/// fragment definition and a query that uses a fragment spread.
///
/// Also verifies sub-node slicing: the fragment definition's
/// `to_source(Some(source))` should return just the fragment portion,
/// and the query operation should return just the query portion.
///
/// Per GraphQL spec:
/// <https://spec.graphql.org/September2025/#sec-Language.Fragments>
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn roundtrip_fragment_definition() {
    let source =
        "fragment UserFields on User { name email }\n\
         query { user { ...UserFields } }";
    assert_roundtrip_executable(source);

    // Sub-node assertions: each definition should slice to its own
    // text.
    let (doc, _) = parse_executable(source).into_valid().unwrap();
    assert_eq!(doc.definitions.len(), 2);

    if let ast::Definition::FragmentDefinition(frag) = &doc.definitions[0] {
        assert_eq!(
            frag.to_source(Some(source)),
            "fragment UserFields on User { name email }",
            "Sub-node fragment slice mismatch",
        );
    } else {
        panic!("Expected FragmentDefinition");
    }

    if let ast::Definition::OperationDefinition(op) = &doc.definitions[1] {
        assert_eq!(
            op.to_source(Some(source)),
            "query { user { ...UserFields } }",
            "Sub-node query slice mismatch",
        );
    } else {
        panic!("Expected OperationDefinition");
    }
}

/// Verifies source-slice round trip for inline fragments with type
/// conditions.
///
/// Per GraphQL spec:
/// <https://spec.graphql.org/September2025/#sec-Inline-Fragments>
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn roundtrip_inline_fragment() {
    let source =
        "{ node { ... on User { name } ... on Bot { handle } } }";
    assert_roundtrip_executable(source);
}

// =============================================================================
// 3. Values (executable)
// =============================================================================

/// Verifies source-slice round trip for a query exercising all GraphQL
/// value types: Int, Float, String, Boolean, Null, Enum, List, and
/// InputObject.
///
/// Per GraphQL spec:
/// <https://spec.graphql.org/September2025/#sec-Input-Values>
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn roundtrip_all_value_types() {
    let source = concat!(
        "{ field(",
        "int: 42, ",
        "float: 3.14, ",
        "str: \"hello\", ",
        "yes: true, ",
        "no: false, ",
        "nil: null, ",
        "enumVal: ACTIVE, ",
        "list: [1, 2], ",
        "obj: {key: \"val\"}",
        ") }",
    );
    assert_roundtrip_executable(source);
}

// =============================================================================
// 4. Directives (executable)
// =============================================================================

/// Verifies source-slice round trip for directives on both operations
/// and fields.
///
/// Per GraphQL spec:
/// <https://spec.graphql.org/September2025/#sec-Language.Directives>
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn roundtrip_directives() {
    let source =
        "query @skip(if: true) { field @include(if: false) }";
    assert_roundtrip_executable(source);
}

// =============================================================================
// 5. Schema types (schema)
// =============================================================================

/// Verifies source-slice round trip for an object type definition with
/// field arguments and descriptions.
///
/// Per GraphQL spec:
/// <https://spec.graphql.org/September2025/#sec-Objects>
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn roundtrip_object_type() {
    let source = concat!(
        "type User {\n",
        "  name: String\n",
        "  age(minAge: Int = 0): Int\n",
        "}",
    );
    assert_roundtrip_schema(source);
}

/// Verifies source-slice round trip for an interface type definition
/// with an `implements` clause.
///
/// Per GraphQL spec:
/// <https://spec.graphql.org/September2025/#sec-Interfaces>
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn roundtrip_interface_type() {
    let source = "interface Node implements Entity { id: ID! }";
    assert_roundtrip_schema(source);
}

/// Verifies source-slice round trip for an enum type definition with
/// values.
///
/// Per GraphQL spec:
/// <https://spec.graphql.org/September2025/#sec-Enums>
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn roundtrip_enum_type() {
    let source = "enum Status { ACTIVE INACTIVE PENDING }";
    assert_roundtrip_schema(source);
}

/// Verifies source-slice round trip for a union type definition.
///
/// Per GraphQL spec:
/// <https://spec.graphql.org/September2025/#sec-Unions>
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn roundtrip_union_type() {
    let source = "union SearchResult = User | Post | Comment";
    assert_roundtrip_schema(source);
}

/// Verifies source-slice round trip for a scalar type definition.
///
/// Per GraphQL spec:
/// <https://spec.graphql.org/September2025/#sec-Scalars>
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn roundtrip_scalar_type() {
    let source = "scalar DateTime";
    assert_roundtrip_schema(source);
}

/// Verifies source-slice round trip for an input object type
/// definition.
///
/// Per GraphQL spec:
/// <https://spec.graphql.org/September2025/#sec-Input-Objects>
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn roundtrip_input_object_type() {
    let source = concat!(
        "input CreateUserInput {\n",
        "  name: String!\n",
        "  email: String\n",
        "}",
    );
    assert_roundtrip_schema(source);
}

/// Verifies source-slice round trip for a directive definition with
/// multiple locations.
///
/// Per GraphQL spec:
/// <https://spec.graphql.org/September2025/#sec-Type-System.Directives>
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn roundtrip_directive_definition() {
    let source =
        "directive @auth(role: String!) on FIELD_DEFINITION | OBJECT";
    assert_roundtrip_schema(source);
}

/// Verifies source-slice round trip for a schema definition with
/// root operation types.
///
/// Per GraphQL spec:
/// <https://spec.graphql.org/September2025/#sec-Schema>
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn roundtrip_schema_definition() {
    let source = "schema { query: Query mutation: Mutation }";
    assert_roundtrip_schema(source);
}

// =============================================================================
// 6. Extensions (schema)
// =============================================================================

/// Verifies source-slice round trip for all type extension kinds:
/// object, enum, union, interface, scalar, and input object.
///
/// Per GraphQL spec:
/// <https://spec.graphql.org/September2025/#sec-Type-Extensions>
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn roundtrip_type_extensions() {
    let source = concat!(
        "extend type User { age: Int }\n",
        "\n",
        "extend enum Status { INACTIVE }\n",
        "\n",
        "extend union SearchResult = Photo\n",
        "\n",
        "extend interface Node { createdAt: DateTime }\n",
        "\n",
        "extend scalar Date @deprecated\n",
        "\n",
        "extend input CreateUserInput { nickname: String }",
    );
    assert_roundtrip_schema(source);
}

// =============================================================================
// 7. Whitespace & trivia (mixed)
// =============================================================================

/// Verifies source-slice round trip for a multiline document
/// containing `#` comments. Comments are trivia captured in spans, so
/// they must survive the round trip.
///
/// Per GraphQL spec:
/// <https://spec.graphql.org/September2025/#sec-Comments>
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn roundtrip_multiline_with_comments() {
    let source = concat!(
        "# This is a comment\n",
        "type User {\n",
        "  # The name field\n",
        "  name: String\n",
        "  age: Int\n",
        "}",
    );
    assert_roundtrip_mixed(source);
}

/// Verifies source-slice round trip for a document with extra
/// whitespace — including leading/trailing whitespace and extra
/// spaces between tokens.
///
/// Source-slice mode should preserve all whitespace exactly since it
/// slices the original source by byte offsets.
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn roundtrip_extra_whitespace() {
    let source = "  query   {   field   }  ";
    assert_roundtrip_mixed(source);
}

/// Verifies source-slice round trip for a document using commas as
/// insignificant list separators between fields.
///
/// Per GraphQL spec:
/// <https://spec.graphql.org/September2025/#sec-Insignificant-Commas>
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn roundtrip_commas() {
    let source = "{ a, b, c }";
    assert_roundtrip_mixed(source);
}

// =============================================================================
// 8. Descriptions (schema)
// =============================================================================

/// Verifies source-slice round trip for string and block-string
/// descriptions on types and fields.
///
/// Per GraphQL spec:
/// <https://spec.graphql.org/September2025/#sec-Descriptions>
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn roundtrip_string_descriptions() {
    let source = concat!(
        "\"A user in the system\"\n",
        "type User {\n",
        "  \"The user's name\"\n",
        "  name: String\n",
        "  \"\"\"\n",
        "  The user's email address.\n",
        "  Must be unique.\n",
        "  \"\"\"\n",
        "  email: String!\n",
        "}",
    );
    assert_roundtrip_schema(source);
}