Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Derive QueryArgs for structs to provide named query arguments #261

Open
AdrienPensart opened this issue Jun 21, 2023 · 8 comments
Open

Derive QueryArgs for structs to provide named query arguments #261

AdrienPensart opened this issue Jun 21, 2023 · 8 comments

Comments

@AdrienPensart
Copy link

Hello!

Would it be possible to execute queries like this :

#[derive(Queryable)]
struct MusicOutput {
    pub id: Uuid,
    pub name: String
}

let parameters = HashMap::from([
    ("artist", "John 5"),
]);
let music_output: Result<MusicOutput, _> = conn.query_required_single(
"select (
        insert Artist {
        name := <str>$artist
   } unless conflict on .name else (select Artist)
) {id, name}", &parameters).await;
@tailhook
Copy link
Contributor

The problem with using HashMap is that arguments can be of different types. So dynamic typing should be used. I.e. edgedb_protocol::value::Value type. Unforutately, we don't have a nice constructor for that at least yet.

The plan in, however, to make a derive:

#[derive(QueryArgs)]
struct Artist {
  name: String,
}

But this is still on our todo list. For now, tuples are what is the easiest to use as query arguments.

@hongquan
Copy link

hongquan commented Jul 7, 2023

Before edgedb-rust offers a convenient way to construct named parameters from your struct, you can use named parameters with Client.query by:

  • Construct a edgedb_protocol::value::Value::Object from your map of Map<String, Value>.
  • Pass that Value::Object to Client.query_xxx

To construct edgedb_protocol::value::Value::Object from a map, you can reference https://github.com/edgedb/edgedb-rust/blob/releases/edgedb-protocol/v0.6.0/edgedb-protocol/src/value.rs#L100. Note that, the example is to build SparseObject, we just learn it to build Value:Object.

You can take a look at my examples:

https://github.com/hongquan/QuanWeb/blob/2bcb09b4e62dd7742e8ebb9fee809a7e9970111c/src/stores/blog.rs#L123-L157

https://github.com/hongquan/QuanWeb/blob/2bcb09b4e62dd7742e8ebb9fee809a7e9970111c/src/api/structs.rs#L168-L206

@AlexDaniel
Copy link

Also: https://quan.hoabinh.vn/post/2023/8/querying-edgedb-with-name-parameters-in-rust

@MrFoxPro
Copy link
Contributor

#304 should solve this

@KevinMGranger

This comment was marked as off-topic.

@hongquan

This comment was marked as off-topic.

@aljazerzen

This comment was marked as off-topic.

@aljazerzen
Copy link
Contributor

For anyone finding this thread in the future, named_args! is currently recommended way to pass named params into client.query_xxx():

let query = "SELECT (<str>$my_str, <int64>$my_int)";
let args = edgedb_protocol::named_args! {
    "my_str" => "Hello world!".to_string(),
    "my_int" => Value::Int64(42),
};
let res = client.query(query, &args).await.unwrap();

This issue will remain open to track derive macro for QueryArgs on structs, as outlined by this comment.

@aljazerzen aljazerzen changed the title Named parameters in queries Derive QueryArgs for structs to provide named query arguments Sep 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants