Skip to content

Commit 927e422

Browse files
ilslvtyranron
andauthored
Rework #[derive(GraphQLInputObject)] macro implementation (#1052)
Co-authored-by: Kai Ren <tyranron@gmail.com>
1 parent 9ca2364 commit 927e422

29 files changed

+1925
-1227
lines changed

juniper/src/executor_tests/introspection/input_object.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,9 @@ struct FieldDescription {
7676

7777
#[derive(GraphQLInputObject, Debug)]
7878
struct FieldWithDefaults {
79-
#[graphql(default = "123")]
79+
#[graphql(default = 123)]
8080
field_one: i32,
81-
#[graphql(default = "456", description = "The second field")]
81+
#[graphql(default = 456, description = "The second field")]
8282
field_two: i32,
8383
}
8484

juniper/src/executor_tests/variables.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ struct ExampleInputObject {
4949

5050
#[derive(GraphQLInputObject, Debug)]
5151
struct InputWithDefaults {
52-
#[graphql(default = "123")]
52+
#[graphql(default = 123)]
5353
a: i32,
5454
}
5555

juniper_codegen/src/common/default.rs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
//! Common functions, definitions and extensions for parsing and code generation
2+
//! of [GraphQL default values][0]
3+
//!
4+
//! [0]: https://spec.graphql.org/October2021#DefaultValue
5+
6+
use proc_macro2::TokenStream;
7+
use quote::{quote, ToTokens};
8+
use syn::{
9+
parse::{Parse, ParseStream},
10+
token,
11+
};
12+
13+
use crate::common::parse::ParseBufferExt as _;
14+
15+
/// Representation of a [GraphQL default value][0] for code generation.
16+
///
17+
/// [0]: https://spec.graphql.org/October2021#DefaultValue
18+
#[derive(Clone, Debug)]
19+
pub(crate) enum Value {
20+
/// [`Default`] implementation should be used.
21+
Default,
22+
23+
/// Explicit [`Expr`]ession to be used as the [default value][0].
24+
///
25+
/// [`Expr`]: syn::Expr
26+
/// [0]: https://spec.graphql.org/October2021#DefaultValue
27+
Expr(Box<syn::Expr>),
28+
}
29+
30+
impl Default for Value {
31+
fn default() -> Self {
32+
Self::Default
33+
}
34+
}
35+
36+
impl From<Option<syn::Expr>> for Value {
37+
fn from(opt: Option<syn::Expr>) -> Self {
38+
match opt {
39+
Some(expr) => Self::Expr(Box::new(expr)),
40+
None => Self::Default,
41+
}
42+
}
43+
}
44+
45+
impl Parse for Value {
46+
fn parse(input: ParseStream<'_>) -> syn::Result<Self> {
47+
Ok(input
48+
.try_parse::<token::Eq>()?
49+
.map(|_| input.parse::<syn::Expr>())
50+
.transpose()?
51+
.into())
52+
}
53+
}
54+
55+
impl ToTokens for Value {
56+
fn to_tokens(&self, into: &mut TokenStream) {
57+
match self {
58+
Self::Default => quote! {
59+
::std::default::Default::default()
60+
}
61+
.to_tokens(into),
62+
Self::Expr(expr) => expr.to_tokens(into),
63+
}
64+
}
65+
}

juniper_codegen/src/common/field/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Common functions, definitions and extensions for parsing and code generation
22
//! of [GraphQL fields][1]
33
//!
4-
//! [1]: https://spec.graphql.org/June2018/#sec-Language.Fields.
4+
//! [1]: https://spec.graphql.org/June2018/#sec-Language.Fields
55
66
pub(crate) mod arg;
77

@@ -42,16 +42,16 @@ pub(crate) struct Attr {
4242

4343
/// Explicitly specified [description][2] of this [GraphQL field][1].
4444
///
45-
/// If [`None`], then Rust doc comment is used as the [description][2], if
46-
/// any.
45+
/// If [`None`], then Rust doc comment will be used as the [description][2],
46+
/// if any.
4747
///
4848
/// [1]: https://spec.graphql.org/June2018/#sec-Language.Fields
4949
/// [2]: https://spec.graphql.org/June2018/#sec-Descriptions
5050
pub(crate) description: Option<SpanContainer<syn::LitStr>>,
5151

5252
/// Explicitly specified [deprecation][2] of this [GraphQL field][1].
5353
///
54-
/// If [`None`], then Rust `#[deprecated]` attribute is used as the
54+
/// If [`None`], then Rust `#[deprecated]` attribute will be used as the
5555
/// [deprecation][2], if any.
5656
///
5757
/// [1]: https://spec.graphql.org/June2018/#sec-Language.Fields

juniper_codegen/src/common/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! Common functions, definitions and extensions for code generation, used by this crate.
22
3+
pub(crate) mod default;
34
pub(crate) mod field;
45
pub(crate) mod gen;
56
pub(crate) mod parse;

juniper_codegen/src/derive_input_object.rs

Lines changed: 0 additions & 151 deletions
This file was deleted.

juniper_codegen/src/graphql_enum/derive.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
//! Code generation for `#[derive(GraphQLEnum)]` macro.
22
3+
use std::collections::HashSet;
4+
35
use proc_macro2::TokenStream;
46
use quote::ToTokens as _;
5-
use std::collections::HashSet;
67
use syn::{ext::IdentExt as _, parse_quote, spanned::Spanned};
78

89
use crate::{

juniper_codegen/src/graphql_enum/mod.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use crate::{
3030
};
3131

3232
/// Available arguments behind `#[graphql]` attribute placed on a Rust enum
33-
/// definition, when generating code for a [GraphQL enum][0] type.
33+
/// definition, when generating code for a [GraphQL enum][0].
3434
///
3535
/// [0]: https://spec.graphql.org/October2021#sec-Enums
3636
#[derive(Debug, Default)]
@@ -44,8 +44,8 @@ struct ContainerAttr {
4444

4545
/// Explicitly specified [description][2] of this [GraphQL enum][0].
4646
///
47-
/// If [`None`], then Rust doc comment will be used as [description][2], if
48-
/// any.
47+
/// If [`None`], then Rust doc comment will be used as the [description][2],
48+
/// if any.
4949
///
5050
/// [0]: https://spec.graphql.org/October2021#sec-Enums
5151
/// [2]: https://spec.graphql.org/October2021#sec-Descriptions
@@ -190,22 +190,23 @@ impl ContainerAttr {
190190
struct VariantAttr {
191191
/// Explicitly specified name of this [GraphQL enum value][1].
192192
///
193-
/// If [`None`], then Rust enum variant's name is used by default.
193+
/// If [`None`], then Rust enum variant's name will be used by default.
194194
///
195195
/// [1]: https://spec.graphql.org/October2021#sec-Enum-Value
196196
name: Option<SpanContainer<String>>,
197197

198198
/// Explicitly specified [description][2] of this [GraphQL enum value][1].
199199
///
200-
/// If [`None`], then Rust doc comment is used as [description][2], if any.
200+
/// If [`None`], then Rust doc comment will be used as the [description][2],
201+
/// if any.
201202
///
202203
/// [1]: https://spec.graphql.org/October2021#sec-Enum-Value
203204
/// [2]: https://spec.graphql.org/October2021#sec-Descriptions
204205
description: Option<SpanContainer<String>>,
205206

206207
/// Explicitly specified [deprecation][2] of this [GraphQL enum value][1].
207208
///
208-
/// If [`None`], then Rust `#[deprecated]` attribute is used as the
209+
/// If [`None`], then Rust `#[deprecated]` attribute will be used as the
209210
/// [deprecation][2], if any.
210211
///
211212
/// If the inner [`Option`] is [`None`], then no [reason][3] was provided.
@@ -357,8 +358,9 @@ struct Definition {
357358
/// [0]: https://spec.graphql.org/October2021#sec-Enums
358359
ident: syn::Ident,
359360

360-
/// [`syn::Generics`] of the Rust enum behind this [GraphQL enum][0].
361+
/// [`Generics`] of the Rust enum behind this [GraphQL enum][0].
361362
///
363+
/// [`Generics`]: syn::Generics
362364
/// [0]: https://spec.graphql.org/October2021#sec-Enums
363365
generics: syn::Generics,
364366

0 commit comments

Comments
 (0)