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.
|
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
|
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
|
```rust
|
||||||
#[derive(juniper::GraphQLScalarValue)]
|
#[derive(juniper::GraphQLScalarValue)]
|
||||||
#[graphql(transparent)]
|
|
||||||
pub struct UserId(i32);
|
pub struct UserId(i32);
|
||||||
|
|
||||||
#[derive(juniper::GraphQLObject)]
|
#[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
|
The newtype pattern can now be used with the `GraphQLScalarValue` custom derive
|
||||||
to easily implement custom scalar values that just wrap another scalar,
|
to easily implement custom scalar values that just wrap another scalar,
|
||||||
similar to serdes `#[transparent]` functionality.
|
similar to serdes `#[serde(transparent)]` functionality.
|
||||||
|
|
||||||
Example:
|
Example:
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
#[derive(juniper::GraphQLScalarValue)]
|
#[derive(juniper::GraphQLScalarValue)]
|
||||||
#[graphql(transparent)]
|
|
||||||
struct UserId(i32);
|
struct UserId(i32);
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
|
@ -5,9 +5,9 @@ use syn::{self, Data, Fields, Ident, Variant};
|
||||||
|
|
||||||
use crate::util;
|
use crate::util;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug, Default)]
|
||||||
struct TransparentAttributes {
|
struct TransparentAttributes {
|
||||||
transparent: bool,
|
transparent: Option<bool>,
|
||||||
name: Option<String>,
|
name: Option<String>,
|
||||||
description: Option<String>,
|
description: Option<String>,
|
||||||
}
|
}
|
||||||
|
@ -15,7 +15,7 @@ struct TransparentAttributes {
|
||||||
impl syn::parse::Parse for TransparentAttributes {
|
impl syn::parse::Parse for TransparentAttributes {
|
||||||
fn parse(input: syn::parse::ParseStream) -> syn::parse::Result<Self> {
|
fn parse(input: syn::parse::ParseStream) -> syn::parse::Result<Self> {
|
||||||
let mut output = Self {
|
let mut output = Self {
|
||||||
transparent: false,
|
transparent: None,
|
||||||
name: None,
|
name: None,
|
||||||
description: None,
|
description: None,
|
||||||
};
|
};
|
||||||
|
@ -37,7 +37,7 @@ impl syn::parse::Parse for TransparentAttributes {
|
||||||
output.description = Some(val.value());
|
output.description = Some(val.value());
|
||||||
}
|
}
|
||||||
"transparent" => {
|
"transparent" => {
|
||||||
output.transparent = true;
|
output.transparent = Some(true);
|
||||||
}
|
}
|
||||||
other => {
|
other => {
|
||||||
return Err(content.error(format!("Unknown attribute: {}", other)));
|
return Err(content.error(format!("Unknown attribute: {}", other)));
|
||||||
|
@ -62,9 +62,7 @@ impl TransparentAttributes {
|
||||||
}
|
}
|
||||||
Ok(parsed)
|
Ok(parsed)
|
||||||
}
|
}
|
||||||
None => {
|
None => Ok(Default::default()),
|
||||||
panic!("Missing required attribute: #[graphql(transparent)]");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -68,9 +68,7 @@ pub fn derive_object(input: TokenStream) -> TokenStream {
|
||||||
///
|
///
|
||||||
/// ```rust
|
/// ```rust
|
||||||
/// // Deriving GraphQLScalar is all that is required.
|
/// // Deriving GraphQLScalar is all that is required.
|
||||||
/// // Note the #[graphql(transparent)] attribute, which is mandatory.
|
|
||||||
/// #[derive(juniper::GraphQLScalarValue)]
|
/// #[derive(juniper::GraphQLScalarValue)]
|
||||||
/// #[graphql(transparent)]
|
|
||||||
/// struct UserId(String);
|
/// struct UserId(String);
|
||||||
///
|
///
|
||||||
/// #[derive(juniper::GraphQLObject)]
|
/// #[derive(juniper::GraphQLObject)]
|
||||||
|
|
Loading…
Add table
Reference in a new issue