-
Hello, I'm trying to write an app that gets graphql things, and then stores them in a "cache" in surrealdb. I also tried to create a transparent enum that has variants of both Id types, but that doesn't work with graphql, cuz scalars can't be enums. minimal repro repo: #[derive(cynic::QueryFragment, Debug, Serialize, PartialEq, Clone)]
pub struct Species {
pub id: cynic::Id,
pub systems: Option<System>,
}
#[derive(cynic::QueryFragment, Debug, Serialize, PartialEq, Clone)]
pub struct System {
pub id: cynic::Id,
pub username: Option<String>,
pub ctime: Option<f64>,
pub tags: Option<Vec<Option<f64>>>,
}
#[tokio::test]
async fn direct_sharing() {
let gql_species = get_gql_species().await;
let db = setup_db().await;
for species in gql_species.clone() {
let _: Option<Species> = db
.upsert(("species", species.id.inner().to_owned()))
.content(species)
.await
.unwrap();
}
let sql_species: Vec<Species> = db.select("species").await.unwrap();
assert_eq!(gql_species, sql_species);
} This produces the following error: thread 'tests::direct_sharing' panicked at src/main.rs:59:18:
called `Result::unwrap()` on an `Err` value: Db(Serialization("failed to deserialize; expected a string, found $surrealdb::private::sql::Thing { tb: \"species\
", id: Id::String(\"U3BlY2llczooNDE2MjA2LCA3MSwgNCk=\") }")) I have tried also through Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
surrealdb/surrealdb#4921 (comment), https://cynic-rs.dev/derives/query-fragments#field-attributes #[tokio::test]
async fn through_serde_json_value_sharing() {
let gql_species = get_gql_species().await;
let db = setup_db().await;
for species in gql_species.clone() {
let x = serde_json::to_value(species).unwrap();
db.upsert(Resource::from("species"))
.content(x)
.await
.unwrap();
}
let sql_species: surrealdb::Value = db
.query("SELECT * FROM species")
.await
.unwrap()
.take(0)
.unwrap();
let sql_species = sql_species.into_inner().into_json();
let sql_species = sql_species.as_array().unwrap();
dbg!(&sql_species);
let mut real_sql_species: Vec<Species> = vec![];
for val in sql_species {
dbg!(val);
real_sql_species.push(serde_json::from_value(val.to_owned()).unwrap());
}
dbg!(&real_sql_species); //works! After turning the response into json, I kept getting called `Result::unwrap()` on an `Err` value: Error("missing field `tags`", line:
0, column: 0) , then I discovered that flagging the tags field like this helped: pub tags: Option<Vec<Option<f64>>>,
// as
#[cynic(flatten)]
pub tags: Vec<f64>, and now I share types between both! Last issue is that the surrealdb Id keeps the table name in the Id field, but that can be stripped with some string manipulation. assertion `left == right` failed
left: [Species { id: Id("species:⟨U3BlY2llczooNDE2MjA2LCA3LCAzNik=⟩"), systems: ... }]
right: [Species { id: Id("U3BlY2llczooNDE2MjA2LCA3MSwgNCk="), systems: ...}] Yipee |
Beta Was this translation helpful? Give feedback.
surrealdb/surrealdb#4921 (comment), https://cynic-rs.dev/derives/query-fragments#field-attributes
These ended up being invaluable to tackling this problem.