I am using both mongodb and juniper in rust. Both use on the bson crate.
I can either import ObjectId from mongodb, like so:
use mongodb::{bson::{doc, oid::ObjectId}, sync::Client};
use juniper::GraphQLObject;
use serde::{Serialize, Deserialize};
In which case Juniper complains:
error[E0277]: the trait bound `mongodb::bson::oid::ObjectId: GraphQLValueAsync<__S>` is not satisfied
--> src/main.rs:6:10
|
6 | #[derive(GraphQLObject)]
| ^^^^^^^^^^^^^ the trait `GraphQLValueAsync<__S>` is not implemented for `mongodb::bson::oid::ObjectId`
|
= note: required because of the requirements on the impl of `GraphQLValueAsync<__S>` for `&mongodb::bson::oid::ObjectId`
= note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info)
or from bson directly, like so:
use mongodb::{bson::doc, sync::Client};
use bson::oid::ObjectId;
use juniper::GraphQLObject;
use serde::{Serialize, Deserialize};
in which case mongodb complains:
error[E0277]: the trait bound `mongodb::bson::Bson: From<bson::oid::ObjectId>` is not satisfied
--> src/main.rs:20:42
|
20 | let commune = collection.find_one(Some(doc! {"_id": oid}), None).expect("result").expect("option");
| ^^^^^^^^^^^^^^^^^ the trait `From<bson::oid::ObjectId>` is not implemented for `mongodb::bson::Bson`
|
= help: the following implementations were found:
<mongodb::bson::Bson as From<&T>>
<mongodb::bson::Bson as From<&[T]>>
<mongodb::bson::Bson as From<&str>>
<mongodb::bson::Bson as From<Vec<T>>>
and 20 others
Here is a minimal example (add the lines of code above in turn).
#[derive(Debug, Serialize, Deserialize)]
#[derive(GraphQLObject)]
pub struct User {
pub _id: ObjectId,
}
fn main() {
let uri = "URI";
let client = Client::with_uri_str(uri).expect("unable to connect");
let db = client.database("Database");
let oid = ObjectId::new();
let collection = db.collection::<User>("users");
let commune = collection.find_one(Some(doc! {"_id": oid}), None).expect("result").expect("option");
println!("{:?}", commune)
}
Based on the error I am guessing that I need to impl std::convert::From, but I feel like I am doing something wrong here. What is the better way of resolving this?