Skip to content

Commit 3e4d4ea

Browse files
Support time crate types as GraphQL scalars (#1006)
Co-authored-by: Kai Ren <[email protected]>
1 parent 265d4c5 commit 3e4d4ea

File tree

8 files changed

+742
-14
lines changed

8 files changed

+742
-14
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ your Schemas automatically.
7373
- [url][url]
7474
- [chrono][chrono]
7575
- [chrono-tz][chrono-tz]
76+
- [time][time]
7677
- [bson][bson]
7778

7879
### Web Frameworks
@@ -118,5 +119,6 @@ Juniper has not reached 1.0 yet, thus some API instability should be expected.
118119
[url]: https://crates.io/crates/url
119120
[chrono]: https://crates.io/crates/chrono
120121
[chrono-tz]: https://crates.io/crates/chrono-tz
122+
[time]: https://crates.io/crates/time
121123
[bson]: https://crates.io/crates/bson
122124
[juniper-from-schema]: https://github.com/davidpdrsn/juniper-from-schema

docs/book/content/types/scalars.md

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ but this often requires coordination with the client library intended to consume
66
the API you're building.
77

88
Since any value going over the wire is eventually transformed into JSON, you're
9-
also limited in the data types you can use.
9+
also limited in the data types you can use.
1010

11-
There are two ways to define custom scalars.
11+
There are two ways to define custom scalars.
1212
* For simple scalars that just wrap a primitive type, you can use the newtype pattern with
13-
a custom derive.
13+
a custom derive.
1414
* For more advanced use cases with custom validation, you can use
1515
the `graphql_scalar` proc macro.
1616

@@ -36,12 +36,13 @@ crates. They are enabled via features that are on by default.
3636

3737
* uuid::Uuid
3838
* chrono::DateTime
39+
* time::{Date, OffsetDateTime, PrimitiveDateTime, Time, UtcOffset}
3940
* url::Url
4041
* bson::oid::ObjectId
4142

4243
## newtype pattern
4344

44-
Often, you might need a custom scalar that just wraps an existing type.
45+
Often, you might need a custom scalar that just wraps an existing type.
4546

4647
This can be done with the newtype pattern and a custom derive, similar to how
4748
serde supports this pattern with `#[serde(transparent)]`.
@@ -82,15 +83,15 @@ pub struct UserId(i32);
8283

8384
## Custom scalars
8485

85-
For more complex situations where you also need custom parsing or validation,
86+
For more complex situations where you also need custom parsing or validation,
8687
you can use the `graphql_scalar` proc macro.
8788

8889
Typically, you represent your custom scalars as strings.
8990

9091
The example below implements a custom scalar for a custom `Date` type.
9192

92-
Note: juniper already has built-in support for the `chrono::DateTime` type
93-
via `chrono` feature, which is enabled by default and should be used for this
93+
Note: juniper already has built-in support for the `chrono::DateTime` type
94+
via `chrono` feature, which is enabled by default and should be used for this
9495
purpose.
9596

9697
The example below is used just for illustration.
@@ -101,9 +102,9 @@ The example below is used just for illustration.
101102

102103
```rust
103104
# extern crate juniper;
104-
# mod date {
105-
# pub struct Date;
106-
# impl std::str::FromStr for Date{
105+
# mod date {
106+
# pub struct Date;
107+
# impl std::str::FromStr for Date {
107108
# type Err = String; fn from_str(_value: &str) -> Result<Self, Self::Err> { unimplemented!() }
108109
# }
109110
# // And we define how to represent date as a string.
@@ -118,7 +119,7 @@ use juniper::{Value, ParseScalarResult, ParseScalarValue};
118119
use date::Date;
119120

120121
#[juniper::graphql_scalar(description = "Date")]
121-
impl<S> GraphQLScalar for Date
122+
impl<S> GraphQLScalar for Date
122123
where
123124
S: ScalarValue
124125
{

juniper/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
- Use `null` in addition to `None` to create `Value::Null` in `graphql_value!` macro to mirror `serde_json::json!`. ([#996](https://github.com/graphql-rust/juniper/pull/996))
2020
- Add `From` impls to `InputValue` mirroring the ones for `Value` and provide better support for `Option` handling. ([#996](https://github.com/graphql-rust/juniper/pull/996))
2121
- Implement `graphql_input_value!` and `graphql_vars!` macros. ([#996](https://github.com/graphql-rust/juniper/pull/996))
22+
- Support [`time` crate](https://docs.rs/time) types as GraphQL scalars behind `time` feature. ([#1006](https://github.com/graphql-rust/juniper/pull/1006))
2223

2324
## Fixes
2425

juniper/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ travis-ci = { repository = "graphql-rust/juniper" }
2222
default = [
2323
"bson",
2424
"chrono",
25+
"time",
2526
"schema-language",
2627
"url",
2728
"uuid",
@@ -48,6 +49,7 @@ serde = { version = "1.0.8", features = ["derive"], default-features = false }
4849
serde_json = { version = "1.0.2", default-features = false, optional = true }
4950
smartstring = "0.2.6"
5051
static_assertions = "1.1"
52+
time = { version = "0.3", features = ["formatting", "macros", "parsing"], optional = true }
5153
url = { version = "2.0", optional = true }
5254

5355
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]

juniper/src/integrations/chrono.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,6 @@ use crate::{
2424
Value,
2525
};
2626

27-
#[doc(hidden)]
28-
pub static RFC3339_FORMAT: &str = "%Y-%m-%dT%H:%M:%S%.f%:z";
29-
3027
#[crate::graphql_scalar(name = "DateTimeFixedOffset", description = "DateTime")]
3128
impl<S> GraphQLScalar for DateTime<FixedOffset>
3229
where

juniper/src/integrations/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ pub mod chrono;
88
pub mod chrono_tz;
99
#[doc(hidden)]
1010
pub mod serde;
11+
#[cfg(feature = "time")]
12+
pub mod time;
1113
#[cfg(feature = "url")]
1214
pub mod url;
1315
#[cfg(feature = "uuid")]

0 commit comments

Comments
 (0)