Query result

#[query_result]{
    #[field]
    #[backlink]
}

#[query_result] attribute marks a struct as a result of a edgeDB query.

When decorating a struct with #[query_result] attribute, the resulting struct is decorated with edgedb_derive::Queryable macro derive.

For this reason, a struct decorated #[query_result] ⚠️ must have a field id: uuid::Uuid ⚠️

Usage

This is an example of usage of the field attribute 👇

     #[query_reqult]
     pub struct UserResult {
        pub id: uuid::Uuid,
        #[field(column_name="pseudo", wrapper_fn="str_upper", default_value="john")]
        pub login: String,
     }

    fn main() {
        let shape = UserResult::shape();
        assert_eq!(shape, "{id, login := (select <str>str_upper(.pseudo)) ?? (select <str>'john')}")
    }

And then, an example of usage of backlink attribute 👇

Consider the following edgeDB schema :

    module cinema {
        
        type Movie {
            required property title -> str;
            multi link actors -> Actor;
        }
        
        type Actor {
            required property name -> str;
        }
    }

To query the Actor table and get the actor's name and all the movies he's been in, we need to write the following query :

     select Actor
        {
            name,
            movies := (
                select Actor.<actors[is Movie] {
                    title
                }
            )
        }

Using #[query_result] attribute and its backlink attribute we can do things like this 👇

    #[query_result]
    pub struct MovieResult {
        pub id: uuid::Uuid,
        pub title: String,
    }
    
    #[query_result]
    pub struct Actor {
        pub id: uuid::Uuid,
        pub name: String,
        #[back_link(
            module="cinema",
            source_table="Actor",
            target_table="Movie",
            target_column="actors"
        )]
        pub movies: Vec<MovieResult>,
    }

    fn main() {
        let rm_spaces = |s: &str| s.split_whitespace().collect::<String>();
        
        let shape = Actor::shape();
        let expected_shape = r#"
            {
                id,
                name, 
                movies := (
                    select cinema::Actor.<actors[is cinema::Movie] {
                       title
                    }
                )
            }
        "#;
        assert_eq!(rm_spaces(shape.as_str()), rm_spaces(expected_shape));
    }