Skip to content

Commit

Permalink
Rework #[derive(GraphQLInputObject)] macro implementation (#1052)
Browse files Browse the repository at this point in the history
Co-authored-by: Kai Ren <tyranron@gmail.com>
  • Loading branch information
ilslv and tyranron committed Jun 28, 2022
1 parent 9ca2364 commit 927e422
Show file tree
Hide file tree
Showing 29 changed files with 1,925 additions and 1,227 deletions.
4 changes: 2 additions & 2 deletions juniper/src/executor_tests/introspection/input_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ struct FieldDescription {

#[derive(GraphQLInputObject, Debug)]
struct FieldWithDefaults {
#[graphql(default = "123")]
#[graphql(default = 123)]
field_one: i32,
#[graphql(default = "456", description = "The second field")]
#[graphql(default = 456, description = "The second field")]
field_two: i32,
}

Expand Down
2 changes: 1 addition & 1 deletion juniper/src/executor_tests/variables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ struct ExampleInputObject {

#[derive(GraphQLInputObject, Debug)]
struct InputWithDefaults {
#[graphql(default = "123")]
#[graphql(default = 123)]
a: i32,
}

Expand Down
65 changes: 65 additions & 0 deletions juniper_codegen/src/common/default.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
//! Common functions, definitions and extensions for parsing and code generation
//! of [GraphQL default values][0]
//!
//! [0]: https://spec.graphql.org/October2021#DefaultValue

use proc_macro2::TokenStream;
use quote::{quote, ToTokens};
use syn::{
parse::{Parse, ParseStream},
token,
};

use crate::common::parse::ParseBufferExt as _;

/// Representation of a [GraphQL default value][0] for code generation.
///
/// [0]: https://spec.graphql.org/October2021#DefaultValue
#[derive(Clone, Debug)]
pub(crate) enum Value {
/// [`Default`] implementation should be used.
Default,

/// Explicit [`Expr`]ession to be used as the [default value][0].
///
/// [`Expr`]: syn::Expr
/// [0]: https://spec.graphql.org/October2021#DefaultValue
Expr(Box<syn::Expr>),
}

impl Default for Value {
fn default() -> Self {
Self::Default
}
}

impl From<Option<syn::Expr>> for Value {
fn from(opt: Option<syn::Expr>) -> Self {
match opt {
Some(expr) => Self::Expr(Box::new(expr)),
None => Self::Default,
}
}
}

impl Parse for Value {
fn parse(input: ParseStream<'_>) -> syn::Result<Self> {
Ok(input
.try_parse::<token::Eq>()?
.map(|_| input.parse::<syn::Expr>())
.transpose()?
.into())
}
}

impl ToTokens for Value {
fn to_tokens(&self, into: &mut TokenStream) {
match self {
Self::Default => quote! {
::std::default::Default::default()
}
.to_tokens(into),
Self::Expr(expr) => expr.to_tokens(into),
}
}
}
8 changes: 4 additions & 4 deletions juniper_codegen/src/common/field/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Common functions, definitions and extensions for parsing and code generation
//! of [GraphQL fields][1]
//!
//! [1]: https://spec.graphql.org/June2018/#sec-Language.Fields.
//! [1]: https://spec.graphql.org/June2018/#sec-Language.Fields

pub(crate) mod arg;

Expand Down Expand Up @@ -42,16 +42,16 @@ pub(crate) struct Attr {

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

/// Explicitly specified [deprecation][2] of this [GraphQL field][1].
///
/// If [`None`], then Rust `#[deprecated]` attribute is used as the
/// If [`None`], then Rust `#[deprecated]` attribute will be used as the
/// [deprecation][2], if any.
///
/// [1]: https://spec.graphql.org/June2018/#sec-Language.Fields
Expand Down
1 change: 1 addition & 0 deletions juniper_codegen/src/common/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Common functions, definitions and extensions for code generation, used by this crate.

pub(crate) mod default;
pub(crate) mod field;
pub(crate) mod gen;
pub(crate) mod parse;
Expand Down
151 changes: 0 additions & 151 deletions juniper_codegen/src/derive_input_object.rs

This file was deleted.

3 changes: 2 additions & 1 deletion juniper_codegen/src/graphql_enum/derive.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
//! Code generation for `#[derive(GraphQLEnum)]` macro.

use std::collections::HashSet;

use proc_macro2::TokenStream;
use quote::ToTokens as _;
use std::collections::HashSet;
use syn::{ext::IdentExt as _, parse_quote, spanned::Spanned};

use crate::{
Expand Down
16 changes: 9 additions & 7 deletions juniper_codegen/src/graphql_enum/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use crate::{
};

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

/// Explicitly specified [description][2] of this [GraphQL enum][0].
///
/// If [`None`], then Rust doc comment will be used as [description][2], if
/// any.
/// If [`None`], then Rust doc comment will be used as the [description][2],
/// if any.
///
/// [0]: https://spec.graphql.org/October2021#sec-Enums
/// [2]: https://spec.graphql.org/October2021#sec-Descriptions
Expand Down Expand Up @@ -190,22 +190,23 @@ impl ContainerAttr {
struct VariantAttr {
/// Explicitly specified name of this [GraphQL enum value][1].
///
/// If [`None`], then Rust enum variant's name is used by default.
/// If [`None`], then Rust enum variant's name will be used by default.
///
/// [1]: https://spec.graphql.org/October2021#sec-Enum-Value
name: Option<SpanContainer<String>>,

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

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

/// [`syn::Generics`] of the Rust enum behind this [GraphQL enum][0].
/// [`Generics`] of the Rust enum behind this [GraphQL enum][0].
///
/// [`Generics`]: syn::Generics
/// [0]: https://spec.graphql.org/October2021#sec-Enums
generics: syn::Generics,

Expand Down

0 comments on commit 927e422

Please sign in to comment.