From 97e1005178b4bd4d96d85ffb7c82cd36b66380b3 Mon Sep 17 00:00:00 2001 From: Christoph Herzog Date: Tue, 25 Jun 2019 13:35:31 +0200 Subject: [PATCH] Clean up ScalarValue transparent derive argument handling and documentation. --- docs/book/content/types/scalars.md | 3 +-- juniper/CHANGELOG.md | 3 +-- juniper_codegen/src/derive_scalar_value.rs | 12 +++++------- juniper_codegen/src/lib.rs | 2 -- 4 files changed, 7 insertions(+), 13 deletions(-) diff --git a/docs/book/content/types/scalars.md b/docs/book/content/types/scalars.md index 52d227e0..7ab9a1e6 100644 --- a/docs/book/content/types/scalars.md +++ b/docs/book/content/types/scalars.md @@ -41,11 +41,10 @@ crates. They are enabled via features that are on by default. 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 `#[transparent]`. +serde supports this pattern with `#[serde(transparent)]`. ```rust #[derive(juniper::GraphQLScalarValue)] -#[graphql(transparent)] pub struct UserId(i32); #[derive(juniper::GraphQLObject)] diff --git a/juniper/CHANGELOG.md b/juniper/CHANGELOG.md index 526b94f8..9424ea27 100644 --- a/juniper/CHANGELOG.md +++ b/juniper/CHANGELOG.md @@ -6,13 +6,12 @@ See [#345](https://github.com/graphql-rust/juniper/pull/345). The newtype pattern can now be used with the `GraphQLScalarValue` custom derive to easily implement custom scalar values that just wrap another scalar, -similar to serdes `#[transparent]` functionality. +similar to serdes `#[serde(transparent)]` functionality. Example: ```rust #[derive(juniper::GraphQLScalarValue)] -#[graphql(transparent)] struct UserId(i32); ``` diff --git a/juniper_codegen/src/derive_scalar_value.rs b/juniper_codegen/src/derive_scalar_value.rs index 43a2d274..27bf14e8 100644 --- a/juniper_codegen/src/derive_scalar_value.rs +++ b/juniper_codegen/src/derive_scalar_value.rs @@ -5,9 +5,9 @@ use syn::{self, Data, Fields, Ident, Variant}; use crate::util; -#[derive(Debug)] +#[derive(Debug, Default)] struct TransparentAttributes { - transparent: bool, + transparent: Option, name: Option, description: Option, } @@ -15,7 +15,7 @@ struct TransparentAttributes { impl syn::parse::Parse for TransparentAttributes { fn parse(input: syn::parse::ParseStream) -> syn::parse::Result { let mut output = Self { - transparent: false, + transparent: None, name: None, description: None, }; @@ -37,7 +37,7 @@ impl syn::parse::Parse for TransparentAttributes { output.description = Some(val.value()); } "transparent" => { - output.transparent = true; + output.transparent = Some(true); } other => { return Err(content.error(format!("Unknown attribute: {}", other))); @@ -62,9 +62,7 @@ impl TransparentAttributes { } Ok(parsed) } - None => { - panic!("Missing required attribute: #[graphql(transparent)]"); - } + None => Ok(Default::default()), } } } diff --git a/juniper_codegen/src/lib.rs b/juniper_codegen/src/lib.rs index 00b1028e..f578ce31 100644 --- a/juniper_codegen/src/lib.rs +++ b/juniper_codegen/src/lib.rs @@ -68,9 +68,7 @@ pub fn derive_object(input: TokenStream) -> TokenStream { /// /// ```rust /// // Deriving GraphQLScalar is all that is required. -/// // Note the #[graphql(transparent)] attribute, which is mandatory. /// #[derive(juniper::GraphQLScalarValue)] -/// #[graphql(transparent)] /// struct UserId(String); /// /// #[derive(juniper::GraphQLObject)]