Skip to content

Commit 389fd5c

Browse files
authored
Add bson crate's ObjectId to juniper foreign scalar type integrations (#517)
1 parent 4ccb129 commit 389fd5c

File tree

7 files changed

+61
-0
lines changed

7 files changed

+61
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ your Schemas automatically.
6767
- [uuid][uuid]
6868
- [url][url]
6969
- [chrono][chrono]
70+
- [bson][bson]
7071

7172
### Web Frameworks
7273

@@ -105,4 +106,5 @@ Juniper has not reached 1.0 yet, thus some API instability should be expected.
105106
[uuid]: https://crates.io/crates/uuid
106107
[url]: https://crates.io/crates/url
107108
[chrono]: https://crates.io/crates/chrono
109+
[bson]: https://crates.io/crates/bson
108110
[juniper-from-schema]: https://github.com/davidpdrsn/juniper-from-schema

docs/book/content/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ your Schemas automatically.
4141
- [uuid][uuid]
4242
- [url][url]
4343
- [chrono][chrono]
44+
- [bson][bson]
4445

4546
### Web Frameworks
4647

@@ -72,3 +73,4 @@ Juniper has not reached 1.0 yet, thus some API instability should be expected.
7273
[uuid]: https://crates.io/crates/uuid
7374
[url]: https://crates.io/crates/url
7475
[chrono]: https://crates.io/crates/chrono
76+
[bson]: https://crates.io/crates/bson

docs/book/content/types/scalars.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ crates. They are enabled via features that are on by default.
3535
* uuid::Uuid
3636
* chrono::DateTime
3737
* url::Url
38+
* bson::oid::ObjectId
3839

3940
## newtype pattern
4041

juniper/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ path = "benches/bench.rs"
2727
async = ["juniper_codegen/async", "futures"]
2828
expose-test-schema = []
2929
default = [
30+
"bson",
3031
"chrono",
3132
"url",
3233
"uuid",
@@ -35,6 +36,7 @@ default = [
3536
[dependencies]
3637
juniper_codegen = { version = "0.14.2", path = "../juniper_codegen" }
3738

39+
bson = { version = "0.14.0", optional = true }
3840
chrono = { version = "0.4.0", optional = true }
3941
fnv = "1.0.3"
4042
futures = { version = "0.3.1", optional = true }

juniper/src/integrations/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,7 @@ pub mod url;
1313
#[cfg(feature = "uuid")]
1414
/// GraphQL support for [uuid](https://doc.rust-lang.org/uuid/uuid/struct.Uuid.html) types.
1515
pub mod uuid;
16+
17+
#[cfg(feature = "bson")]
18+
/// GraphQL support for [bson](https://github.com/mongodb/bson-rust) types.
19+
pub mod objectid;

juniper/src/integrations/objectid.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
use bson::oid::ObjectId;
2+
3+
use crate::{
4+
parser::{ParseError, ScalarToken, Token},
5+
value::ParseScalarResult,
6+
Value,
7+
};
8+
9+
graphql_scalar!(ObjectId where Scalar = <S> {
10+
description: "ObjectId"
11+
12+
resolve(&self) -> Value {
13+
Value::scalar(self.to_hex())
14+
}
15+
16+
from_input_value(v: &InputValue) -> Option<ObjectId> {
17+
v.as_string_value()
18+
.and_then(|s| ObjectId::with_string(s).ok())
19+
}
20+
21+
from_str<'a>(value: ScalarToken<'a>) -> ParseScalarResult<'a, S> {
22+
if let ScalarToken::String(value) = value {
23+
Ok(S::from(value.to_owned()))
24+
} else {
25+
Err(ParseError::UnexpectedToken(Token::Scalar(value)))
26+
}
27+
}
28+
});
29+
30+
#[cfg(test)]
31+
mod test {
32+
use crate::{value::DefaultScalarValue, InputValue};
33+
use bson::oid::ObjectId;
34+
35+
#[test]
36+
fn objectid_from_input_value() {
37+
let raw = "53e37d08776f724e42000000";
38+
let input: InputValue<DefaultScalarValue> = InputValue::scalar(raw.to_string());
39+
40+
let parsed: ObjectId = crate::FromInputValue::from_input_value(&input).unwrap();
41+
let id = ObjectId::with_string(raw).unwrap();
42+
43+
assert_eq!(parsed, id);
44+
}
45+
}

juniper/src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ your Schemas automatically.
5959
* [uuid][uuid]
6060
* [url][url]
6161
* [chrono][chrono]
62+
* [bson][bson]
6263
6364
### Web Frameworks
6465
@@ -86,6 +87,7 @@ Juniper has not reached 1.0 yet, thus some API instability should be expected.
8687
[uuid]: https://crates.io/crates/uuid
8788
[url]: https://crates.io/crates/url
8889
[chrono]: https://crates.io/crates/chrono
90+
[bson]: https://crates.io/crates/bson
8991
9092
*/
9193
#![doc(html_root_url = "https://docs.rs/juniper/0.14.2")]
@@ -106,6 +108,9 @@ extern crate url;
106108
#[cfg(any(test, feature = "uuid"))]
107109
extern crate uuid;
108110

111+
#[cfg(any(test, feature = "bson"))]
112+
extern crate bson;
113+
109114
// Depend on juniper_codegen and re-export everything in it.
110115
// This allows users to just depend on juniper and get the derive
111116
// functionality automatically.

0 commit comments

Comments
 (0)