libgraphql-macros 0.0.10

Macros provided by the `libgraphql` crate at `libgraphql::macros`.
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
//! Integration tests for interesting edge cases when parsing
//! GraphQL schemas through `RustMacroGraphQLTokenSource`.
//!
//! These tests verify token source behavior for cases that
//! require special handling: block string recombination,
//! negative number recombination, and description parsing
//! through the Rust token pipeline.
//!
//! Tests that use `TokenStream::from_str()` (rather than
//! `quote!`) do so because `quote!` produces synthetic
//! spans with no position info, which prevents the block
//! string adjacency detection in
//! `RustMacroGraphQLTokenSource::try_combine_block_string`.

use crate::rust_macro_graphql_token_source::RustMacroGraphQLTokenSource;
use libgraphql_core::ast;
use libgraphql_parser::GraphQLParser;
use libgraphql_parser::ParseResult;
use quote::quote;
use std::cell::RefCell;
use std::collections::HashMap;
use std::rc::Rc;
use std::str::FromStr;

fn parse_schema_from_str(
    input: &str,
) -> ParseResult<'static, ast::schema::Document> {
    let stream = proc_macro2::TokenStream::from_str(input)
        .expect("Failed to parse as Rust tokens");
    parse_schema_from_quote(stream)
}

fn parse_schema_from_quote(
    input: proc_macro2::TokenStream,
) -> ParseResult<'static, ast::schema::Document> {
    let span_map = Rc::new(RefCell::new(HashMap::new()));
    let token_source =
        RustMacroGraphQLTokenSource::new(input, span_map);
    let parser =
        GraphQLParser::from_token_source(token_source);
    let result = parser.parse_schema_document();
    let mut errors = result.errors().to_vec();
    let doc = result.into_ast();
    let compat =
        libgraphql_parser::compat::graphql_parser_v0_4
            ::to_graphql_parser_schema_ast(
                &doc,
                &libgraphql_parser::SourceMap::empty(),
            );
    errors.extend(compat.errors().to_vec());
    let legacy_doc = compat.into_ast();
    if errors.is_empty() {
        ParseResult::Ok {
            ast: legacy_doc,
            source_map: libgraphql_parser::SourceMap::empty(),
        }
    } else {
        ParseResult::Recovered {
            ast: legacy_doc,
            errors,
            source_map: libgraphql_parser::SourceMap::empty(),
        }
    }
}

/// Extract the first object type from a parsed document.
fn first_object_type(
    doc: &ast::schema::Document,
) -> &ast::schema::ObjectType {
    for def in &doc.definitions {
        if let ast::schema::Definition::TypeDefinition(
            ast::schema::TypeDefinition::Object(obj),
        ) = def
        {
            return obj;
        }
    }
    panic!("No object type found in document");
}

/// Verify that block string descriptions (`"""..."""`) are
/// correctly recombined from the three Rust tokens
/// (`""`, `"content"`, `""`) by the token source when
/// parsed via `TokenStream::from_str` (real positions).
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn test_block_string_description_on_field() {
    let result = parse_schema_from_str(
        r#"type User {
            """The user's primary address"""
            address: String!
        }"#,
    );

    assert!(
        !result.has_errors(),
        "Should parse block string descriptions: {:?}",
        result.errors(),
    );
    let (doc, _) = result.into_valid().unwrap();
    let obj = first_object_type(&doc);
    let field = &obj.fields[0];

    assert_eq!(field.name, "address");
    let desc = field.description.as_ref()
        .expect("Field should have a description");
    assert!(
        desc.contains("user's primary address"),
        "Expected description containing 'user's primary \
         address', got: {desc}",
    );
}

/// Verify that block string descriptions with escaped
/// quotes are correctly parsed and the quote characters
/// survive into the AST.
///
/// Note: Embedded `"` inside `"""..."""` must be escaped
/// as `\"` in Rust source because Rust's tokenizer treats
/// unescaped `"` as string delimiters, breaking the block
/// string recombination.
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn test_block_string_with_escaped_quotes() {
    let result = parse_schema_from_str(
        r#"type Response {
            """The formatted \"output\" string."""
            output: String
        }"#,
    );

    assert!(
        !result.has_errors(),
        "Should handle escaped quotes: {:?}",
        result.errors(),
    );
    let (doc, _) = result.into_valid().unwrap();
    let obj = first_object_type(&doc);
    let desc = obj.fields[0].description.as_ref()
        .expect("Field should have a description");
    assert!(
        desc.contains("output"),
        "Description should contain 'output', got: {desc}",
    );
}

/// Verify that block string descriptions on field arguments
/// are recombined correctly by the token source.
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn test_block_string_on_field_arguments() {
    let result = parse_schema_from_str(
        r#"type DataProcessor {
            process(
                """The target format for processing."""
                format: String
            ): String
        }"#,
    );

    assert!(
        !result.has_errors(),
        "Should handle argument descriptions: {:?}",
        result.errors(),
    );
    let (doc, _) = result.into_valid().unwrap();
    let obj = first_object_type(&doc);
    let arg = &obj.fields[0].arguments[0];

    assert_eq!(arg.name, "format");
    let desc = arg.description.as_ref()
        .expect("Argument should have a description");
    assert!(
        desc.contains("target format"),
        "Expected description containing 'target format', \
         got: {desc}",
    );
}

/// Verify that multiple field arguments each with block
/// string descriptions are parsed correctly, including
/// multiline block strings.
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn test_block_string_on_multiple_arguments() {
    let result = parse_schema_from_str(
        r#"type DataProcessor {
            filter(
                """List of filter criteria to apply."""
                criteria: [FilterCriterion]

                """
                Include related records.
                Defaults to true.
                """
                includeRelated: Boolean = true

                """Custom list of field names."""
                fields: [String]
            ): [Record]
        }"#,
    );

    assert!(
        !result.has_errors(),
        "Should handle multiple argument descriptions: {:?}",
        result.errors(),
    );
    let (doc, _) = result.into_valid().unwrap();
    let obj = first_object_type(&doc);
    let args = &obj.fields[0].arguments;

    assert_eq!(args.len(), 3);

    assert_eq!(args[0].name, "criteria");
    assert!(
        args[0].description.as_ref().unwrap()
            .contains("filter criteria"),
    );

    assert_eq!(args[1].name, "includeRelated");
    assert!(
        args[1].description.as_ref().unwrap()
            .contains("Include related records"),
    );

    assert_eq!(args[2].name, "fields");
    assert!(
        args[2].description.as_ref().unwrap()
            .contains("field names"),
    );
}

/// Verify that block string descriptions on enum values are
/// parsed correctly.
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn test_block_string_on_enum_values() {
    let result = parse_schema_from_str(
        r#"enum AccessLevel {
            """Read-only access"""
            READ

            """Full write access"""
            WRITE
        }"#,
    );

    assert!(
        !result.has_errors(),
        "Should handle enum value descriptions: {:?}",
        result.errors(),
    );
    let (doc, _) = result.into_valid().unwrap();
    if let Some(ast::schema::Definition::TypeDefinition(
        ast::schema::TypeDefinition::Enum(enum_type),
    )) = doc.definitions.first()
    {
        assert_eq!(enum_type.values.len(), 2);

        assert_eq!(enum_type.values[0].name, "READ");
        assert!(
            enum_type.values[0].description.as_ref().unwrap()
                .contains("Read-only"),
        );

        assert_eq!(enum_type.values[1].name, "WRITE");
        assert!(
            enum_type.values[1].description.as_ref().unwrap()
                .contains("Full write"),
        );
    } else {
        panic!("Expected enum type definition");
    }
}

/// Verify that a directive definition with a block string
/// description and arguments with descriptions and defaults
/// is parsed correctly.
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn test_block_string_on_directive_definition() {
    let result = parse_schema_from_str(
        r#""""
        Marks a field as deprecated with a reason.
        """
        directive @deprecated(
            """The reason for deprecation."""
            reason: String = "No longer supported"
        ) on FIELD_DEFINITION | ENUM_VALUE"#,
    );

    assert!(
        !result.has_errors(),
        "Should handle directive descriptions: {:?}",
        result.errors(),
    );
    let (doc, _) = result.into_valid().unwrap();
    if let Some(ast::schema::Definition::DirectiveDefinition(
        dir,
    )) = doc.definitions.first()
    {
        assert_eq!(dir.name, "deprecated");
        assert!(
            dir.description.as_ref().unwrap()
                .contains("deprecated"),
        );

        assert_eq!(dir.arguments.len(), 1);
        assert_eq!(dir.arguments[0].name, "reason");
        assert!(
            dir.arguments[0].description.as_ref().unwrap()
                .contains("reason for deprecation"),
        );
    } else {
        panic!("Expected directive definition");
    }
}

/// Verify that `RustMacroGraphQLTokenSource` correctly
/// recombines the `-` punct and integer literal into a
/// negative number when used as a default value.
///
/// In `quote!`, `-1` produces two Rust tokens: `Punct('-')`
/// and `Literal(1)`. The token source must recombine these
/// into a single negative `IntValue`.
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn test_negative_default_values_in_input_types() {
    let input = quote! {
        input PaginationInput {
            limit: Int = -1
            threshold: Float = -0.5
            offset: Int = -100
        }
    };

    let result = parse_schema_from_quote(input);

    assert!(
        !result.has_errors(),
        "Should handle negative defaults: {:?}",
        result.errors(),
    );

    let (doc, _) = result.into_valid().unwrap();
    if let Some(ast::schema::Definition::TypeDefinition(
        ast::schema::TypeDefinition::InputObject(input_obj),
    )) = doc.definitions.first()
    {
        assert_eq!(input_obj.fields.len(), 3);

        assert_eq!(input_obj.fields[0].name, "limit");
        let limit_val =
            input_obj.fields[0].default_value.as_ref()
                .expect("limit should have a default value");
        assert!(
            matches!(
                limit_val,
                ast::Value::Int(n)
                    if n.as_i64() == Some(-1),
            ),
            "Expected Int(-1), got: {limit_val:?}",
        );

        assert_eq!(input_obj.fields[1].name, "threshold");
        let thresh_val =
            input_obj.fields[1].default_value.as_ref()
                .expect(
                    "threshold should have a default value",
                );
        assert!(
            matches!(
                thresh_val,
                ast::Value::Float(f)
                    if *f == -0.5,
            ),
            "Expected Float(-0.5), got: {thresh_val:?}",
        );

        assert_eq!(input_obj.fields[2].name, "offset");
        let offset_val =
            input_obj.fields[2].default_value.as_ref()
                .expect("offset should have a default value");
        assert!(
            matches!(
                offset_val,
                ast::Value::Int(n)
                    if n.as_i64() == Some(-100),
            ),
            "Expected Int(-100), got: {offset_val:?}",
        );
    } else {
        panic!("Expected input object type definition");
    }
}

/// Verify that negative defaults in field arguments work
/// through `quote!`, testing the minus-number recombination
/// in argument position.
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn test_negative_default_in_field_arguments() {
    let input = quote! {
        type Query {
            records(
                limit: Int = -1,
                offset: Int = -10
            ): [Record]
        }

        type Record {
            id: ID
        }
    };

    let result = parse_schema_from_quote(input);

    assert!(
        !result.has_errors(),
        "Should handle negative argument defaults: {:?}",
        result.errors(),
    );

    let (doc, _) = result.into_valid().unwrap();
    let obj = first_object_type(&doc);
    let args = &obj.fields[0].arguments;

    assert_eq!(args.len(), 2);

    assert_eq!(args[0].name, "limit");
    assert!(
        matches!(
            args[0].default_value.as_ref().unwrap(),
            ast::Value::Int(n)
                if n.as_i64() == Some(-1),
        ),
        "Expected Int(-1), got: {:?}",
        args[0].default_value,
    );

    assert_eq!(args[1].name, "offset");
    assert!(
        matches!(
            args[1].default_value.as_ref().unwrap(),
            ast::Value::Int(n)
                if n.as_i64() == Some(-10),
        ),
        "Expected Int(-10), got: {:?}",
        args[1].default_value,
    );
}

/// Verify a complex schema combining block string
/// descriptions, embedded quotes, and negative defaults
/// through `TokenStream::from_str()`.
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn test_complex_description_patterns() {
    let result = parse_schema_from_str(
        r#"type Query {
            """
            Search for records.
            Supports operators like \"+\" and \"-\".
            """
            search(
                """The search query string."""
                query: String!

                """Maximum results. -1 means unlimited."""
                limit: Int = -1

                """Whether to include draft versions."""
                includeDrafts: Boolean = false
            ): [Record]
        }

        type Record {
            """The record's canonical identifier."""
            id: ID!
        }"#,
    );

    assert!(
        !result.has_errors(),
        "Should handle complex descriptions: {:?}",
        result.errors(),
    );

    let (doc, _) = result.into_valid().unwrap();
    assert_eq!(doc.definitions.len(), 2);

    // Verify Query type
    let query_obj = first_object_type(&doc);
    assert_eq!(query_obj.name, "Query");

    let search = &query_obj.fields[0];
    assert_eq!(search.name, "search");
    assert!(
        search.description.as_ref().unwrap()
            .contains("Search for records"),
    );

    assert_eq!(search.arguments.len(), 3);
    assert_eq!(search.arguments[0].name, "query");
    assert_eq!(search.arguments[1].name, "limit");
    assert!(
        matches!(
            search.arguments[1].default_value.as_ref().unwrap(),
            ast::Value::Int(n)
                if n.as_i64() == Some(-1),
        ),
    );
    assert_eq!(search.arguments[2].name, "includeDrafts");

    // Verify Record type
    if let Some(ast::schema::Definition::TypeDefinition(
        ast::schema::TypeDefinition::Object(record),
    )) = doc.definitions.get(1)
    {
        assert_eq!(record.name, "Record");
        assert!(
            record.fields[0].description.as_ref().unwrap()
                .contains("canonical identifier"),
        );
    } else {
        panic!("Expected Record object type");
    }
}