graphql-rust / juniper

GraphQL server library for Rust
Other
5.72k stars 425 forks source link

Preserve input body when attribute macro expansion fails (#1244) #1245

Closed tyranron closed 9 months ago

tyranron commented 9 months ago

Resolves #1244

Synopsis

See https://github.com/graphql-rust/juniper/issues/1244#issue-2105985314:

Describe the bug

#[graphql_object] attribute macro prevents syntax errors from being shown:

https://github.com/graphql-rust/juniper/blob/8a69e14c8bbbbd1da8bdefe3e30c60ff342ed863/juniper_codegen/src/graphql_object/attr.rs#L35

To Reproduce Steps to reproduce the behavior:

1. Add a valid `struct`;

2. Impl fields resolvers with `#[graphql_object]`;

3. Broke syntax inside method impl.
struct MyObject {
  my_field: i32,
}

#[graphql_object] // ERROR: #[graphql_object] attribute is applicable to non-trait `impl` blocks only
impl MyObject {
    fn my_field(&self) -> i32 {
        self.self.my_field // actual syntax error is not reported.
    }
}

Expected behavior

A syntax error will be shown.

Additional context

The problem is happening because attribute macro ignores errors of impl block parsing:

https://github.com/graphql-rust/juniper/blob/8a69e14c8bbbbd1da8bdefe3e30c60ff342ed863/juniper_codegen/src/graphql_object/attr.rs#L22

Solution

Preserve input body when proc_macro_attribute expansion fails.

Checklist