Skip to content

How to make the Record id as a string instead of Thing #2686

Answered by skriems
mimib00 asked this question in Q&A
Discussion options

You must be logged in to vote

I needed to do that for an async-graphql server, knowing that this obviously has downsides, in the sense that I'm limiting myself to use string id's only... anyway, for now I just needed that.

use serde::{Deserialize, Deserializer};
use surrealdb::sql::Thing;

fn thing_to_string<'de, D>(deserializer: D) -> Result<Option<String>, D::Error>
where
    D: Deserializer<'de>,
{
    let t = Thing::deserialize(deserializer)?;
    Ok(Some(t.to_raw()))
}


#[derive(Deserialize, Serialize)]
pub struct Foo {
    #[serde(deserialize_with = "thing_to_string")]
    id: Option<String>,
    ...
}

Hope that helps

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@mimib00
Comment options

@4F2E4A2E
Comment options

Answer selected by mimib00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants