From 2288138d5b20f2dd19d9c9188f6a0410e5e9f574 Mon Sep 17 00:00:00 2001 From: Samuel Hurel Date: Thu, 7 May 2020 08:42:34 +0200 Subject: [PATCH] Add ObjectId to ID type conversion --- src/scalars/id.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/scalars/id.rs b/src/scalars/id.rs index a2cf55fe..02802645 100644 --- a/src/scalars/id.rs +++ b/src/scalars/id.rs @@ -1,5 +1,6 @@ use crate::{Result, ScalarType, Value}; use async_graphql_derive::Scalar; +use bson::oid::{self, ObjectId}; use std::convert::TryInto; use std::num::ParseIntError; use std::ops::{Deref, DerefMut}; @@ -77,6 +78,20 @@ impl TryInto for ID { } } +impl From for ID { + fn from(object_id: ObjectId) -> ID { + ID(object_id.to_hex()) + } +} + +impl TryInto for ID { + type Error = oid::Error; + + fn try_into(self) -> std::result::Result { + ObjectId::with_string(&self.0) + } +} + impl PartialEq<&str> for ID { fn eq(&self, other: &&str) -> bool { self.0.as_str() == *other