Skip to content

Commit d66bac3

Browse files
committed
Implement ToInputValue when creating input objects
1 parent f7e0ffa commit d66bac3

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

src/macros/input_object.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,19 @@ macro_rules! graphql_input_object {
7878
})
7979
};
8080

81+
// Generate the ToInputValue::To method body, provided self in $self
82+
(
83+
@generate_to_input_value,
84+
$name:tt, $selfvar:tt,
85+
( $($field_name:ident $(= $default:tt)* : $field_type:ty $(as $descr:tt)* $(,)* ),* )
86+
) => {
87+
$crate::InputValue::object(vec![
88+
$(
89+
($crate::to_camel_case(stringify!($field_name)), $selfvar.$field_name.to())
90+
),*
91+
].into_iter().collect())
92+
};
93+
8194
// Generate the struct declaration, including (Rust) meta attributes
8295
(
8396
@generate_struct_fields,
@@ -220,6 +233,12 @@ macro_rules! graphql_input_object {
220233
}
221234
}
222235

236+
impl $crate::ToInputValue for $name {
237+
fn to(&self) -> $crate::InputValue {
238+
graphql_input_object!(@generate_to_input_value, $name, self, $fields)
239+
}
240+
}
241+
223242
impl $crate::GraphQLType for $name {
224243
type Context = ();
225244

0 commit comments

Comments
 (0)