Clean up ScalarValue transparent derive argument handling and documentation.
This commit is contained in:
parent
7e0e7beaa7
commit
97e1005178
4 changed files with 7 additions and 13 deletions
|
@ -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)]
|
||||
|
|
|
@ -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);
|
||||
```
|
||||
|
||||
|
|
|
@ -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<bool>,
|
||||
name: Option<String>,
|
||||
description: Option<String>,
|
||||
}
|
||||
|
@ -15,7 +15,7 @@ struct TransparentAttributes {
|
|||
impl syn::parse::Parse for TransparentAttributes {
|
||||
fn parse(input: syn::parse::ParseStream) -> syn::parse::Result<Self> {
|
||||
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()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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)]
|
||||
|
|
Loading…
Reference in a new issue