Upgrade 'bson' crate to 2.0 version (, )

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Kai Ren <tyranron@gmail.com>
Co-authored-by: Grant Perry <grant42perry@gmail.com>
This commit is contained in:
dependabot[bot] 2021-09-08 19:42:07 +03:00 committed by GitHub
parent 4c76e93cfb
commit 6bb34b9b64
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 19 deletions
juniper

View file

@ -4,6 +4,7 @@
- `#[graphql_object]` and `#[graphql_subscription]` macros expansion now preserves defined `impl` blocks "as is" and reuses defined methods in opaque way. ([#971](https://github.com/graphql-rust/juniper/pull/971) - `#[graphql_object]` and `#[graphql_subscription]` macros expansion now preserves defined `impl` blocks "as is" and reuses defined methods in opaque way. ([#971](https://github.com/graphql-rust/juniper/pull/971)
- `rename = "<policy>"` attribute's argument renamed to `rename_all = "<policy>"`. ([#971](https://github.com/graphql-rust/juniper/pull/971) - `rename = "<policy>"` attribute's argument renamed to `rename_all = "<policy>"`. ([#971](https://github.com/graphql-rust/juniper/pull/971)
- Upgrade `bson` feature to [2.0 version of its crate](https://github.com/mongodb/bson-rust/releases/tag/v2.0.0). ([#979](https://github.com/graphql-rust/juniper/pull/979)
## Features ## Features

View file

@ -36,7 +36,7 @@ juniper_codegen = { version = "0.15.7", path = "../juniper_codegen" }
anyhow = { version = "1.0.32", optional = true, default-features = false } anyhow = { version = "1.0.32", optional = true, default-features = false }
async-trait = "0.1.39" async-trait = "0.1.39"
bson = { version = "1.0", optional = true } bson = { version = "2.0", features = ["chrono-0_4"], optional = true }
chrono = { version = "0.4", default-features = false, optional = true } chrono = { version = "0.4", default-features = false, optional = true }
chrono-tz = { version = "0.5", default-features = false, optional = true } chrono-tz = { version = "0.5", default-features = false, optional = true }
fnv = "1.0.3" fnv = "1.0.3"
@ -51,6 +51,9 @@ static_assertions = "1.1"
url = { version = "2.0", optional = true } url = { version = "2.0", optional = true }
uuid = { version = "0.8", default-features = false, optional = true } uuid = { version = "0.8", default-features = false, optional = true }
[target.'cfg(target_arch = "wasm32")'.dependencies]
getrandom = { version = "0.2", features = ["js"] }
[dev-dependencies] [dev-dependencies]
bencher = "0.1.2" bencher = "0.1.2"
pretty_assertions = "0.7.1" pretty_assertions = "0.7.1"

View file

@ -4,12 +4,13 @@ use bson::{oid::ObjectId, DateTime as UtcDateTime};
use chrono::prelude::*; use chrono::prelude::*;
use crate::{ use crate::{
graphql_scalar,
parser::{ParseError, ScalarToken, Token}, parser::{ParseError, ScalarToken, Token},
value::ParseScalarResult, value::ParseScalarResult,
Value, Value,
}; };
#[crate::graphql_scalar(description = "ObjectId")] #[graphql_scalar(description = "ObjectId")]
impl<S> GraphQLScalar for ObjectId impl<S> GraphQLScalar for ObjectId
where where
S: ScalarValue, S: ScalarValue,
@ -19,37 +20,36 @@ where
} }
fn from_input_value(v: &InputValue) -> Option<ObjectId> { fn from_input_value(v: &InputValue) -> Option<ObjectId> {
v.as_string_value() v.as_string_value().and_then(|s| Self::parse_str(s).ok())
.and_then(|s| ObjectId::with_string(s).ok())
} }
fn from_str<'a>(value: ScalarToken<'a>) -> ParseScalarResult<'a, S> { fn from_str<'a>(value: ScalarToken<'a>) -> ParseScalarResult<'a, S> {
if let ScalarToken::String(value) = value { if let ScalarToken::String(val) = value {
Ok(S::from(value.to_owned())) Ok(S::from(val.to_owned()))
} else { } else {
Err(ParseError::UnexpectedToken(Token::Scalar(value))) Err(ParseError::UnexpectedToken(Token::Scalar(value)))
} }
} }
} }
#[crate::graphql_scalar(description = "UtcDateTime")] #[graphql_scalar(description = "UtcDateTime")]
impl<S> GraphQLScalar for UtcDateTime impl<S> GraphQLScalar for UtcDateTime
where where
S: ScalarValue, S: ScalarValue,
{ {
fn resolve(&self) -> Value { fn resolve(&self) -> Value {
Value::scalar((*self).to_rfc3339()) Value::scalar((*self).to_chrono().to_rfc3339())
} }
fn from_input_value(v: &InputValue) -> Option<UtcDateTime> { fn from_input_value(v: &InputValue) -> Option<UtcDateTime> {
v.as_string_value() v.as_string_value()
.and_then(|s| (s.parse::<DateTime<Utc>>().ok())) .and_then(|s| (s.parse::<DateTime<Utc>>().ok()))
.map(UtcDateTime) .map(Self::from_chrono)
} }
fn from_str<'a>(value: ScalarToken<'a>) -> ParseScalarResult<'a, S> { fn from_str<'a>(value: ScalarToken<'a>) -> ParseScalarResult<'a, S> {
if let ScalarToken::String(value) = value { if let ScalarToken::String(val) = value {
Ok(S::from(value.to_owned())) Ok(S::from(val.to_owned()))
} else { } else {
Err(ParseError::UnexpectedToken(Token::Scalar(value))) Err(ParseError::UnexpectedToken(Token::Scalar(value)))
} }
@ -58,17 +58,18 @@ where
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use crate::{value::DefaultScalarValue, InputValue};
use bson::{oid::ObjectId, DateTime as UtcDateTime}; use bson::{oid::ObjectId, DateTime as UtcDateTime};
use chrono::prelude::*; use chrono::{DateTime, Utc};
use crate::{value::DefaultScalarValue, FromInputValue, InputValue};
#[test] #[test]
fn objectid_from_input_value() { fn objectid_from_input_value() {
let raw = "53e37d08776f724e42000000"; let raw = "53e37d08776f724e42000000";
let input: InputValue<DefaultScalarValue> = InputValue::scalar(raw.to_string()); let input = InputValue::<DefaultScalarValue>::scalar(raw.to_string());
let parsed: ObjectId = crate::FromInputValue::from_input_value(&input).unwrap(); let parsed: ObjectId = FromInputValue::from_input_value(&input).unwrap();
let id = ObjectId::with_string(raw).unwrap(); let id = ObjectId::parse_str(raw).unwrap();
assert_eq!(parsed, id); assert_eq!(parsed, id);
} }
@ -76,10 +77,10 @@ mod test {
#[test] #[test]
fn utcdatetime_from_input_value() { fn utcdatetime_from_input_value() {
let raw = "2020-03-23T17:38:32.446+00:00"; let raw = "2020-03-23T17:38:32.446+00:00";
let input: InputValue<DefaultScalarValue> = InputValue::scalar(raw.to_string()); let input = InputValue::<DefaultScalarValue>::scalar(raw.to_string());
let parsed: UtcDateTime = crate::FromInputValue::from_input_value(&input).unwrap(); let parsed: UtcDateTime = FromInputValue::from_input_value(&input).unwrap();
let date_time = UtcDateTime( let date_time = UtcDateTime::from_chrono(
DateTime::parse_from_rfc3339(raw) DateTime::parse_from_rfc3339(raw)
.unwrap() .unwrap()
.with_timezone(&Utc), .with_timezone(&Utc),