Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add support for the time crate #1006

Merged
merged 8 commits into from
Dec 16, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions docs/book/content/types/scalars.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ but this often requires coordination with the client library intended to consume
the API you're building.

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

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

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

* uuid::Uuid
* chrono::DateTime
* time::{Date, OffsetDateTime, PrimitiveDateTime}
* url::Url
* bson::oid::ObjectId

## newtype pattern

Often, you might need a custom scalar that just wraps an existing type.
Often, you might need a custom scalar that just wraps an existing type.

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

## Custom scalars

For more complex situations where you also need custom parsing or validation,
For more complex situations where you also need custom parsing or validation,
you can use the `graphql_scalar` proc macro.

Typically, you represent your custom scalars as strings.

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

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

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

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

#[juniper::graphql_scalar(description = "Date")]
impl<S> GraphQLScalar for Date
impl<S> GraphQLScalar for Date
where
S: ScalarValue
{
Expand Down
2 changes: 2 additions & 0 deletions juniper/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ travis-ci = { repository = "graphql-rust/juniper" }
default = [
"bson",
"chrono",
"time",
"schema-language",
"url",
"uuid",
Expand All @@ -39,6 +40,7 @@ async-trait = "0.1.39"
bson = { version = "2.0", features = ["chrono-0_4"], optional = true }
chrono = { version = "0.4", default-features = false, optional = true }
chrono-tz = { version = "0.6", default-features = false, optional = true }
time = { version = "0.3", features = ["parsing", "formatting"], optional = true }
fnv = "1.0.3"
futures = { version = "0.3.1", features = ["alloc"], default-features = false }
futures-enum = { version = "0.1.12", default-features = false }
Expand Down
2 changes: 2 additions & 0 deletions juniper/src/integrations/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ pub mod chrono;
pub mod chrono_tz;
#[doc(hidden)]
pub mod serde;
#[cfg(feature = "time")]
pub mod time;
#[cfg(feature = "url")]
pub mod url;
#[cfg(feature = "uuid")]
Expand Down
Loading