edgedb / edgedb-rust

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

Inserting JSON collections appears to not work with rust client #243

Closed windowsdeveloperwannabe closed 1 year ago

windowsdeveloperwannabe commented 1 year ago

Tried to insert an array of things into edgedb in the simplest way I could think of (so JSON). Client returns error

Error: DescriptorMismatch: 
EdgeDB returned unexpected type BaseScalar(BaseScalarTypeDescriptor { id: BaseScalar(std::json) })
Client expected std::str

but I'm not sure what that means. Schema and code is:

type Tag {
    required property text -> str {
        constraint exclusive;
    }
}
use anyhow::Result;
use edgedb_derive::Queryable;
use uuid::Uuid;

#[tokio::main]
async fn main() -> Result<()> {
    let conn = edgedb_tokio::create_client().await?;
    #[derive(Debug, Queryable)]
    struct Id {
        id: Uuid,
    }
    let json = serde_json::to_string(&vec!["tag1", "tag2", "tag3"]).unwrap();
    let result: Vec<Id> = conn
        .query(
            r#"
            for tag in array_unpack(<array<str>><json>$0)
            union (
                insert Tag {
                    text := tag
                }
                unless conflict on .text
                else (select Tag)
            )"#,
            &(&json,),
        )
        .await?;
    println!("{result:?}");
    Ok(())
}

In the REPL the query works fine with input ["tag1", "tag2", "tag3"] and returns

[
  {
    "id": "a314fe5e-f678-11ed-a9d2-3ffb47775395"
  },
  {
    "id": "864883c2-f678-11ed-a9d2-5bb2894ae519"
  },
  {
    "id": "86488a70-f678-11ed-a9d2-3f67699c7691"
  }
]
tailhook commented 1 year ago

You have to wrap your json data into edgedb_protocol::model::Json::new_unchecked(x) (sorry for having only unsafe API for now, we have to figure out safe API here).

Otherwise, client tries to send the value as a string. You can also do <array<str>><json><str>$0.