Skip to content

Commit 0268d03

Browse files
committed
Clean up ScalarValue transparent derive argument handling and documentation.
1 parent 93eebff commit 0268d03

File tree

4 files changed

+7
-13
lines changed

4 files changed

+7
-13
lines changed

docs/book/content/types/scalars.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,10 @@ crates. They are enabled via features that are on by default.
4141
Often, you might need a custom scalar that just wraps an existing type.
4242

4343
This can be done with the newtype pattern and a custom derive, similar to how
44-
serde supports this pattern with `#[transparent]`.
44+
serde supports this pattern with `#[serde(transparent)]`.
4545

4646
```rust
4747
#[derive(juniper::GraphQLScalarValue)]
48-
#[graphql(transparent)]
4948
pub struct UserId(i32);
5049

5150
#[derive(juniper::GraphQLObject)]

juniper/CHANGELOG.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,12 @@ See [#345](https://github.com/graphql-rust/juniper/pull/345).
66

77
The newtype pattern can now be used with the `GraphQLScalarValue` custom derive
88
to easily implement custom scalar values that just wrap another scalar,
9-
similar to serdes `#[transparent]` functionality.
9+
similar to serdes `#[serde(transparent)]` functionality.
1010

1111
Example:
1212

1313
```rust
1414
#[derive(juniper::GraphQLScalarValue)]
15-
#[graphql(transparent)]
1615
struct UserId(i32);
1716
```
1817

juniper_codegen/src/derive_scalar_value.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,17 @@ use syn::{self, Data, Fields, Ident, Variant};
55

66
use crate::util;
77

8-
#[derive(Debug)]
8+
#[derive(Debug, Default)]
99
struct TransparentAttributes {
10-
transparent: bool,
10+
transparent: Option<bool>,
1111
name: Option<String>,
1212
description: Option<String>,
1313
}
1414

1515
impl syn::parse::Parse for TransparentAttributes {
1616
fn parse(input: syn::parse::ParseStream) -> syn::parse::Result<Self> {
1717
let mut output = Self {
18-
transparent: false,
18+
transparent: None,
1919
name: None,
2020
description: None,
2121
};
@@ -37,7 +37,7 @@ impl syn::parse::Parse for TransparentAttributes {
3737
output.description = Some(val.value());
3838
}
3939
"transparent" => {
40-
output.transparent = true;
40+
output.transparent = Some(true);
4141
}
4242
other => {
4343
return Err(content.error(format!("Unknown attribute: {}", other)));
@@ -62,9 +62,7 @@ impl TransparentAttributes {
6262
}
6363
Ok(parsed)
6464
}
65-
None => {
66-
panic!("Missing required attribute: #[graphql(transparent)]");
67-
}
65+
None => Ok(Default::default()),
6866
}
6967
}
7068
}

juniper_codegen/src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,7 @@ pub fn derive_object(input: TokenStream) -> TokenStream {
6868
///
6969
/// ```rust
7070
/// // Deriving GraphQLScalar is all that is required.
71-
/// // Note the #[graphql(transparent)] attribute, which is mandatory.
7271
/// #[derive(juniper::GraphQLScalarValue)]
73-
/// #[graphql(transparent)]
7472
/// struct UserId(String);
7573
///
7674
/// #[derive(juniper::GraphQLObject)]

0 commit comments

Comments
 (0)