edgedb / edgedb-rust

The official Rust binding for EdgeDB
https://edgedb.com
Apache License 2.0
209 stars 26 forks source link

Impl check descriptor for enum #288

Closed Dhghomon closed 7 months ago

Dhghomon commented 7 months ago

Was surprised today to see that a simple query like the following doesn't work:

Schema: module default { scalar type Country extending enum<Full, ReadOnly, None>; }

Rust:

fn main() {
let client = edgedb_tokio::create_client()
        .await
        .unwrap();
    let val: Value = client
        .query_required_single(
            "select <Country>$0",
            &(Value::Enum(EnumValue::from("None")),),
        )
        .await
        .unwrap();
}

The error is that it expected an enum, and got an...Enumeration.

called `Result::unwrap()` on an `Err` value: Error(Inner { code: 4278386176, messages: ["\nEdgeDB returned unexpected type Enumeration(EnumerationTypeDescriptor { id: 16e1e80c-94fe-11ee-96e2-dd6c21ea77c9, members: [\"Full\", \"ReadOnly\", \"None\"] })\nClient expected enum"], error: None, headers: {}, fields: {("descriptor", TypeId { t: 116367074156074466779343831034874852404 }): Any { .. }, ("capabilities", TypeId { t: 264000494568981768888535343960226937934 }): Any { .. }} })

Turns out that it's just that check_descriptor() simply isn't complete yet. A check to make sure that the value is found inside the members (which holds the possible enum values) should do the trick while the cast in the query itself (in this case a <Country>).

After that the error when an incorrect enum value was passed in was still the above, so changed it to output an InvalidReferenceError (that's what the REPL gives in the same case) with some context so now it looks like this:

called `Result::unwrap()` on an `Err` value: Error(Inner { code: 67305472, messages: ["Expected one of: 'Full', 'ReadOnly', 'None', while enum value 'RRRRRRRRRRRRRRRRRReadOnly' was provided"], error: None, headers: {}, fields: {("descriptor", TypeId { t: 254184924864600833047165177448722979326 }): Any { .. }, ("capabilities", TypeId { t: 267257778208590603416093306289546576982 }): Any { .. }} })