Skip to content

Infer graphql "deprecation" from #[deprecated(note = "...")] in derive (and macros) #269

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Oct 28, 2018
Merged
70 changes: 70 additions & 0 deletions changelog/master.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,73 @@
generic code. To retain the current behaviour use `DefaultScalarValue` as scalar value type

[#251](https://github.com/graphql-rust/juniper/pull/251)

- The `GraphQLObject` and `GraphQLEnum` derives will mark graphql fields as
`@deprecated` when struct fields or enum variants are marked with the
builtin `#[deprecated]` attribute.

The deprecation reason can be set using the `note = ...` meta item
(e.g. `#[deprecated(note = "Replaced by betterField")]`).
The `since` attribute is ignored.

[#269](https://github.com/graphql-rust/juniper/pull/269)


- There is an alternative syntax for setting a field's _description_ and
_deprecation reason_ in the `graphql_object!` and `graphql_interface!` macros.

To __deprecate__ a graphql field:
```rust
// Original syntax for setting deprecation reason
field deprecated "Reason" my_field() -> { ... }

// New alternative syntax for deprecation reason.
#[deprecated(note = "Reason")]
field my_field() -> { ... }

// You can now also deprecate without a reason.
#[deprecated]
field my_field() -> { ... }
```

To set the __description__ of a graphql field:
```rust
// Original syntax for field descriptions
field my_field() as "Description" -> { ... }

// Original syntax for argument descriptions
field my_field(
floops: i32 as "The number of starfish to be returned. \
Can't be more than 100.",
) -> {
...
}

// New alternative syntax for field descriptions
#[doc = "Description"]
field my_field() -> { ... }

// New alternative syntax for argument descriptions
field my_field(
#[doc = "The number of starfish to be returned. \
Can't be more than 100."]
arg: i32,
) -> {
...
}

// You can also use raw strings and const &'static str.
//
// Multiple docstrings will be collapsed into a single
// description separated by newlines.
#[doc = r#"
This is my field.

Make sure not to flitz the bitlet.
Flitzing without a bitlet has undefined behaviour.
"]
#[doc = my_consts::ADDED_IN_VERSION_XYZ]
field my_field() -> { ... }
```

[#269](https://github.com/graphql-rust/juniper/pull/269)
8 changes: 4 additions & 4 deletions juniper/src/executor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ use value::Value;
use GraphQLError;

use schema::meta::{
Argument, EnumMeta, EnumValue, Field, InputObjectMeta, InterfaceMeta, ListMeta, MetaType,
NullableMeta, ObjectMeta, PlaceholderMeta, ScalarMeta, UnionMeta,
Argument, DeprecationStatus, EnumMeta, EnumValue, Field, InputObjectMeta, InterfaceMeta,
ListMeta, MetaType, NullableMeta, ObjectMeta, PlaceholderMeta, ScalarMeta, UnionMeta,
};
use schema::model::{RootNode, SchemaType, TypeType};

Expand Down Expand Up @@ -729,7 +729,7 @@ where
description: None,
arguments: None,
field_type: self.get_type::<T>(info),
deprecation_reason: None,
deprecation_status: DeprecationStatus::Current,
}
}

Expand All @@ -748,7 +748,7 @@ where
description: None,
arguments: None,
field_type: self.get_type::<I>(info),
deprecation_reason: None,
deprecation_status: DeprecationStatus::Current,
}
}

Expand Down
87 changes: 80 additions & 7 deletions juniper/src/macros/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -398,15 +398,84 @@ macro_rules! __juniper_parse_field_list {
);
};


(
success_callback = $success_callback: ident,
additional_parser = {$($additional:tt)*},
meta = {$($meta:tt)*},
items = [$({$($items: tt)*},)*],
rest = $(#[doc = $desc: tt])*
#[deprecated $(( $(since = $since: tt,)* note = $reason: tt ))* ]
field $name: ident (
$(&$executor: tt)* $(,)*
$($(#[doc = $arg_desc: expr])* $arg_name:ident $(= $arg_default: tt)* : $arg_ty: ty),* $(,)*
) -> $return_ty: ty $body: block
$($rest:tt)*
) => {
__juniper_parse_field_list!(
success_callback = $success_callback,
additional_parser = {$($additional)*},
meta = {$($meta)*},
items = [$({$($items)*},)* {
name = $name,
body = $body,
return_ty = $return_ty,
args = [
$({
arg_name = $arg_name,
arg_ty = $arg_ty,
$(arg_default = $arg_default,)*
$(arg_docstring = $arg_desc,)*
},)*
],
$(docstring = $desc,)*
deprecated = None$(.unwrap_or(Some($reason)))*,
$(executor_var = $executor,)*
},],
rest = $($rest)*
);
};
(
success_callback = $success_callback: ident,
additional_parser = {$($additional:tt)*},
meta = {$($meta:tt)*},
items = [$({$($items: tt)*},)*],
rest = $(#[doc = $desc: tt])*
field $name: ident (
$(&$executor: ident)* $(,)*
$($(#[doc = $arg_desc: expr])* $arg_name:ident $(= $arg_default: tt)* : $arg_ty: ty),* $(,)*
) -> $return_ty: ty $body: block
$($rest:tt)*
) => {
__juniper_parse_field_list!(
success_callback = $success_callback,
additional_parser = {$($additional)*},
meta = {$($meta)*},
items = [$({$($items)*},)* {
name = $name,
body = $body,
return_ty = $return_ty,
args = [
$({
arg_name = $arg_name,
arg_ty = $arg_ty,
$(arg_default = $arg_default,)*
$(arg_docstring = $arg_desc,)*
},)*
],
$(docstring = $desc,)*
$(executor_var = $executor,)*
},],
rest = $($rest)*
);
};
(
success_callback = $success_callback: ident,
additional_parser = {$($additional:tt)*},
meta = {$($meta:tt)*},
items = [$({$($items: tt)*},)*],
rest = field deprecated $reason:tt $name: ident (
$(&$executor: tt)* $(,)*
$($arg_name:ident $(= $default_value: tt)* : $arg_ty: ty $(as $arg_des: expr)*),* $(,)*
$($arg_name:ident $(= $arg_default: tt)* : $arg_ty: ty $(as $arg_desc: expr)*),* $(,)*
) -> $return_ty: ty $(as $desc: tt)* $body: block
$($rest:tt)*
) => {
Expand All @@ -422,12 +491,12 @@ macro_rules! __juniper_parse_field_list {
$({
arg_name = $arg_name,
arg_ty = $arg_ty,
$(arg_default = $arg_default,)*
$(arg_description = $arg_desc,)*
$(arg_default = $default_value,)*
},)*
],
$(decs = $desc,)*
deprecated = $reason,
deprecated = Some($reason),
$(executor_var = $executor,)*
},],
rest = $($rest)*
Expand All @@ -440,7 +509,7 @@ macro_rules! __juniper_parse_field_list {
items = [$({$($items: tt)*},)*],
rest = field $name: ident (
$(&$executor: ident)* $(,)*
$($arg_name:ident $(= $default_value: tt)* : $arg_ty: ty $(as $arg_desc: expr)*),* $(,)*
$($arg_name:ident $(= $arg_default: tt)* : $arg_ty: ty $(as $arg_desc: expr)*),* $(,)*
) -> $return_ty: ty $(as $desc: tt)* $body: block
$($rest:tt)*
) => {
Expand All @@ -456,8 +525,8 @@ macro_rules! __juniper_parse_field_list {
$({
arg_name = $arg_name,
arg_ty = $arg_ty,
$(arg_default = $arg_default,)*
$(arg_description = $arg_desc,)*
$(arg_default = $default_value,)*
},)*
],
$(decs = $desc,)*
Expand Down Expand Up @@ -601,27 +670,31 @@ macro_rules! __juniper_create_arg {
arg_ty = $arg_ty: ty,
arg_name = $arg_name: ident,
$(description = $arg_description: expr,)*
$(docstring = $arg_docstring: expr,)*
) => {
$reg.arg::<$arg_ty>(
&$crate::to_camel_case(stringify!($arg_name)),
$info,
)
$(.description($arg_description))*
$(.push_docstring($arg_docstring))*
};

(
registry = $reg: ident,
info = $info: ident,
arg_ty = $arg_ty: ty,
arg_name = $arg_name: ident,
$(description = $arg_description: expr,)*
default = $arg_default: expr,
$(description = $arg_description: expr,)*
$(docstring = $arg_docstring: expr,)*
) => {
$reg.arg_with_default::<$arg_ty>(
&$crate::to_camel_case(stringify!($arg_name)),
&($arg_default),
$info,
)
$(.description($arg_description))*
$(.push_docstring($arg_docstring))*
};
}
8 changes: 6 additions & 2 deletions juniper/src/macros/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,12 @@ macro_rules! graphql_interface {
args = [$({
arg_name = $arg_name : ident,
arg_ty = $arg_ty: ty,
$(arg_description = $arg_description: expr,)*
$(arg_default = $arg_default: expr,)*
$(arg_description = $arg_description: expr,)*
$(arg_docstring = $arg_docstring: expr,)*
},)*],
$(decs = $fn_description: expr,)*
$(docstring = $docstring: expr,)*
$(deprecated = $deprecated: expr,)*
$(executor_var = $executor: ident,)*
},)*],
Expand Down Expand Up @@ -151,15 +153,17 @@ macro_rules! graphql_interface {
info
)
$(.description($fn_description))*
$(.push_docstring($docstring))*
$(.deprecated($deprecated))*
$(.argument(
__juniper_create_arg!(
registry = registry,
info = info,
arg_ty = $arg_ty,
arg_name = $arg_name,
$(description = $arg_description,)*
$(default = $arg_default,)*
$(description = $arg_description,)*
$(docstring = $arg_docstring,)*
)
))*,
)*];
Expand Down
Loading