Add bson crate's ObjectId to juniper foreign scalar type integrations (#517)
This commit is contained in:
parent
4ccb129fa2
commit
389fd5ca19
7 changed files with 61 additions and 0 deletions
|
@ -67,6 +67,7 @@ your Schemas automatically.
|
|||
- [uuid][uuid]
|
||||
- [url][url]
|
||||
- [chrono][chrono]
|
||||
- [bson][bson]
|
||||
|
||||
### Web Frameworks
|
||||
|
||||
|
@ -105,4 +106,5 @@ Juniper has not reached 1.0 yet, thus some API instability should be expected.
|
|||
[uuid]: https://crates.io/crates/uuid
|
||||
[url]: https://crates.io/crates/url
|
||||
[chrono]: https://crates.io/crates/chrono
|
||||
[bson]: https://crates.io/crates/bson
|
||||
[juniper-from-schema]: https://github.com/davidpdrsn/juniper-from-schema
|
||||
|
|
|
@ -41,6 +41,7 @@ your Schemas automatically.
|
|||
- [uuid][uuid]
|
||||
- [url][url]
|
||||
- [chrono][chrono]
|
||||
- [bson][bson]
|
||||
|
||||
### Web Frameworks
|
||||
|
||||
|
@ -72,3 +73,4 @@ Juniper has not reached 1.0 yet, thus some API instability should be expected.
|
|||
[uuid]: https://crates.io/crates/uuid
|
||||
[url]: https://crates.io/crates/url
|
||||
[chrono]: https://crates.io/crates/chrono
|
||||
[bson]: https://crates.io/crates/bson
|
||||
|
|
|
@ -35,6 +35,7 @@ crates. They are enabled via features that are on by default.
|
|||
* uuid::Uuid
|
||||
* chrono::DateTime
|
||||
* url::Url
|
||||
* bson::oid::ObjectId
|
||||
|
||||
## newtype pattern
|
||||
|
||||
|
|
|
@ -27,6 +27,7 @@ path = "benches/bench.rs"
|
|||
async = ["juniper_codegen/async", "futures"]
|
||||
expose-test-schema = []
|
||||
default = [
|
||||
"bson",
|
||||
"chrono",
|
||||
"url",
|
||||
"uuid",
|
||||
|
@ -35,6 +36,7 @@ default = [
|
|||
[dependencies]
|
||||
juniper_codegen = { version = "0.14.2", path = "../juniper_codegen" }
|
||||
|
||||
bson = { version = "0.14.0", optional = true }
|
||||
chrono = { version = "0.4.0", optional = true }
|
||||
fnv = "1.0.3"
|
||||
futures = { version = "0.3.1", optional = true }
|
||||
|
|
|
@ -13,3 +13,7 @@ pub mod url;
|
|||
#[cfg(feature = "uuid")]
|
||||
/// GraphQL support for [uuid](https://doc.rust-lang.org/uuid/uuid/struct.Uuid.html) types.
|
||||
pub mod uuid;
|
||||
|
||||
#[cfg(feature = "bson")]
|
||||
/// GraphQL support for [bson](https://github.com/mongodb/bson-rust) types.
|
||||
pub mod objectid;
|
||||
|
|
45
juniper/src/integrations/objectid.rs
Normal file
45
juniper/src/integrations/objectid.rs
Normal file
|
@ -0,0 +1,45 @@
|
|||
use bson::oid::ObjectId;
|
||||
|
||||
use crate::{
|
||||
parser::{ParseError, ScalarToken, Token},
|
||||
value::ParseScalarResult,
|
||||
Value,
|
||||
};
|
||||
|
||||
graphql_scalar!(ObjectId where Scalar = <S> {
|
||||
description: "ObjectId"
|
||||
|
||||
resolve(&self) -> Value {
|
||||
Value::scalar(self.to_hex())
|
||||
}
|
||||
|
||||
from_input_value(v: &InputValue) -> Option<ObjectId> {
|
||||
v.as_string_value()
|
||||
.and_then(|s| ObjectId::with_string(s).ok())
|
||||
}
|
||||
|
||||
from_str<'a>(value: ScalarToken<'a>) -> ParseScalarResult<'a, S> {
|
||||
if let ScalarToken::String(value) = value {
|
||||
Ok(S::from(value.to_owned()))
|
||||
} else {
|
||||
Err(ParseError::UnexpectedToken(Token::Scalar(value)))
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use crate::{value::DefaultScalarValue, InputValue};
|
||||
use bson::oid::ObjectId;
|
||||
|
||||
#[test]
|
||||
fn objectid_from_input_value() {
|
||||
let raw = "53e37d08776f724e42000000";
|
||||
let input: InputValue<DefaultScalarValue> = InputValue::scalar(raw.to_string());
|
||||
|
||||
let parsed: ObjectId = crate::FromInputValue::from_input_value(&input).unwrap();
|
||||
let id = ObjectId::with_string(raw).unwrap();
|
||||
|
||||
assert_eq!(parsed, id);
|
||||
}
|
||||
}
|
|
@ -59,6 +59,7 @@ your Schemas automatically.
|
|||
* [uuid][uuid]
|
||||
* [url][url]
|
||||
* [chrono][chrono]
|
||||
* [bson][bson]
|
||||
|
||||
### Web Frameworks
|
||||
|
||||
|
@ -86,6 +87,7 @@ Juniper has not reached 1.0 yet, thus some API instability should be expected.
|
|||
[uuid]: https://crates.io/crates/uuid
|
||||
[url]: https://crates.io/crates/url
|
||||
[chrono]: https://crates.io/crates/chrono
|
||||
[bson]: https://crates.io/crates/bson
|
||||
|
||||
*/
|
||||
#![doc(html_root_url = "https://docs.rs/juniper/0.14.2")]
|
||||
|
@ -106,6 +108,9 @@ extern crate url;
|
|||
#[cfg(any(test, feature = "uuid"))]
|
||||
extern crate uuid;
|
||||
|
||||
#[cfg(any(test, feature = "bson"))]
|
||||
extern crate bson;
|
||||
|
||||
// Depend on juniper_codegen and re-export everything in it.
|
||||
// This allows users to just depend on juniper and get the derive
|
||||
// functionality automatically.
|
||||
|
|
Loading…
Reference in a new issue